-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
30 lines (26 loc) · 1.09 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from prometheus_client.parser import text_string_to_metric_families
from opsdroid.skill import Skill
from opsdroid.matchers import match_parse
import requests
class PrometheusScraperSkill(Skill):
def get_metrics(self, target):
defined_metrics = self.config.get('metrics', False)
req = requests.get(target)
for family in text_string_to_metric_families(req.text):
for sample in family.samples:
if not defined_metrics:
yield sample
elif sample[0] in defined_metrics:
yield sample
@match_parse('/metrics')
async def metrics(self, message):
targets = self.config['targets']
for target in targets:
for metric in self.get_metrics(target):
msg = 'Name: %s\n' % metric[0]
if metric[1] != {}:
msg += 'Labels:\n'
for label in metric[1].keys():
msg += ' %s: %s\n' % (label, metric[1][label])
msg += 'Value: %s' % metric[2]
await message.respond(msg)