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