Create custom Prometheus exporters and metrics
✓Works with OpenClaudeYou are a monitoring engineer specializing in Prometheus instrumentation. The user wants to create custom Prometheus exporters and expose application metrics.
What to check first
- Run
prometheus --versionto confirm Prometheus is installed locally - Verify the target application or service architecture (what needs metrics exposure)
- Check if the application already has a
/metricsendpoint or if you're building from scratch
Steps
- Choose your Prometheus client library based on language:
prometheus_clientfor Python,prom/client_golangfor Go, ormicrometer-corefor Java - Import the client library and initialize a registry:
CollectorRegistry()in Python orregistry.DefaultRegisterersin Go - Define Counter, Gauge, Histogram, or Summary metrics with appropriate labels using
Counter(),Gauge(),Histogram(),Summary() - Instrument your code by calling
metric.inc(),metric.set(),metric.observe()at relevant execution points - Expose metrics via HTTP endpoint: use
start_http_server()on port 8000+ or register a/metricshandler in your web framework - Test the exporter locally by curling
http://localhost:8000/metricsand verify metric format matches Prometheus text exposition format - Configure Prometheus
scrape_configsinprometheus.ymlto pull from your exporter's endpoint with ajob_nameandtargets - Validate metric names follow Prometheus naming conventions: lowercase, snake_case, with
_totalsuffix for counters and_secondsfor durations
Code
from prometheus_client import Counter, Gauge, Histogram, Summary, CollectorRegistry, start_http_server
import time
# Create custom registry
registry = CollectorRegistry()
# Define metrics with labels
request_counter = Counter(
'http_requests_total',
'Total HTTP requests',
['method', 'endpoint', 'status'],
registry=registry
)
request_duration_histogram = Histogram(
'http_request_duration_seconds',
'HTTP request latency',
['method', 'endpoint'],
buckets=(0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0),
registry=registry
)
active_connections_gauge = Gauge(
'active_connections',
'Number of active connections',
registry=registry
)
request_size_summary = Summary(
'http_request_size_bytes',
'HTTP request payload size',
['method'],
registry=registry
)
# Simulate instrumentation
def handle_request(method, endpoint, status_code, duration_seconds, payload_size):
request_counter.labels(method=method, endpoint=endpoint, status=status_code).inc()
request_duration_histogram.labels(method=method, endpoint=endpoint).observe
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Monitoring & Logging Skills
Other Claude Code skills in the same category — free to download.
Structured Logging
Implement structured logging (Winston, Pino)
Error Tracking
Set up error tracking (Sentry)
APM Setup
Set up Application Performance Monitoring
Log Rotation
Configure log rotation and management
Health Dashboard
Create health monitoring dashboard
Alert Rules
Configure alerting rules and notifications
Distributed Tracing
Set up distributed tracing
Metrics Collector
Implement custom metrics collection
Want a Monitoring & Logging skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.