Recommended Approach: OTLP Exporter
For Prometheus integration, use the OTLP exporter instead. Prometheus natively supports the OTLP protocol, providing a more stable, secure, and actively maintained solution.Why OTLP?
- Active maintenance: Part of the OpenTelemetry specification
- Security: No dependency on unmaintained libraries
- Native support: Prometheus 2.47+ has built-in OTLP receiver
- Consistency: Same exporter works with multiple backends
- Future-proof: Official OpenTelemetry standard
Quick Start
1. Run Prometheus with OTLP Support
Create a minimalprometheus.yml configuration:
http://localhost:9090/api/v1/otlp/v1/metrics
2. Configure Your Application
Add dependencies toCargo.toml:
3. Export Metrics to Prometheus
4. Query Metrics in Prometheus
Open the Prometheus web UI athttp://localhost:9090 and query:
Complete Example with Multiple Metric Types
Migration from Old Prometheus Exporter
If you’re migrating from the discontinuedopentelemetry-prometheus crate:
Before (Old Approach)
After (New Approach with OTLP)
Metric Types Mapping
OpenTelemetry metrics map to Prometheus as follows:Naming Conventions
Follow Prometheus naming best practices:- Use snake_case
- Include unit suffix (
_seconds,_bytes,_total) - Be descriptive and specific
- Avoid redundant prefixes
Configuration
Custom Export Interval
By default, metrics are exported every 60 seconds. Customize this:Resource Attributes
Add resource attributes that appear as labels:Production Deployment
Using OpenTelemetry Collector
For production, use the OpenTelemetry Collector as an intermediary:- Buffering and retry logic
- Multiple backend support
- Metric transformation and filtering
- Service discovery integration
Troubleshooting
Metrics Not Appearing
-
Verify Prometheus OTLP receiver is enabled:
-
Check endpoint URL:
- Must include full path:
/api/v1/otlp/v1/metrics - Default:
http://localhost:9090/api/v1/otlp/v1/metrics
- Must include full path:
-
Ensure shutdown is called:
Missing Labels
Ensure attributes are added when recording metrics:Version Compatibility
Prometheus OTLP support requires:- Prometheus 2.47.0 or later
- OpenTelemetry Rust SDK 0.20.0 or later
Legacy Prometheus Exporter (Deprecated)
The oldopentelemetry-prometheus crate provided a pull-based exporter that exposed metrics via an HTTP endpoint. This approach is no longer recommended.
For pull-based metrics, use the OpenTelemetry Collector with the Prometheus exporter as shown in the Production Deployment section.
Next Steps
OTLP Exporter
Learn more about OTLP configuration
Metrics Guide
Comprehensive guide to metrics in Rust