Skip to content

Commit 8dd7d69

Browse files
committed
Make cargo test work without metrics feature
1 parent 401daf1 commit 8dd7d69

File tree

2 files changed

+50
-40
lines changed

2 files changed

+50
-40
lines changed

docs/releasing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ In order to release a new version, follow these steps.
1010

1111
1. Update the [README.md](../README.md) by running `cargo readme -o README.md` in the root of the repository.
1212

13+
TODO: I think this has to change. cargo-readme doesn't work with feature-gated docs, which is nice to have for the metrics feature. I'd like to have a code example for it, which will only compile with the metrics feature. But I also want cargo test to work with just the default features.
14+
15+
Parsing cfg_attr in cargo-readme seems quite hard, though. So maybe the solution is to create a shorter, separately updated readme. Then have an integration test that extracts code samples from the readme and compiles them to ensure they keep working.
16+
17+
1318
1. Commit the changes.
1419

1520
1. Tag the commit.

src/lib.rs

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,45 +72,50 @@
7272
//!
7373
//! Alternatively you can bring any other HTTP client by implementing the `HttpClient` trait.
7474
//!
75-
//! ## Metrics
76-
//!
77-
//! Please note: Metrics are still experimental both in the OpenTelemetry specification as well as
78-
//! Rust implementation.
79-
//!
80-
//! Please note: The metrics export configuration is still a bit rough in this crate. But once
81-
//! configured it should work as expected.
82-
//!
83-
//! This requires the **metrics** feature.
84-
//!
85-
//! ```no_run
86-
//! use opentelemetry::{global, sdk};
87-
//! use std::time::Duration;
88-
//!
89-
//! #[tokio::main]
90-
//! async fn main() {
91-
//! // Setup exporter
92-
//! let instrumentation_key = std::env::var("INSTRUMENTATION_KEY").unwrap();
93-
//! let exporter = opentelemetry_application_insights::Exporter::new(instrumentation_key, ());
94-
//! let controller = sdk::metrics::controllers::push(
95-
//! sdk::metrics::selectors::simple::Selector::Exact,
96-
//! sdk::export::metrics::ExportKindSelector::Stateless,
97-
//! exporter,
98-
//! tokio::spawn,
99-
//! opentelemetry::util::tokio_interval_stream,
100-
//! )
101-
//! .with_period(Duration::from_secs(1))
102-
//! .build();
103-
//! global::set_meter_provider(controller.provider());
104-
//!
105-
//! // Record value
106-
//! let meter = global::meter("example");
107-
//! let value_recorder = meter.f64_value_recorder("pi").init();
108-
//! value_recorder.record(3.14, &[]);
109-
//!
110-
//! // Give exporter some time to export values before exiting
111-
//! tokio::time::sleep(Duration::from_secs(5)).await;
112-
//! }
113-
//! ```
75+
#![cfg_attr(
76+
feature = "metrics",
77+
doc = r#"
78+
## Metrics
79+
80+
Please note: Metrics are still experimental both in the OpenTelemetry specification as well as
81+
Rust implementation.
82+
83+
Please note: The metrics export configuration is still a bit rough in this crate. But once
84+
configured it should work as expected.
85+
86+
This requires the **metrics** feature.
87+
88+
```no_run
89+
use opentelemetry::{global, sdk};
90+
use std::time::Duration;
91+
92+
#[tokio::main]
93+
async fn main() {
94+
// Setup exporter
95+
let instrumentation_key = std::env::var("INSTRUMENTATION_KEY").unwrap();
96+
let exporter = opentelemetry_application_insights::Exporter::new(instrumentation_key, ());
97+
let controller = sdk::metrics::controllers::push(
98+
sdk::metrics::selectors::simple::Selector::Exact,
99+
sdk::export::metrics::ExportKindSelector::Stateless,
100+
exporter,
101+
tokio::spawn,
102+
opentelemetry::util::tokio_interval_stream,
103+
)
104+
.with_period(Duration::from_secs(1))
105+
.build();
106+
global::set_meter_provider(controller.provider());
107+
108+
// Record value
109+
let meter = global::meter("example");
110+
let value_recorder = meter.f64_value_recorder("pi").init();
111+
value_recorder.record(3.14, &[]);
112+
113+
// Give exporter some time to export values before exiting
114+
tokio::time::sleep(Duration::from_secs(5)).await;
115+
}
116+
```
117+
"#
118+
)]
114119
//!
115120
//! # Attribute mapping
116121
//!
@@ -272,7 +277,7 @@ impl<C> PipelineBuilder<C> {
272277
/// [`reqwest`]: https://crates.io/crates/reqwest
273278
///
274279
/// ```no_run
275-
/// # fn main() -> Result<(), Box<std::error::Error + Send + Sync + 'static>> {
280+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
276281
/// let tracer = opentelemetry_application_insights::new_pipeline("...".into())
277282
/// .with_client(reqwest::blocking::Client::new())
278283
/// .with_endpoint("https://westus2-0.in.applicationinsights.azure.com")?

0 commit comments

Comments
 (0)