|
8 | 8 | //! |
9 | 9 | //! * Client requests can be traced by using the [`ClientExt::trace_request`] method. |
10 | 10 | //! |
11 | | -//! The `metrics` feature allows you to expose request metrics to [Prometheus]. |
| 11 | +//! The `metrics` feature allows you to export request metrics to any OTLP supported |
| 12 | +//! backend like [Prometheus]. |
12 | 13 | //! |
13 | 14 | //! * Metrics can be tracked using the [`RequestMetrics`] middleware. |
14 | 15 | //! |
|
59 | 60 | //! |
60 | 61 | //! #[actix_web::main] |
61 | 62 | //! async fn main() -> std::io::Result<()> { |
62 | | -//! // Install an OpenTelemetry trace pipeline. |
63 | | -//! // Swap for https://docs.rs/opentelemetry-jaeger or other compatible |
64 | | -//! // exporter to send trace information to your collector. |
| 63 | +//! // Swap for `opentelemetry_otlp` or any other compatible |
| 64 | +//! // exporter to send metrics to your collector. |
65 | 65 | //! let exporter = opentelemetry_stdout::SpanExporter::default(); |
66 | 66 | //! |
67 | 67 | //! // Configure your tracer provider with your exporter(s) |
|
86 | 86 | //! |
87 | 87 | //! ```no_run |
88 | 88 | //! use actix_web::{dev, http, web, App, HttpRequest, HttpServer}; |
89 | | -//! use opentelemetry::global; |
90 | | -//! # #[cfg(feature = "metrics-prometheus")] |
91 | | -//! use opentelemetry_instrumentation_actix_web::{PrometheusMetricsHandler, RequestMetrics, RequestTracing}; |
92 | | -//! use opentelemetry_sdk::metrics::SdkMeterProvider; |
| 89 | +//! use opentelemetry::{global, KeyValue}; |
| 90 | +//! # #[cfg(feature = "metrics")] |
| 91 | +//! use opentelemetry_instrumentation_actix_web::{RequestMetrics, RequestTracing}; |
| 92 | +//! use opentelemetry_sdk::{metrics::SdkMeterProvider, Resource}; |
| 93 | +//! |
| 94 | +//! async fn index() -> &'static str { |
| 95 | +//! "Hello world!" |
| 96 | +//! } |
93 | 97 | //! |
94 | | -//! # #[cfg(feature = "metrics-prometheus")] |
| 98 | +//! # #[cfg(feature = "metrics")] |
95 | 99 | //! #[actix_web::main] |
96 | 100 | //! async fn main() -> Result<(), Box<dyn std::error::Error>> { |
97 | | -//! // Configure prometheus or your preferred metrics service |
98 | | -//! let registry = prometheus::Registry::new(); |
99 | | -//! let exporter = opentelemetry_prometheus::exporter() |
100 | | -//! .with_registry(registry.clone()) |
101 | | -//! .build()?; |
| 101 | +//! // Swap for `opentelemetry_otlp` or any other compatible |
| 102 | +//! // exporter to send metrics to your collector. |
| 103 | +//! let exporter = opentelemetry_stdout::MetricExporter::default(); |
102 | 104 | //! |
103 | 105 | //! // set up your meter provider with your exporter(s) |
104 | 106 | //! let provider = SdkMeterProvider::builder() |
105 | | -//! .with_reader(exporter) |
| 107 | +//! .with_periodic_exporter(exporter) |
| 108 | +//! .with_resource( |
| 109 | +//! Resource::builder_empty() |
| 110 | +//! .with_attribute(KeyValue::new("service.name", "my_app")) |
| 111 | +//! .build(), |
| 112 | +//! ) |
106 | 113 | //! .build(); |
107 | | -//! global::set_meter_provider(provider); |
| 114 | +//! global::set_meter_provider(provider.clone()); |
108 | 115 | //! |
109 | 116 | //! // Run actix server, metrics are now available at http://localhost:8080/metrics |
110 | 117 | //! HttpServer::new(move || { |
111 | 118 | //! App::new() |
112 | 119 | //! .wrap(RequestTracing::new()) |
113 | 120 | //! .wrap(RequestMetrics::default()) |
114 | | -//! .route("/metrics", web::get().to(PrometheusMetricsHandler::new(registry.clone()))) |
| 121 | +//! .service(web::resource("/").to(index)) |
115 | 122 | //! }) |
116 | 123 | //! .bind("localhost:8080")? |
117 | 124 | //! .run() |
118 | 125 | //! .await; |
119 | 126 | //! |
| 127 | +//! //Shutdown the meter provider. This will trigger an export of all metrics. |
| 128 | +//! provider.shutdown()?; |
| 129 | +//! |
120 | 130 | //! Ok(()) |
121 | 131 | //! } |
122 | | -//! # #[cfg(not(feature = "metrics-prometheus"))] |
| 132 | +//! # #[cfg(not(feature = "metrics"))] |
123 | 133 | //! # fn main() {} |
124 | 134 | //! ``` |
125 | 135 | //! |
| 136 | +//! For more information on how to configure Prometheus with [OTLP](https://prometheus.io/docs/guides/opentelemetry) |
| 137 | +//! |
126 | 138 | //! ### Exporter configuration |
127 | 139 | //! |
128 | 140 | //! [`actix-web`] uses [`tokio`] as the underlying executor, so exporters should be |
@@ -153,9 +165,6 @@ mod util; |
153 | 165 | #[cfg_attr(docsrs, doc(cfg(feature = "awc")))] |
154 | 166 | pub use client::{ClientExt, InstrumentedClientRequest}; |
155 | 167 |
|
156 | | -#[cfg(feature = "metrics-prometheus")] |
157 | | -#[cfg_attr(docsrs, doc(cfg(feature = "metrics-prometheus")))] |
158 | | -pub use middleware::metrics::prometheus::PrometheusMetricsHandler; |
159 | 168 | #[cfg(feature = "metrics")] |
160 | 169 | #[cfg_attr(docsrs, doc(cfg(feature = "metrics")))] |
161 | 170 | pub use middleware::metrics::{RequestMetrics, RequestMetricsBuilder, RequestMetricsMiddleware}; |
|
0 commit comments