Protobuf python mimir client
Jump to navigation
Jump to search
#!/bin/bash set -eu mkdir -p prometheus_proto/gogoproto cd prometheus_proto wget https://raw.githubusercontent.com/prometheus/prometheus/main/prompb/remote.proto wget https://raw.githubusercontent.com/prometheus/prometheus/main/prompb/types.proto wget -P gogoproto https://raw.githubusercontent.com/gogo/protobuf/master/gogoproto/gogo.proto sudo apt install protobuf-compiler protoc --proto_path=. --python_out=. gogoproto/gogo.proto protoc --proto_path=. --python_out=. types.proto protoc --proto_path=. --python_out=. remote.proto mv *.py ../ mv gogoproto ../ cd ..
client-send-metric.py
import time import requests import snappy from remote_pb2 import WriteRequest from types_pb2 import TimeSeries from gogoproto.gogo_pb2 import * # from gogo_pb2 import * # Instead of gogoproto.gogo_pb2 MIMIR_URL = "https://mimir.example/api/v1/push" AUTH = ("myuser", "mypass") def create_remote_write_payload(): """ Constructs a Remote Write Protobuf payload for Mimir. """ remote_write = WriteRequest() series = remote_write.timeseries.add() series.labels.add(name="__name__", value="example_metric") series.labels.add(name="job", value="example") # Example job label ts = int(time.time() * 1000) # Convert to milliseconds sample = series.samples.add(value=42, timestamp=ts) return remote_write.SerializeToString() def send_protobuf(): """ Compresses and sends the Protobuf payload to Mimir. """ payload = create_remote_write_payload() compressed_payload = snappy.compress(payload) # Compress with Snappy headers = { "Content-Type": "application/x-protobuf", "Content-Encoding": "snappy", "X-Scope-OrgID": "org1" # Adjust as per your Mimir setup } response = requests.post(MIMIR_URL, data=compressed_payload, headers=headers, auth=AUTH) print(f"Protobuf Response: {response.status_code} - {response.text}") send_protobuf()