This utility generates payloads for Prometheus remote-write, compatible with Python 3 and MicroPython environments.
- Python 3 or MicroPython
pip install git+https://github.com/ttk1/prometheus_remote_write_payload.git
import mip
mip.install("github:ttk1/prometheus_remote_write_payload")
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}
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,
)