Skip to content

Commit e8a88d9

Browse files
authored
refactor: remove Prometheus related dependencies from actix instrumentation (#246)
1 parent df088ca commit e8a88d9

File tree

8 files changed

+98
-146
lines changed

8 files changed

+98
-146
lines changed

deny.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
[graph]
2-
exclude=[
3-
"protobuf" # opentelemetry-instrumentation-actix-web depends on prometheus should try to remove it
4-
]
2+
exclude=[]
53

64
[licenses]
75
allow = [

opentelemetry-instrumentation-actix-web/CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,31 @@
44

55
### Changed
66

7+
* Remove `opentelemetry-prometheus`, `opentelemetry_sdk`, `prometheus` and `tracing` dependencies
78
* **Breaking** Rename crate to `opentelemetry-instrumentation-actix-web`
9+
* **Breaking** Remove `metrics-prometheus` feature and use `metric` feature instead
10+
* **Breaking** Remove Prometheus middleware `PrometheusMetricsHandler` and use OTLP exporter instead
11+
```rust
12+
// Initialize OTLP exporter using HTTP binary protocol
13+
let exporter = opentelemetry_otlp::MetricExporter::builder()
14+
.with_http()
15+
.with_protocol(Protocol::HttpBinary)
16+
.with_endpoint("http://localhost:9090/api/v1/otlp/v1/metrics")
17+
.build()?;
18+
19+
// set up your meter provider with your exporter(s)
20+
let provider = SdkMeterProvider::builder()
21+
.with_periodic_exporter(exporter)
22+
.with_resource(
23+
// recommended attributes
24+
Resource::builder_empty()
25+
.with_attribute(KeyValue::new("service.name", "my_app"))
26+
.with_attribute(KeyValue::new("service.instance.id", Uuid::new_v4().to_string()))
27+
.build(),
28+
)
29+
.build();
30+
global::set_meter_provider(provider.clone());
31+
```
832

933
## [v0.22.0](https://github.com/OutThereLabs/actix-web-opentelemetry/compare/v0.22.0..v0.21.0)
1034

opentelemetry-instrumentation-actix-web/Cargo.toml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@ homepage = "https://github.com/open-telemetry/opentelemetry-rust-contrib/tree/ma
66
repository = "https://github.com/open-telemetry/opentelemetry-rust-contrib/tree/main/actix-web-opentelemetry"
77
readme = "README.md"
88
categories = ["api-bindings"]
9-
keywords = ["actix", "actix-web", "opentelemetry", "prometheus"]
9+
keywords = ["actix", "actix-web", "opentelemetry"]
1010
license = "Apache-2.0"
1111
edition = "2021"
1212
rust-version = "1.75.0"
1313

1414
[features]
1515
metrics = ["opentelemetry/metrics"]
16-
metrics-prometheus = [
17-
"metrics",
18-
"opentelemetry-prometheus",
19-
"prometheus",
20-
"dep:opentelemetry_sdk",
21-
"dep:tracing",
22-
]
2316
sync-middleware = []
2417

2518
[dependencies]
@@ -36,22 +29,15 @@ futures-util = { version = "0.3", default-features = false, features = [
3629
"alloc",
3730
] }
3831
opentelemetry = { workspace = true, features = ["trace"] }
39-
opentelemetry-prometheus = { version = "0.29", optional = true }
4032
opentelemetry-semantic-conventions = { workspace = true, features = [
4133
"semconv_experimental",
4234
] }
43-
opentelemetry_sdk = { workspace = true, optional = true, features = [
44-
"metrics",
45-
"rt-tokio-current-thread",
46-
] }
47-
prometheus = { version = "0.13", default-features = false, optional = true }
4835
serde = "1.0"
49-
tracing = { version = "0.1.41", optional = true }
5036

5137
[dev-dependencies]
5238
actix-web = { version = "4.0", features = ["macros"] }
5339
opentelemetry-instrumentation-actix-web = { path = ".", features = [
54-
"metrics-prometheus",
40+
"metrics",
5541
"sync-middleware",
5642
"awc",
5743
] }
@@ -60,7 +46,6 @@ opentelemetry_sdk = { workspace = true, features = [
6046
"metrics",
6147
"rt-tokio-current-thread",
6248
] }
63-
opentelemetry-otlp = { version = "0.29", features = ["grpc-tonic"] }
6449
opentelemetry-stdout = { workspace = true, features = ["trace", "metrics"] }
6550

6651
[package.metadata.docs.rs]

opentelemetry-instrumentation-actix-web/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,4 @@ $ firefox http://localhost:16686/
5252

5353
- `awc` -- enable support for tracing the `awc` http client.
5454
- `metrics` -- enable support for opentelemetry metrics (only traces are enabled by default)
55-
- `metrics-prometheus` -- enable support for prometheus metrics (requires `metrics` feature)
5655
- `sync-middleware` -- enable tracing on actix-web middlewares that do synchronous work before returning a future. Adds a small amount of overhead to every request.

opentelemetry-instrumentation-actix-web/examples/client.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use opentelemetry_instrumentation_actix_web::ClientExt;
33
use opentelemetry_sdk::propagation::TraceContextPropagator;
44
use opentelemetry_sdk::trace::SdkTracerProvider;
55
use opentelemetry_sdk::Resource;
6+
use opentelemetry_stdout::SpanExporter;
67
use std::error::Error;
78
use std::io;
89

@@ -34,11 +35,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
3435
.build();
3536

3637
let tracer = SdkTracerProvider::builder()
37-
.with_batch_exporter(
38-
opentelemetry_otlp::SpanExporter::builder()
39-
.with_tonic()
40-
.build()?,
41-
)
38+
.with_batch_exporter(SpanExporter::default())
4239
.with_resource(service_name_resource)
4340
.build();
4441

opentelemetry-instrumentation-actix-web/examples/server.rs

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
use actix_web::{web, App, HttpRequest, HttpServer};
1+
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
22
use opentelemetry::{global, KeyValue};
3-
use opentelemetry_instrumentation_actix_web::{
4-
PrometheusMetricsHandler, RequestMetrics, RequestTracing,
5-
};
6-
use opentelemetry_otlp::WithExportConfig;
3+
use opentelemetry_instrumentation_actix_web::{RequestMetrics, RequestTracing};
74
use opentelemetry_sdk::{
85
metrics::{Aggregation, Instrument, SdkMeterProvider, Stream},
96
propagation::TraceContextPropagator,
107
trace::SdkTracerProvider,
118
Resource,
129
};
10+
use opentelemetry_stdout::{MetricExporter, SpanExporter};
1311

14-
async fn index(_req: HttpRequest, _path: actix_web::web::Path<String>) -> &'static str {
15-
"Hello world!"
12+
async fn manual_hello() -> impl Responder {
13+
HttpResponse::Ok().body("Hey there!")
1614
}
1715

1816
#[actix_web::main]
@@ -25,27 +23,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2523
.build();
2624

2725
let tracer = SdkTracerProvider::builder()
28-
.with_batch_exporter(
29-
opentelemetry_otlp::SpanExporter::builder()
30-
.with_tonic()
31-
.with_endpoint("http://127.0.0.1:6565")
32-
.build()?,
33-
)
26+
.with_simple_exporter(SpanExporter::default())
3427
.with_resource(service_name_resource)
3528
.build();
3629

3730
global::set_tracer_provider(tracer.clone());
3831

39-
// Start a new prometheus metrics pipeline if --features metrics-prometheus is used
40-
#[cfg(feature = "metrics-prometheus")]
41-
let (metrics_handler, meter_provider) = {
42-
let registry = prometheus::Registry::new();
43-
let exporter = opentelemetry_prometheus::exporter()
44-
.with_registry(registry.clone())
45-
.build()?;
46-
32+
// Setup a OTLP metrics exporter if --features metrics is used
33+
#[cfg(feature = "metrics")]
34+
let meter_provider = {
4735
let provider = SdkMeterProvider::builder()
48-
.with_reader(exporter)
36+
.with_periodic_exporter(MetricExporter::default())
4937
.with_resource(
5038
Resource::builder_empty()
5139
.with_attribute(KeyValue::new("service.name", "my_app"))
@@ -67,19 +55,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6755
.build();
6856
global::set_meter_provider(provider.clone());
6957

70-
(PrometheusMetricsHandler::new(registry), provider)
58+
provider
7159
};
7260

7361
HttpServer::new(move || {
74-
let app = App::new()
62+
App::new()
7563
.wrap(RequestTracing::new())
7664
.wrap(RequestMetrics::default())
77-
.service(web::resource("/users/{id}").to(index));
78-
79-
#[cfg(feature = "metrics-prometheus")]
80-
let app = app.route("/metrics", web::get().to(metrics_handler.clone()));
81-
82-
app
65+
.route("/hey", web::get().to(manual_hello))
8366
})
8467
.bind("127.0.0.1:8080")?
8568
.run()
@@ -88,7 +71,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
8871
// Ensure all spans have been reported
8972
tracer.shutdown()?;
9073

91-
#[cfg(feature = "metrics-prometheus")]
74+
#[cfg(feature = "metrics")]
9275
meter_provider.shutdown()?;
9376

9477
Ok(())

opentelemetry-instrumentation-actix-web/src/lib.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
//!
99
//! * Client requests can be traced by using the [`ClientExt::trace_request`] method.
1010
//!
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].
1213
//!
1314
//! * Metrics can be tracked using the [`RequestMetrics`] middleware.
1415
//!
@@ -59,9 +60,8 @@
5960
//!
6061
//! #[actix_web::main]
6162
//! 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.
6565
//! let exporter = opentelemetry_stdout::SpanExporter::default();
6666
//!
6767
//! // Configure your tracer provider with your exporter(s)
@@ -86,43 +86,55 @@
8686
//!
8787
//! ```no_run
8888
//! 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+
//! }
9397
//!
94-
//! # #[cfg(feature = "metrics-prometheus")]
98+
//! # #[cfg(feature = "metrics")]
9599
//! #[actix_web::main]
96100
//! 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();
102104
//!
103105
//! // set up your meter provider with your exporter(s)
104106
//! 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+
//! )
106113
//! .build();
107-
//! global::set_meter_provider(provider);
114+
//! global::set_meter_provider(provider.clone());
108115
//!
109116
//! // Run actix server, metrics are now available at http://localhost:8080/metrics
110117
//! HttpServer::new(move || {
111118
//! App::new()
112119
//! .wrap(RequestTracing::new())
113120
//! .wrap(RequestMetrics::default())
114-
//! .route("/metrics", web::get().to(PrometheusMetricsHandler::new(registry.clone())))
121+
//! .service(web::resource("/").to(index))
115122
//! })
116123
//! .bind("localhost:8080")?
117124
//! .run()
118125
//! .await;
119126
//!
127+
//! //Shutdown the meter provider. This will trigger an export of all metrics.
128+
//! provider.shutdown()?;
129+
//!
120130
//! Ok(())
121131
//! }
122-
//! # #[cfg(not(feature = "metrics-prometheus"))]
132+
//! # #[cfg(not(feature = "metrics"))]
123133
//! # fn main() {}
124134
//! ```
125135
//!
136+
//! For more information on how to configure Prometheus with [OTLP](https://prometheus.io/docs/guides/opentelemetry)
137+
//!
126138
//! ### Exporter configuration
127139
//!
128140
//! [`actix-web`] uses [`tokio`] as the underlying executor, so exporters should be
@@ -153,9 +165,6 @@ mod util;
153165
#[cfg_attr(docsrs, doc(cfg(feature = "awc")))]
154166
pub use client::{ClientExt, InstrumentedClientRequest};
155167

156-
#[cfg(feature = "metrics-prometheus")]
157-
#[cfg_attr(docsrs, doc(cfg(feature = "metrics-prometheus")))]
158-
pub use middleware::metrics::prometheus::PrometheusMetricsHandler;
159168
#[cfg(feature = "metrics")]
160169
#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
161170
pub use middleware::metrics::{RequestMetrics, RequestMetricsBuilder, RequestMetricsMiddleware};

0 commit comments

Comments
 (0)