view victoriametrics_write.py @ 7:a640efa9fb01

logging
author drewp@bigasterisk.com
date Sat, 10 Aug 2024 23:05:18 -0700
parents cd1b8d7bda78
children
line wrap: on
line source

import httpx
import logging
log = logging.getLogger()

class MetricsWriter:
    # todo: this could merge quick writes, keep the connection open, etc.
    agentImportUrl = "http://victoriametrics-forever-vmagent/m/forever/vmagent/api/v1/import"

    def write(self, t, metricEvent):
        # To see inserted data, try this:
        #   curl http://`khost victoriametrics-vmselect`/m/vmselect/select/0/prometheus/api/v1/export -d 'match[]=vm_http_request_errors_total'
        labelDict = dict((x['labelName'], x['labelValue']) for x in metricEvent['labels'])
        promBody = {
            "metric": {
                "__name__": metricEvent['name']
            } | labelDict,
            "values": [metricEvent['value']],
            "timestamps": [int(t * 1000)],
        }
        # log.info(promBody)
        req = httpx.post(
            self.agentImportUrl,
            json=promBody,
            headers={'content-type': 'application/stream+json'},
        )
        req.raise_for_status()