Skip to content

Commit 766a4c0

Browse files
hawkwkaffarell
authored andcommitted
opentelemetry: feature-flag MetricsLayer (tokio-rs#2234)
In the upstream `opentelemetry` crate, the `trace` and `metrics` features are gated by separate feature flags. This allows users who are only using OpenTelemetry for tracing, or who are only using it for metrics, to pick and choose what they depend on. Currently, the release version of `tracing-opentelemetry` only provides tracing functionality, and therefore, it only depends on `opentelemetry` with the `trace` feature enabled. However, the metrics support added in tokio-rs#2185 adds a dependency on the `opentelemetry/metrics` feature. This is currently always enabled. We should probably follow the same approach as upstream `opentelemetry`, and allow enabling/disabling metrics and tracing separately. This branch adds a `metrics` feature to `tracing-opentelemetry`, and makes the `MetricsLayer` from tokio-rs#2185 gated on the `metrics` feature. This feature flag is on by default, like the upstream `opentelemetry/metrics` feature, but it can be disabled using `default-features = false`. We should probably do something similar for the tracing components of the crate, and make them gated on a `trace` feature flag, but adding a feature flag to released APIs is not semver-compatible, so we should save that until the next breaking release.
1 parent 7647492 commit 766a4c0

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

tracing-opentelemetry/Cargo.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ edition = "2018"
2020
rust-version = "1.46.0"
2121

2222
[features]
23-
default = ["tracing-log"]
23+
default = ["tracing-log", "metrics"]
24+
# Enables support for exporting OpenTelemetry metrics
25+
metrics = ["opentelemetry/metrics"]
2426

2527
[dependencies]
26-
opentelemetry = { version = "0.17.0", default-features = false, features = ["metrics", "trace"] }
28+
opentelemetry = { version = "0.17.0", default-features = false, features = ["trace"] }
2729
tracing = { path = "../tracing", version = "0.1.35", default-features = false, features = ["std"] }
2830
tracing-core = { path = "../tracing-core", version = "0.1.28" }
2931
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3.0", default-features = false, features = ["registry", "std"] }
3032
tracing-log = { path = "../tracing-log", version = "0.1.3", default-features = false, optional = true }
31-
once_cell = "1"
33+
once_cell = "1.13.0"
3234

3335
# Fix minimal-versions
3436
async-trait = { version = "0.1.56", optional = true }
@@ -48,3 +50,7 @@ bench = false
4850
[[bench]]
4951
name = "trace"
5052
harness = false
53+
54+
[package.metadata.docs.rs]
55+
all-features = true
56+
rustdoc-args = ["--cfg", "docsrs"]

tracing-opentelemetry/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ $ firefox http://localhost:16686/
101101

102102
![Jaeger UI](trace.png)
103103

104+
## Feature Flags
105+
106+
- `metrics`: Enables the [`MetricsSubscriber`] type, a [subscriber] that
107+
exports OpenTelemetry metrics from specifically-named events. This enables
108+
the `metrics` feature flag on the `opentelemetry` crate.
109+
104110
## Supported Rust Versions
105111

106112
Tracing Opentelemetry is built against the latest stable release. The minimum

tracing-opentelemetry/src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@
7676
//! });
7777
//! ```
7878
//!
79+
//! ## Feature Flags
80+
//!
81+
//! - `metrics`: Enables the [`MetricsSubscriber`] type, a [subscriber] that
82+
//! exports OpenTelemetry metrics from specifically-named events. This enables
83+
//! the `metrics` feature flag on the `opentelemetry` crate. *Enabled by
84+
//! default*.
85+
//!
7986
//! ## Supported Rust Versions
8087
//!
8188
//! Tracing is built against the latest stable release. The minimum supported
@@ -90,16 +97,26 @@
9097
//! supported compiler version is not considered a semver breaking change as
9198
//! long as doing so complies with this policy.
9299
//!
100+
//! [subscriber]: tracing_subscriber::subscribe
93101
#![deny(unreachable_pub)]
94102
#![cfg_attr(test, deny(warnings))]
95103
#![doc(html_root_url = "https://docs.rs/tracing-opentelemetry/0.17.4")]
96104
#![doc(
97105
html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png",
98106
issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/"
99107
)]
100-
#![cfg_attr(docsrs, deny(rustdoc::broken_intra_doc_links))]
108+
#![cfg_attr(
109+
docsrs,
110+
// Allows displaying cfgs/feature flags in the documentation.
111+
feature(doc_cfg, doc_auto_cfg),
112+
// Allows adding traits to RustDoc's list of "notable traits"
113+
feature(doc_notable_trait),
114+
// Fail the docs build if any intra-docs links are broken
115+
deny(rustdoc::broken_intra_doc_links),
116+
)]
101117

102-
/// Implementation of the trace::Layer trait; publishes OpenTelemetry metrics.
118+
/// Implementation of the trace::Subscriber trait; publishes OpenTelemetry metrics.
119+
#[cfg(feature = "metrics")]
103120
mod metrics;
104121

105122
/// Implementation of the trace::Layer as a source of OpenTelemetry data.
@@ -111,6 +128,7 @@ mod tracer;
111128

112129
pub use layer::{layer, OpenTelemetryLayer};
113130

131+
#[cfg(feature = "metrics")]
114132
pub use metrics::MetricsLayer;
115133
pub use span_ext::OpenTelemetrySpanExt;
116134
pub use tracer::PreSampledTracer;

tracing-opentelemetry/src/metrics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ impl<'a> Visit for MetricVisitor<'a> {
320320
///
321321
/// In the future, this can be improved by associating each `Metric` instance to
322322
/// its callsite, eliminating the need for any maps.
323+
///
324+
#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
323325
pub struct MetricsLayer {
324326
meter: Meter,
325327
instruments: Instruments,

tracing-opentelemetry/tests/metrics_publishing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg(feature = "metrics")]
12
use async_trait::async_trait;
23
use futures_util::{Stream, StreamExt as _};
34
use opentelemetry::{

0 commit comments

Comments
 (0)