Initial version for Prometheus File Driver

-Plugin
-Tests
-Updates for requirements, setup, tox
This commit is contained in:
Iury Gregory Melo Ferreira 2019-03-26 09:55:27 +01:00
parent 5f80b9781c
commit a94a8d8eee
9 changed files with 74 additions and 3 deletions

1
.gitignore vendored
View File

@ -125,3 +125,4 @@ dmypy.json
# End of https://www.gitignore.io/api/python
.stestr/

3
.stestr.conf Normal file
View File

@ -0,0 +1,3 @@
[DEFAULT]
test_path=./ironic_prometheus_exporter/tests
top_path=./

View File

@ -1,3 +1,17 @@
Ironic Prometheus Exporter
==========================
Configuration
=============
After install the driver you will need to update the :ironic.conf: and add
:file_dir: and :file_name: options
```
[oslo_messaging_notifications]
driver = prometheus_exporter
transport_url = fake://
file_dir=/tmp/ironic_prometheus_exporter
file_name=myfile.txt
```

View File

@ -0,0 +1,29 @@
import os
from oslo_config import cfg
from oslo_messaging.notify import notifier
prometheus_opts = [
cfg.StrOpt('file_name', required=True),
cfg.StrOpt('file_dir', required=True)
]
class PrometheusFileDriver(notifier.Driver):
"Publish notifications into a File to be used by Prometheus"
def __init__(self, conf, topics, transport):
conf.register_opts(prometheus_opts,
group='oslo_messaging_notifications')
self.conf = conf
if not os.path.exists(self.conf.oslo_messaging_notifications.file_dir):
os.makedirs(self.conf.oslo_messaging_notifications.file_dir)
self.file_dir = self.conf.oslo_messaging_notifications.file_dir
self.file_name = self.conf.oslo_messaging_notifications.file_name
super(PrometheusFileDriver, self).__init__(conf, topics, transport)
def notify(self, ctxt, message, priority, retry):
prometheus_file = open((self.file_dir + '/' + self.file_name), 'w')
prometheus_file.write(str(message))
prometheus_file.close()

View File

@ -0,0 +1,23 @@
import oslo_messaging
from oslo_messaging.tests import utils as test_utils
class TestPrometheusFileNotifier(test_utils.BaseTestCase):
def setUp(self):
super(TestPrometheusFileNotifier, self).setUp()
def test_notifier(self):
transport = oslo_messaging.get_notification_transport(self.conf)
my_notifier = oslo_messaging.Notifier(transport,
driver='prometheus_exporter',
topics=['my_topics'])
self.config(file_name='test.txt',
file_dir='/tmp/ironic_prometheus_exporter',
group='oslo_messaging_notifications')
self.assertEqual(self.conf.oslo_messaging_notifications.file_name,
"test.txt")
self.assertEqual(self.conf.oslo_messaging_notifications.file_dir,
'/tmp/ironic_prometheus_exporter')
my_notifier.debug({}, "My Event", 'Payload')

View File

@ -2,3 +2,5 @@ pbr!=2.1.0,>=2.0.0 # Apache-2.0
flake8
stevedore>=1.20.0 # Apache-2.0
oslo.messaging!=9.0.0 # Apache-2.0
oslotest>=2.15.0 # Apache-2.0
stestr>=2.0.0 # Apache-2.0

View File

@ -23,4 +23,4 @@ packages =
[entry_points]
oslo.messaging.notify.drivers =
simplefile = ironic_prometheus_exporter.messaging:FileDriver
prometheus_exporter = ironic_prometheus_exporter.messaging:PrometheusFileDriver

View File

@ -10,7 +10,7 @@ setenv =
VIRTUAL_ENV={envdir}
PYTHONWARNINGS=default::DeprecationWarning
deps = -r{toxinidir}/requirements.txt
commands = python setup.py test
commands = stestr run {posargs}
[testenv:pep8]
basepython = python3
@ -22,7 +22,6 @@ commands = {posargs}
[flake8]
ignore = E129
show-source = True
builtins = _
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build