Skip to content

Commit

Permalink
Make PeriodicReader with own Thread the default (#2403)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Dec 11, 2024
1 parent d67d1fc commit 238a8f2
Show file tree
Hide file tree
Showing 15 changed files with 1,167 additions and 1,114 deletions.
4 changes: 2 additions & 2 deletions examples/metrics-advanced/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use opentelemetry::KeyValue;
use opentelemetry_sdk::metrics::{
Aggregation, Instrument, PeriodicReader, SdkMeterProvider, Stream, Temporality,
};
use opentelemetry_sdk::{runtime, Resource};
use opentelemetry_sdk::Resource;
use std::error::Error;

fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
Expand Down Expand Up @@ -49,7 +49,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
.with_temporality(Temporality::Delta)
.build();

let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter).build();
let provider = SdkMeterProvider::builder()
.with_reader(reader)
.with_resource(Resource::new([KeyValue::new(
Expand Down
4 changes: 2 additions & 2 deletions examples/metrics-basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use opentelemetry::{global, KeyValue};
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
use opentelemetry_sdk::{runtime, Resource};
use opentelemetry_sdk::Resource;
use std::error::Error;
use std::vec;

Expand All @@ -9,7 +9,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
// Build exporter using Delta Temporality (Defaults to Temporality::Cumulative)
// .with_temporality(opentelemetry_sdk::metrics::Temporality::Delta)
.build();
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter).build();
let provider = SdkMeterProvider::builder()
.with_reader(reader)
.with_resource(Resource::new([KeyValue::new(
Expand Down
3 changes: 2 additions & 1 deletion opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ publish = false
default = ["reqwest"]
reqwest = ["opentelemetry-otlp/reqwest-client"]
hyper = ["opentelemetry-otlp/hyper-client"]
experimental_metrics_periodicreader_with_async_runtime = ["opentelemetry_sdk/experimental_metrics_periodicreader_with_async_runtime"]


[dependencies]
once_cell = { workspace = true }
opentelemetry = { path = "../../../opentelemetry" }
opentelemetry_sdk = { path = "../../../opentelemetry-sdk", features = ["rt-tokio", "metrics", "logs"]}
opentelemetry_sdk = { path = "../../../opentelemetry-sdk", features = ["rt-tokio", "metrics", "logs", "experimental_metrics_periodicreader_with_async_runtime"]}
opentelemetry-http = { path = "../../../opentelemetry-http", optional = true, default-features = false}
opentelemetry-otlp = { path = "../..", features = ["http-proto", "http-json", "logs"] , default-features = false}
opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing", default-features = false}
Expand Down
15 changes: 13 additions & 2 deletions opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use opentelemetry_otlp::WithExportConfig;
use opentelemetry_otlp::{LogExporter, MetricExporter, Protocol, SpanExporter};
use opentelemetry_sdk::{
logs::LoggerProvider,
metrics::{MetricError, PeriodicReader, SdkMeterProvider},
metrics::{MetricError, SdkMeterProvider},
runtime,
trace::{self as sdktrace, TracerProvider},
};
Expand Down Expand Up @@ -63,8 +63,19 @@ fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, Metric
.with_endpoint("http://localhost:4318/v1/metrics")
.build()?;

#[cfg(feature = "experimental_metrics_periodicreader_with_async_runtime")]
let reader =
opentelemetry_sdk::metrics::periodic_reader_with_async_runtime::PeriodicReader::builder(
exporter,
runtime::Tokio,
)
.build();
// TODO: This does not work today. See https://github.com/open-telemetry/opentelemetry-rust/issues/2400
#[cfg(not(feature = "experimental_metrics_periodicreader_with_async_runtime"))]
let reader = opentelemetry_sdk::metrics::PeriodicReader::builder(exporter).build();

Ok(SdkMeterProvider::builder()
.with_reader(PeriodicReader::builder(exporter, runtime::Tokio).build())
.with_reader(reader)
.with_resource(RESOURCE.clone())
.build())
}
Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-otlp/examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ fn init_traces() -> Result<sdktrace::TracerProvider, TraceError> {

fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricError> {
let exporter = MetricExporter::builder().with_tonic().build()?;

let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter).build();

Ok(SdkMeterProvider::builder()
.with_reader(reader)
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-otlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
//! .build()
//! .unwrap();
//!
//! let reader = opentelemetry_sdk::metrics::PeriodicReader::builder(exporter, opentelemetry_sdk::runtime::Tokio)
//! let reader = opentelemetry_sdk::metrics::PeriodicReader::builder(exporter)
//! .with_interval(std::time::Duration::from_secs(3))
//! .with_timeout(Duration::from_secs(10))
//! .build();
Expand Down
46 changes: 46 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,52 @@
Migration Guidance:
- These methods were intended for log appenders. Keep the clone of the provider handle, instead of depending on above methods.

- *Breaking* - `PeriodicReader` Updates

`PeriodicReader` no longer requires an async runtime by default. Instead, it
now creates its own background thread for execution. This change allows
metrics to be used in environments without async runtimes.

For users who prefer the previous behavior of relying on a specific
`Runtime`, they can do so by enabling the feature flag
**`experimental_metrics_periodicreader_with_async_runtime`**.

Migration Guide:

1. *Default Implementation, requires no async runtime* (**Recommended**) The
new default implementation does not require a runtime argument. Replace the
builder method accordingly:
- *Before:*
```rust
let reader = opentelemetry_sdk::metrics::PeriodicReader::builder(exporter, runtime::Tokio).build();
```
- *After:*
```rust
let reader = opentelemetry_sdk::metrics::PeriodicReader::builder(exporter).build();
```

2. *Async Runtime Support*
If your application cannot spin up new threads or you prefer using async
runtimes, enable the
"experimental_metrics_periodicreader_with_async_runtime" feature flag and
adjust code as below.

- *Before:*
```rust
let reader = opentelemetry_sdk::metrics::PeriodicReader::builder(exporter, runtime::Tokio).build();
```

- *After:*
```rust
let reader = opentelemetry_sdk::metrics::periodic_reader_with_async_runtime::PeriodicReader::builder(exporter, runtime::Tokio).build();
```

*Requirements:*
- Enable the feature flag:
`experimental_metrics_periodicreader_with_async_runtime`.
- Continue enabling one of the async runtime feature flags: `rt-tokio`,
`rt-tokio-current-thread`, or `rt-async-std`.

## 0.27.1

Released 2024-Nov-27
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ rt-tokio-current-thread = ["tokio", "tokio-stream"]
rt-async-std = ["async-std"]
internal-logs = ["tracing"]
experimental_metrics_periodic_reader_no_runtime = ["metrics"]
experimental_metrics_periodicreader_with_async_runtime = ["metrics"]
spec_unstable_metrics_views = ["metrics"]

[[bench]]
Expand Down
25 changes: 12 additions & 13 deletions opentelemetry-sdk/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ pub(crate) mod meter;
mod meter_provider;
pub(crate) mod noop;
pub(crate) mod periodic_reader;
#[cfg(feature = "experimental_metrics_periodic_reader_no_runtime")]
pub(crate) mod periodic_reader_with_own_thread;
#[cfg(feature = "experimental_metrics_periodicreader_with_async_runtime")]
/// Module for periodic reader with async runtime.
pub mod periodic_reader_with_async_runtime;
pub(crate) mod pipeline;
pub mod reader;
pub(crate) mod view;
Expand All @@ -61,8 +62,6 @@ pub use error::{MetricError, MetricResult};
pub use manual_reader::*;
pub use meter_provider::*;
pub use periodic_reader::*;
#[cfg(feature = "experimental_metrics_periodic_reader_no_runtime")]
pub use periodic_reader_with_own_thread::*;
pub use pipeline::Pipeline;

pub use instrument::InstrumentKind;
Expand Down Expand Up @@ -107,8 +106,8 @@ mod tests {
use self::data::{HistogramDataPoint, ScopeMetrics, SumDataPoint};
use super::*;
use crate::metrics::data::ResourceMetrics;
use crate::testing::metrics::InMemoryMetricExporter;
use crate::testing::metrics::InMemoryMetricExporterBuilder;
use crate::{runtime, testing::metrics::InMemoryMetricExporter};
use data::GaugeDataPoint;
use opentelemetry::metrics::{Counter, Meter, UpDownCounter};
use opentelemetry::InstrumentationScope;
Expand Down Expand Up @@ -513,7 +512,7 @@ mod tests {
}

let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter.clone()).build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();

// Test Meter creation in 2 ways, both with empty string as meter name
Expand All @@ -529,7 +528,7 @@ mod tests {
async fn counter_duplicate_instrument_merge() {
// Arrange
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter.clone()).build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();

// Act
Expand Down Expand Up @@ -580,7 +579,7 @@ mod tests {
async fn counter_duplicate_instrument_different_meter_no_merge() {
// Arrange
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter.clone()).build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();

// Act
Expand Down Expand Up @@ -669,7 +668,7 @@ mod tests {
async fn instrumentation_scope_identity_test() {
// Arrange
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter.clone()).build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();

// Act
Expand Down Expand Up @@ -753,7 +752,7 @@ mod tests {

// Arrange
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter.clone()).build();
let criteria = Instrument::new().name("test_histogram");
let stream_invalid_aggregation = Stream::new()
.aggregation(Aggregation::ExplicitBucketHistogram {
Expand Down Expand Up @@ -803,7 +802,7 @@ mod tests {

// Arrange
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter.clone()).build();
let criteria = Instrument::new().name("my_observable_counter");
// View drops all attributes.
let stream_invalid_aggregation = Stream::new().allowed_attribute_keys(vec![]);
Expand Down Expand Up @@ -878,7 +877,7 @@ mod tests {

// Arrange
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter.clone()).build();
let criteria = Instrument::new().name("my_counter");
// View drops all attributes.
let stream_invalid_aggregation = Stream::new().allowed_attribute_keys(vec![]);
Expand Down Expand Up @@ -2433,7 +2432,7 @@ mod tests {
let exporter = InMemoryMetricExporterBuilder::new().with_temporality(temporality);

let exporter = exporter.build();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let reader = PeriodicReader::builder(exporter.clone()).build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();

TestContext {
Expand Down
Loading

0 comments on commit 238a8f2

Please sign in to comment.