Mercurial > code > home > repos > mqtt_metrics
comparison victoriametrics_write.py @ 4:cd1b8d7bda78
get metrics writing to victoriametrics
author | drewp@bigasterisk.com |
---|---|
date | Sat, 10 Aug 2024 21:16:19 -0700 |
parents | |
children | a640efa9fb01 |
comparison
equal
deleted
inserted
replaced
3:41e36f98f3b8 | 4:cd1b8d7bda78 |
---|---|
1 import httpx | |
2 import logging | |
3 log = logging.getLogger() | |
4 | |
5 class MetricsWriter: | |
6 # todo: this could merge quick writes, keep the connection open, etc. | |
7 agentImportUrl = "http://victoriametrics-forever-vmagent/m/forever/vmagent/api/v1/import" | |
8 | |
9 def write(self, t, metricEvent): | |
10 # To see inserted data, try this: | |
11 # curl http://`khost victoriametrics-vmselect`/m/vmselect/select/0/prometheus/api/v1/export -d 'match[]=vm_http_request_errors_total' | |
12 labelDict = dict((x['labelName'], x['labelValue']) for x in metricEvent['labels']) | |
13 promBody = { | |
14 "metric": { | |
15 "__name__": metricEvent['name'] | |
16 } | labelDict, | |
17 "values": [metricEvent['value']], | |
18 "timestamps": [int(t * 1000)], | |
19 } | |
20 log.info(promBody) | |
21 req = httpx.post( | |
22 self.agentImportUrl, | |
23 json=promBody, | |
24 headers={'content-type': 'application/stream+json'}, | |
25 ) | |
26 req.raise_for_status() |