Expose your own application’s metrics. Add a client library, publish a /metrics endpoint, and record custom counters and histograms Prometheus can scrape.
Why: exporters give you infrastructure metrics, but only your app knows its own business and request metrics — orders placed, requests by endpoint, handler latency. A Prometheus client library lets your code record these and expose them at /metrics, where Prometheus scrapes them like any other target.
your app code ──records──▶ client library ──exposes──▶ /metrics ──▶ Prometheus
(counter.inc(), histogram.observe()) (HTTP endpoint) (scrapes it)Why: the client library does the hard part — it keeps the metric values and renders them in the Prometheus text format. Here a Python app starts a metrics server on :8000. Any language has an equivalent library; the shape is always: import, define metrics, start the endpoint.
# pip install prometheus-client
from prometheus_client import start_http_server, Counter, Histogram
import time, random
# Define metrics once, at module load
REQUESTS = Counter("app_requests_total", "Total requests", ["endpoint"])
LATENCY = Histogram("app_request_seconds", "Request latency")
if __name__ == "__main__":
start_http_server(8000) # exposes /metrics on :8000
while True:
time.sleep(1)Why: instrumenting is two steps — define the metric (above), then update it where the work happens. Increment a counter per event; time a block with a histogram. Labels (like endpoint) let one metric track many dimensions. Keep label values bounded — never use unbounded values like user IDs.
def handle(endpoint):
REQUESTS.labels(endpoint=endpoint).inc() # count this request
with LATENCY.time(): # time the work
time.sleep(random.random() * 0.3)
return "ok"Why: the final step is the same as any exporter — add a scrape job for your app and reload. Now your custom app_requests_total and app_request_seconds metrics are queryable in PromQL and chartable in Grafana, right alongside the node metrics.
# add to prometheus.yml under scrape_configs:
- job_name: myapp
static_configs:
- targets: ["host.docker.internal:8000"] # your app's /metrics