/prometheus_remote_write_payload

Utility to generate Prometheus remote-write payloads

Primary LanguagePythonMIT LicenseMIT

Prometheus Remote-Write Payload Generator

This utility generates payloads for Prometheus remote-write, compatible with Python 3 and MicroPython environments.

Requirements

  • Python 3 or MicroPython

Installation

Python 3

pip install git+https://github.com/ttk1/prometheus_remote_write_payload.git

MicroPython (mip)

import mip
mip.install("github:ttk1/prometheus_remote_write_payload")

Example Usage

Python 3

import sys
import time
from prometheus_remote_write_payload import PrometheusRemoteWritePayload

prometheus = PrometheusRemoteWritePayload()
prometheus.add_data(
    "test_test", {"instance": "test_instance"}, 123.456, int(time.time() * 1000)
)
sys.stdout.buffer.write(prometheus.get_payload())
python example.py | curl -u "${user}:${password}" --data-binary @- https://${prometheus-remote-write-endpoint}

MicroPython

import utime
import network
import urequests
import ntptime

from prometheus_remote_write_payload import PrometheusRemoteWritePayload

ssid = "ssid"
password = "password for ssid"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
utime.sleep(5)
if wlan.status() != 3:
    raise Exception("network connection failed")
ntptime.settime()

prometheus = PrometheusRemoteWritePayload()
prometheus.add_data(
    "test_test", {"instance": "test_micropython"}, 987.654, int(utime.time() * 1000)
)

prometheus_remote_write_endpoint = "https://prometheus-remote-write-endpoint"
prometheus_remote_write_endpoint_basic_auth = ("user", "password")
urequests.post(
    prometheus_remote_write_endpoint,
    data=prometheus.get_payload(),
    auth=prometheus_remote_write_endpoint_basic_auth,
)