prometheus-writer is a python-based implementation of the prometheus remote write development kit.
Install dependent packages, eg;
yum install -y gcc snappy-devel
pip install prometheus-writer
Pull up the prometheus service through the container and enable remote write feature,
yml
global:
scrape_interval: 30s
evaluation_interval: 60s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
labels:
instance: prometheus
run
docker run -d -p 9090:9090 -v /opt/prometheus/prometheus.yml:/prometheus/prometheus.yml --name prometheus prom/prometheus --enable-feature=remote-write-receiver
test code
import time
import random
from prometheus.Writer import Cwriter
def test():
w = Cwriter("http://127.0.0.1:9090/api/v1/write")
while True:
w.write("random_test", {"hello": "world"}, random.uniform(10, 20))
time.sleep(15)
if __name__ == "__main__":
test()
pass
Open the http://127.0.0.1:9090/ from a web browser, filter metric name then show:
If you want to write multiple lines, you can call the writes function.
lines = (
("random_test", {"hello": "world"}, random.uniform(10, 20)),
("random_test", {"hello": "world2"}, random.uniform(10, 20)),
)
w.writes(lines)
time.sleep(15)