4
|
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 }
|
7
|
20 # log.info(promBody)
|
4
|
21 req = httpx.post(
|
|
22 self.agentImportUrl,
|
|
23 json=promBody,
|
|
24 headers={'content-type': 'application/stream+json'},
|
|
25 )
|
|
26 req.raise_for_status()
|