Skip to content

Instantly share code, notes, and snippets.

@hiimivantang
Created January 10, 2021 11:58
Show Gist options
  • Save hiimivantang/6b7fde339f45f820d9fe305575e16637 to your computer and use it in GitHub Desktop.
Save hiimivantang/6b7fde339f45f820d9fe305575e16637 to your computer and use it in GitHub Desktop.
Sidecar to expose librdkafka statistics in Prometheus exposition format.
from prometheus_client import start_http_server, Metric, REGISTRY
import json
import requests
import sys
import time
class JsonCollector(object):
def collect(self):
# Fetch the JSON
#response = json.loads(requests.get(self._endpoint).content.decode('UTF-8'))
#
# Convert requests and duration to a summary in seconds
response = json.load(open('python_consumer_stats.json','r'))
print(response)
client_id = response['client_id']
client_type = response['type']
metric = Metric('librdkafka_metrics','librdkafka client metrics', 'summary')
metric.add_sample('rx_bytes',value=response['rx_bytes'],labels=dict(client_id=client_id,client_type=client_type))
#TODO: add more librdkafka metrics
yield metric
if __name__ == '__main__':
# Usage: json_exporter.py port endpoint
start_http_server(int(sys.argv[1]))
REGISTRY.register(JsonCollector())
while True: time.sleep(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment