|
103 | 103 | //! After running your application configured with the OTLP exporter, view traces at:
|
104 | 104 | //! `http://localhost:16686`
|
105 | 105 | //!
|
106 |
| -//! ## Using with Prometheus (TODO) |
| 106 | +//! ## Using with Prometheus |
| 107 | +//! |
| 108 | +//! Prometheus natively supports accepting metrics via the OTLP protocol |
| 109 | +//! (HTTP/protobuf). You can [run |
| 110 | +//! Prometheus](https://prometheus.io/docs/prometheus/latest/installation/) with |
| 111 | +//! the following command: |
| 112 | +//! |
| 113 | +//! ```shell |
| 114 | +//! docker run -p 9090:9090 -v ./prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml --web.enable-otlp-receiver |
| 115 | +//! ``` |
| 116 | +//! |
| 117 | +//! (An empty prometheus.yml file is sufficient for this example.) |
| 118 | +//! |
| 119 | +//! Modify your application to export metrics via OTLP: |
| 120 | +//! |
| 121 | +//! ```no_run |
| 122 | +//! # #[cfg(all(feature = "metrics", feature = "http-proto"))] |
| 123 | +//! # { |
| 124 | +//! use opentelemetry::global; |
| 125 | +//! use opentelemetry::metrics::Meter; |
| 126 | +//! use opentelemetry::KeyValue; |
| 127 | +//! use opentelemetry_otlp::Protocol; |
| 128 | +//! use opentelemetry_otlp::WithExportConfig; |
| 129 | +//! |
| 130 | +//! fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { |
| 131 | +//! // Initialize OTLP exporter using HTTP binary protocol |
| 132 | +//! let exporter = opentelemetry_otlp::MetricExporter::builder() |
| 133 | +//! .with_http() |
| 134 | +//! .with_protocol(Protocol::HttpBinary) |
| 135 | +//! .with_endpoint("http://localhost:9090/api/v1/otlp/v1/metrics") |
| 136 | +//! .build()?; |
| 137 | +//! |
| 138 | +//! // Create a meter provider with the OTLP Metric exporter |
| 139 | +//! let meter_provider = opentelemetry_sdk::metrics::SdkMeterProvider::builder() |
| 140 | +//! .with_periodic_exporter(exporter) |
| 141 | +//! .build(); |
| 142 | +//! global::set_meter_provider(meter_provider.clone()); |
| 143 | +//! |
| 144 | +//! // Get a meter |
| 145 | +//! let meter = global::meter("my_meter"); |
| 146 | +//! |
| 147 | +//! // Create a metric |
| 148 | +//! let counter = meter.u64_counter("my_counter").build(); |
| 149 | +//! counter.add(1, &[KeyValue::new("key", "value")]); |
| 150 | +//! |
| 151 | +//! // Shutdown the meter provider. This will trigger an export of all metrics. |
| 152 | +//! meter_provider.shutdown()?; |
| 153 | +//! |
| 154 | +//! Ok(()) |
| 155 | +//! # } |
| 156 | +//! } |
| 157 | +//! ``` |
| 158 | +//! |
| 159 | +//! After running your application configured with the OTLP exporter, view metrics at: |
| 160 | +//! `http://localhost:9090` |
107 | 161 | //! ## Show Logs, Metrics too (TODO)
|
108 | 162 | //!
|
109 | 163 | //! ## Performance
|
|
0 commit comments