Skip to content

Instantly share code, notes, and snippets.

@prathamesh-sonpatki
Created December 22, 2022 11:41
Show Gist options
  • Save prathamesh-sonpatki/c8050d6e8636ee81c91b5e63cae5f9c2 to your computer and use it in GitHub Desktop.
Save prathamesh-sonpatki/c8050d6e8636ee81c91b5e63cae5f9c2 to your computer and use it in GitHub Desktop.
Custom httpd exporter
import requests
import time
from prometheus_client import start_http_server
from prometheus_client.core import Gauge, GaugeMetricFamily, CounterMetricFamily, REGISTRY
APP_URL = "http://localhost:80/server-status/?auto"
def get_metrics():
resp = requests.get(url=APP_URL)
byte_data = resp.content
data = str(byte_data, 'UTF-8')
lines = data.split('\n')
return lines
def split_pair(pair=""):
key_and_value = pair.split(':')
value = key_and_value[1].strip()
return float(value)
gauge = Gauge('gauge_name', 'gauge description')
class CustomCollector(object):
def __init__(self):
pass
def collect(self):
lines = get_metrics()
server_threads = GaugeMetricFamily('httpd_server_threads', 'Number of workers available on the application server', labels=['worker_state'])
for i in lines:
if "ServerUptimeSeconds" in i:
v = split_pair(i)
yield CounterMetricFamily('httpd_server_uptime', 'How long the application server has been up', value=v)
elif "CPULoad" in i:
v = split_pair(i)
yield GaugeMetricFamily('httpd_server_CPU_load', 'How many requests per second the server is processing', value=v)
elif "BusyWorkers" in i:
v = split_pair(i)
server_threads.add_metric(['busy'], v)
elif "IdleWorkers" in i:
v = split_pair(i)
server_threads.add_metric(['idle'], v)
yield server_threads
# code to get gauge value here
# gauge.set(gauge_value)
if __name__ == "__main__":
start_http_server(8080)
REGISTRY.register(CustomCollector())
while True:
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment