How do you instrument an application with a Prometheus client library? [Intermediate]
Answer
To instrument an application, I add a Prometheus client library, define a small set of well-named counters, gauges, and histograms, expose a /metrics endpoint, and ensure labels are bounded and useful.
Technical explanation
Start with service RED metrics: request count, error count, and request duration histogram.
Add domain metrics only when they drive SLOs, capacity planning, or operational debugging.
Protect cardinality by using route templates, status classes, dependency names, and environment labels rather than IDs.
Hands-on example
Python Flask sketch:
from prometheus_client import Counter, Histogram, generate_latest
REQ = Counter('http_requests_total','Requests',['route','method','status'])
LAT = Histogram('http_request_duration_seconds','Latency',['route'])
@app.get('/metrics')
def metrics(): return generate_latest(), 200, {'Content-Type':'text/plain'}Preparing for an interview?
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Observability interview questions
- What is the Prometheus Operator, and what are ServiceMonitor and PodMonitor? [Intermediate]
- How does Prometheus integrate with Kubernetes service discovery? [Intermediate]
- What is the difference between Thanos and Cortex/Mimir at a high level? [Intermediate]
- How does Prometheus storage (TSDB) work, and what is the retention model? [Intermediate]
- How does Prometheus handle high cardinality, and why is it a problem? [Intermediate]
- What is the Splunk data pipeline (input, parsing, indexing, search)? [Intermediate]
- What is the difference between a Splunk forwarder, indexer, and search head? [Intermediate]
- What is an alerting rule, and how does it differ from a recording rule? [Basic]