From 5d5296b138b5d440aed82d0f0f702f95761c7b54 Mon Sep 17 00:00:00 2001 From: Shaun Cox Date: Wed, 8 Nov 2023 10:12:26 -0600 Subject: [PATCH] More distinct naming of metrics constructs in the SDK (#1328) --- examples/metrics-advanced/src/main.rs | 8 ++- examples/metrics-basic/src/main.rs | 6 +- .../examples/basic-otlp-http/src/main.rs | 2 +- .../examples/basic-otlp/src/main.rs | 4 +- opentelemetry-otlp/src/metric.rs | 6 +- opentelemetry-prometheus/examples/hyper.rs | 4 +- opentelemetry-prometheus/src/lib.rs | 6 +- .../tests/integration_test.rs | 8 +-- opentelemetry-sdk/CHANGELOG.md | 1 + opentelemetry-sdk/benches/metric.rs | 8 +-- opentelemetry-sdk/src/metrics/instrument.rs | 8 +-- opentelemetry-sdk/src/metrics/meter.rs | 60 +++++++++---------- .../src/metrics/meter_provider.rs | 51 +++++++++------- opentelemetry-sdk/src/metrics/mod.rs | 8 +-- opentelemetry-sdk/src/metrics/pipeline.rs | 2 +- opentelemetry-sdk/src/metrics/view.rs | 4 +- .../src/testing/metrics/in_memory_exporter.rs | 2 +- opentelemetry-stdout/examples/basic.rs | 6 +- opentelemetry-stdout/src/lib.rs | 8 +-- .../examples/basic.rs | 6 +- stress/src/metrics.rs | 4 +- 21 files changed, 111 insertions(+), 101 deletions(-) diff --git a/examples/metrics-advanced/src/main.rs b/examples/metrics-advanced/src/main.rs index 5716009cd7..760ed1b7e1 100644 --- a/examples/metrics-advanced/src/main.rs +++ b/examples/metrics-advanced/src/main.rs @@ -1,11 +1,13 @@ use opentelemetry::metrics::Unit; use opentelemetry::Key; use opentelemetry::{metrics::MeterProvider as _, KeyValue}; -use opentelemetry_sdk::metrics::{Aggregation, Instrument, MeterProvider, PeriodicReader, Stream}; +use opentelemetry_sdk::metrics::{ + Aggregation, Instrument, PeriodicReader, SdkMeterProvider, Stream, +}; use opentelemetry_sdk::{runtime, Resource}; use std::error::Error; -fn init_meter_provider() -> MeterProvider { +fn init_meter_provider() -> SdkMeterProvider { // for example 1 let my_view_rename_and_unit = |i: &Instrument| { if i.name == "my_histogram" { @@ -48,7 +50,7 @@ fn init_meter_provider() -> MeterProvider { // Ok(serde_json::to_writer_pretty(writer, &data).unwrap())) .build(); let reader = PeriodicReader::builder(exporter, runtime::Tokio).build(); - MeterProvider::builder() + SdkMeterProvider::builder() .with_reader(reader) .with_resource(Resource::new(vec![KeyValue::new( "service.name", diff --git a/examples/metrics-basic/src/main.rs b/examples/metrics-basic/src/main.rs index c4bbe71d46..c3307d1f3a 100644 --- a/examples/metrics-basic/src/main.rs +++ b/examples/metrics-basic/src/main.rs @@ -1,17 +1,17 @@ use opentelemetry::metrics::Unit; use opentelemetry::{metrics::MeterProvider as _, KeyValue}; -use opentelemetry_sdk::metrics::{MeterProvider, PeriodicReader}; +use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider}; use opentelemetry_sdk::{runtime, Resource}; use std::error::Error; -fn init_meter_provider() -> MeterProvider { +fn init_meter_provider() -> SdkMeterProvider { let exporter = opentelemetry_stdout::MetricsExporterBuilder::default() // uncomment the below lines to pretty print output. // .with_encoder(|writer, data| // Ok(serde_json::to_writer_pretty(writer, &data).unwrap())) .build(); let reader = PeriodicReader::builder(exporter, runtime::Tokio).build(); - MeterProvider::builder() + SdkMeterProvider::builder() .with_reader(reader) .with_resource(Resource::new(vec![KeyValue::new( "service.name", diff --git a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs index 893788475c..36b4fdae1f 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs @@ -44,7 +44,7 @@ fn init_tracer() -> Result { .install_batch(opentelemetry_sdk::runtime::Tokio) } -fn init_metrics() -> metrics::Result { +fn init_metrics() -> metrics::Result { let export_config = opentelemetry_otlp::ExportConfig { endpoint: "http://localhost:4318/v1/metrics".to_string(), ..opentelemetry_otlp::ExportConfig::default() diff --git a/opentelemetry-otlp/examples/basic-otlp/src/main.rs b/opentelemetry-otlp/examples/basic-otlp/src/main.rs index 61df37c2ec..7892151176 100644 --- a/opentelemetry-otlp/examples/basic-otlp/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp/src/main.rs @@ -12,7 +12,7 @@ use opentelemetry::{ use opentelemetry_appender_log::OpenTelemetryLogBridge; use opentelemetry_otlp::{ExportConfig, WithExportConfig}; use opentelemetry_sdk::logs::Config; -use opentelemetry_sdk::{metrics::MeterProvider, runtime, trace as sdktrace, Resource}; +use opentelemetry_sdk::{metrics::SdkMeterProvider, runtime, trace as sdktrace, Resource}; use std::error::Error; fn init_tracer() -> Result { @@ -32,7 +32,7 @@ fn init_tracer() -> Result { .install_batch(runtime::Tokio) } -fn init_metrics() -> metrics::Result { +fn init_metrics() -> metrics::Result { let export_config = ExportConfig { endpoint: "http://localhost:4317".to_string(), ..ExportConfig::default() diff --git a/opentelemetry-otlp/src/metric.rs b/opentelemetry-otlp/src/metric.rs index c1148a421b..78155aace1 100644 --- a/opentelemetry-otlp/src/metric.rs +++ b/opentelemetry-otlp/src/metric.rs @@ -18,7 +18,7 @@ use opentelemetry_sdk::{ AggregationSelector, DefaultAggregationSelector, DefaultTemporalitySelector, TemporalitySelector, }, - Aggregation, InstrumentKind, MeterProvider, PeriodicReader, + Aggregation, InstrumentKind, PeriodicReader, SdkMeterProvider, }, runtime::Runtime, Resource, @@ -215,7 +215,7 @@ where RT: Runtime, { /// Build MeterProvider - pub fn build(self) -> Result { + pub fn build(self) -> Result { let exporter = self.exporter_pipeline.build_metrics_exporter( self.temporality_selector .unwrap_or_else(|| Box::new(DefaultTemporalitySelector::new())), @@ -234,7 +234,7 @@ where let reader = builder.build(); - let mut provider = MeterProvider::builder().with_reader(reader); + let mut provider = SdkMeterProvider::builder().with_reader(reader); if let Some(resource) = self.resource { provider = provider.with_resource(resource); diff --git a/opentelemetry-prometheus/examples/hyper.rs b/opentelemetry-prometheus/examples/hyper.rs index 79787af309..943ba617b6 100644 --- a/opentelemetry-prometheus/examples/hyper.rs +++ b/opentelemetry-prometheus/examples/hyper.rs @@ -8,7 +8,7 @@ use opentelemetry::{ metrics::{Counter, Histogram, MeterProvider as _, Unit}, KeyValue, }; -use opentelemetry_sdk::metrics::MeterProvider; +use opentelemetry_sdk::metrics::SdkMeterProvider; use prometheus::{Encoder, Registry, TextEncoder}; use std::convert::Infallible; use std::sync::Arc; @@ -71,7 +71,7 @@ pub async fn main() -> Result<(), Box> { let exporter = opentelemetry_prometheus::exporter() .with_registry(registry.clone()) .build()?; - let provider = MeterProvider::builder().with_reader(exporter).build(); + let provider = SdkMeterProvider::builder().with_reader(exporter).build(); let meter = provider.meter("hyper-example"); let state = Arc::new(AppState { diff --git a/opentelemetry-prometheus/src/lib.rs b/opentelemetry-prometheus/src/lib.rs index dcd008c071..804f69dea6 100644 --- a/opentelemetry-prometheus/src/lib.rs +++ b/opentelemetry-prometheus/src/lib.rs @@ -3,8 +3,8 @@ //! [Prometheus]: https://prometheus.io //! //! ``` -//! use opentelemetry::{metrics::MeterProvider as _, KeyValue}; -//! use opentelemetry_sdk::metrics::MeterProvider; +//! use opentelemetry::{metrics::MeterProvider, KeyValue}; +//! use opentelemetry_sdk::metrics::SdkMeterProvider; //! use prometheus::{Encoder, TextEncoder}; //! //! # fn main() -> Result<(), Box> { @@ -18,7 +18,7 @@ //! .build()?; //! //! // set up a meter meter to create instruments -//! let provider = MeterProvider::builder().with_reader(exporter).build(); +//! let provider = SdkMeterProvider::builder().with_reader(exporter).build(); //! let meter = provider.meter("my-app"); //! //! // Use two instruments diff --git a/opentelemetry-prometheus/tests/integration_test.rs b/opentelemetry-prometheus/tests/integration_test.rs index 95e95c9f16..20dabd4076 100644 --- a/opentelemetry-prometheus/tests/integration_test.rs +++ b/opentelemetry-prometheus/tests/integration_test.rs @@ -6,7 +6,7 @@ use opentelemetry::metrics::{Meter, MeterProvider as _, Unit}; use opentelemetry::Key; use opentelemetry::KeyValue; use opentelemetry_prometheus::ExporterBuilder; -use opentelemetry_sdk::metrics::{new_view, Aggregation, Instrument, MeterProvider, Stream}; +use opentelemetry_sdk::metrics::{new_view, Aggregation, Instrument, SdkMeterProvider, Stream}; use opentelemetry_sdk::resource::{ EnvResourceDetector, SdkProvidedResourceDetector, TelemetryResourceDetector, }; @@ -336,7 +336,7 @@ fn prometheus_exporter_integration() { )) }; - let provider = MeterProvider::builder() + let provider = SdkMeterProvider::builder() .with_resource(res) .with_reader(exporter) .with_view( @@ -395,7 +395,7 @@ fn multiple_scopes() { TELEMETRY_SDK_VERSION.string("latest"), ])); - let provider = MeterProvider::builder() + let provider = SdkMeterProvider::builder() .with_reader(exporter) .with_resource(resource) .build(); @@ -730,7 +730,7 @@ fn duplicate_metrics() { .chain(tc.custom_resource_attrs.into_iter()), )); - let provider = MeterProvider::builder() + let provider = SdkMeterProvider::builder() .with_resource(resource) .with_reader(exporter) .build(); diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index 1fb63971f0..2a6d9efd68 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -30,6 +30,7 @@ ### Changed +- Renamed `MeterProvider` and `Meter` to `SdkMeterProvider` and `SdkMeter` respectively to avoid name collision with public API types. [#1328](https://github.com/open-telemetry/opentelemetry-rust/pull/1328) - Bump MSRV to 1.65 [#1318](https://github.com/open-telemetry/opentelemetry-rust/pull/1318) - Default Resource (the one used when no other Resource is explicitly provided) now includes `TelemetryResourceDetector`, populating "telemetry.sdk.*" attributes. diff --git a/opentelemetry-sdk/benches/metric.rs b/opentelemetry-sdk/benches/metric.rs index 5594bc4d25..052295c279 100644 --- a/opentelemetry-sdk/benches/metric.rs +++ b/opentelemetry-sdk/benches/metric.rs @@ -11,7 +11,7 @@ use opentelemetry_sdk::{ data::{ResourceMetrics, Temporality}, new_view, reader::{AggregationSelector, MetricReader, TemporalitySelector}, - Aggregation, Instrument, InstrumentKind, ManualReader, MeterProvider, Pipeline, Stream, + Aggregation, Instrument, InstrumentKind, ManualReader, Pipeline, SdkMeterProvider, Stream, View, }, Resource, @@ -150,7 +150,7 @@ fn bench_counter(view: Option>, temporality: &str) -> (SharedReade .build(), )) }; - let mut builder = MeterProvider::builder().with_reader(rdr.clone()); + let mut builder = SdkMeterProvider::builder().with_reader(rdr.clone()); if let Some(view) = view { builder = builder.with_view(view); } @@ -367,7 +367,7 @@ fn bench_histogram(bound_count: usize) -> (SharedReader, Histogram) { ); let r = SharedReader(Arc::new(ManualReader::default())); - let mut builder = MeterProvider::builder().with_reader(r.clone()); + let mut builder = SdkMeterProvider::builder().with_reader(r.clone()); if let Some(view) = view { builder = builder.with_view(view); } @@ -408,7 +408,7 @@ fn histograms(c: &mut Criterion) { fn benchmark_collect_histogram(b: &mut Bencher, n: usize) { let r = SharedReader(Arc::new(ManualReader::default())); - let mtr = MeterProvider::builder() + let mtr = SdkMeterProvider::builder() .with_reader(r.clone()) .build() .meter("sdk/metric/bench/histogram"); diff --git a/opentelemetry-sdk/src/metrics/instrument.rs b/opentelemetry-sdk/src/metrics/instrument.rs index 5cc44c6772..7971d47d3c 100644 --- a/opentelemetry-sdk/src/metrics/instrument.rs +++ b/opentelemetry-sdk/src/metrics/instrument.rs @@ -248,11 +248,11 @@ impl InstrumentId { } } -pub(crate) struct InstrumentImpl { +pub(crate) struct ResolvedMeasures { pub(crate) measures: Vec>>, } -impl SyncCounter for InstrumentImpl { +impl SyncCounter for ResolvedMeasures { fn add(&self, val: T, attrs: &[KeyValue]) { for measure in &self.measures { measure.call(val, AttributeSet::from(attrs)) @@ -260,7 +260,7 @@ impl SyncCounter for InstrumentImpl { } } -impl SyncUpDownCounter for InstrumentImpl { +impl SyncUpDownCounter for ResolvedMeasures { fn add(&self, val: T, attrs: &[KeyValue]) { for measure in &self.measures { measure.call(val, AttributeSet::from(attrs)) @@ -268,7 +268,7 @@ impl SyncUpDownCounter for InstrumentImpl { } } -impl SyncHistogram for InstrumentImpl { +impl SyncHistogram for ResolvedMeasures { fn record(&self, val: T, attrs: &[KeyValue]) { for measure in &self.measures { measure.call(val, AttributeSet::from(attrs)) diff --git a/opentelemetry-sdk/src/metrics/meter.rs b/opentelemetry-sdk/src/metrics/meter.rs index 6a7b116863..b025a9152d 100644 --- a/opentelemetry-sdk/src/metrics/meter.rs +++ b/opentelemetry-sdk/src/metrics/meter.rs @@ -15,7 +15,7 @@ use opentelemetry::{ use crate::instrumentation::Scope; use crate::metrics::{ instrument::{ - Instrument, InstrumentImpl, InstrumentKind, Observable, ObservableId, EMPTY_MEASURE_MSG, + Instrument, InstrumentKind, Observable, ObservableId, ResolvedMeasures, EMPTY_MEASURE_MSG, }, internal::{self, Number}, pipeline::{Pipelines, Resolver}, @@ -46,7 +46,7 @@ const INSTRUMENT_UNIT_INVALID_CHAR: &str = "characters in instrument unit must b /// See the [Meter API] docs for usage. /// /// [Meter API]: opentelemetry::metrics::Meter -pub struct Meter { +pub struct SdkMeter { scope: Scope, pipes: Arc, u64_resolver: Resolver, @@ -55,11 +55,11 @@ pub struct Meter { validation_policy: InstrumentValidationPolicy, } -impl Meter { +impl SdkMeter { pub(crate) fn new(scope: Scope, pipes: Arc) -> Self { let view_cache = Default::default(); - Meter { + SdkMeter { scope, pipes: Arc::clone(&pipes), u64_resolver: Resolver::new(Arc::clone(&pipes), Arc::clone(&view_cache)), @@ -79,7 +79,7 @@ impl Meter { } #[doc(hidden)] -impl InstrumentProvider for Meter { +impl InstrumentProvider for SdkMeter { fn u64_counter( &self, name: Cow<'static, str>, @@ -87,7 +87,7 @@ impl InstrumentProvider for Meter { unit: Option, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.u64_resolver); + let p = InstrumentResolver::new(self, &self.u64_resolver); p.lookup( InstrumentKind::Counter, name, @@ -104,7 +104,7 @@ impl InstrumentProvider for Meter { unit: Option, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.f64_resolver); + let p = InstrumentResolver::new(self, &self.f64_resolver); p.lookup( InstrumentKind::Counter, name, @@ -122,7 +122,7 @@ impl InstrumentProvider for Meter { callbacks: Vec>, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.u64_resolver); + let p = InstrumentResolver::new(self, &self.u64_resolver); let ms = p.measures( InstrumentKind::ObservableCounter, name.clone(), @@ -159,7 +159,7 @@ impl InstrumentProvider for Meter { callbacks: Vec>, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.f64_resolver); + let p = InstrumentResolver::new(self, &self.f64_resolver); let ms = p.measures( InstrumentKind::ObservableCounter, name.clone(), @@ -194,7 +194,7 @@ impl InstrumentProvider for Meter { unit: Option, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.i64_resolver); + let p = InstrumentResolver::new(self, &self.i64_resolver); p.lookup( InstrumentKind::UpDownCounter, name, @@ -211,7 +211,7 @@ impl InstrumentProvider for Meter { unit: Option, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.f64_resolver); + let p = InstrumentResolver::new(self, &self.f64_resolver); p.lookup( InstrumentKind::UpDownCounter, name, @@ -229,7 +229,7 @@ impl InstrumentProvider for Meter { callbacks: Vec>, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.i64_resolver); + let p = InstrumentResolver::new(self, &self.i64_resolver); let ms = p.measures( InstrumentKind::ObservableUpDownCounter, name.clone(), @@ -268,7 +268,7 @@ impl InstrumentProvider for Meter { callbacks: Vec>, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.f64_resolver); + let p = InstrumentResolver::new(self, &self.f64_resolver); let ms = p.measures( InstrumentKind::ObservableUpDownCounter, name.clone(), @@ -307,7 +307,7 @@ impl InstrumentProvider for Meter { callbacks: Vec>, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.u64_resolver); + let p = InstrumentResolver::new(self, &self.u64_resolver); let ms = p.measures( InstrumentKind::ObservableGauge, name.clone(), @@ -344,7 +344,7 @@ impl InstrumentProvider for Meter { callbacks: Vec>, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.i64_resolver); + let p = InstrumentResolver::new(self, &self.i64_resolver); let ms = p.measures( InstrumentKind::ObservableGauge, name.clone(), @@ -381,7 +381,7 @@ impl InstrumentProvider for Meter { callbacks: Vec>, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.f64_resolver); + let p = InstrumentResolver::new(self, &self.f64_resolver); let ms = p.measures( InstrumentKind::ObservableGauge, name.clone(), @@ -417,7 +417,7 @@ impl InstrumentProvider for Meter { unit: Option, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.f64_resolver); + let p = InstrumentResolver::new(self, &self.f64_resolver); p.lookup( InstrumentKind::Histogram, name, @@ -434,7 +434,7 @@ impl InstrumentProvider for Meter { unit: Option, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.u64_resolver); + let p = InstrumentResolver::new(self, &self.u64_resolver); p.lookup( InstrumentKind::Histogram, name, @@ -451,7 +451,7 @@ impl InstrumentProvider for Meter { unit: Option, ) -> Result> { validate_instrument_config(name.as_ref(), unit.as_ref(), self.validation_policy)?; - let p = InstProvider::new(self, &self.i64_resolver); + let p = InstrumentResolver::new(self, &self.i64_resolver); p.lookup( InstrumentKind::Histogram, @@ -672,36 +672,36 @@ impl ApiObserver for Observer { } } -impl fmt::Debug for Meter { +impl fmt::Debug for SdkMeter { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Meter").field("scope", &self.scope).finish() } } /// Provides all OpenTelemetry instruments. -struct InstProvider<'a, T> { - meter: &'a Meter, +struct InstrumentResolver<'a, T> { + meter: &'a SdkMeter, resolve: &'a Resolver, } -impl<'a, T> InstProvider<'a, T> +impl<'a, T> InstrumentResolver<'a, T> where T: Number, { - fn new(meter: &'a Meter, resolve: &'a Resolver) -> Self { - InstProvider { meter, resolve } + fn new(meter: &'a SdkMeter, resolve: &'a Resolver) -> Self { + InstrumentResolver { meter, resolve } } - /// lookup returns the resolved InstrumentImpl. + /// lookup returns the resolved measures. fn lookup( &self, kind: InstrumentKind, name: Cow<'static, str>, description: Option>, unit: Unit, - ) -> Result> { + ) -> Result> { let aggregators = self.measures(kind, name, description, unit)?; - Ok(InstrumentImpl { + Ok(ResolvedMeasures { measures: aggregators, }) } @@ -732,7 +732,7 @@ mod tests { use opentelemetry::metrics::{InstrumentProvider, MetricsError, Unit}; use super::{ - InstrumentValidationPolicy, Meter, INSTRUMENT_NAME_FIRST_ALPHABETIC, + InstrumentValidationPolicy, SdkMeter, INSTRUMENT_NAME_FIRST_ALPHABETIC, INSTRUMENT_NAME_INVALID_CHAR, INSTRUMENT_NAME_LENGTH, INSTRUMENT_UNIT_INVALID_CHAR, INSTRUMENT_UNIT_LENGTH, }; @@ -741,7 +741,7 @@ mod tests { #[test] fn test_instrument_config_validation() { // scope and pipelines are not related to test - let meter = Meter::new( + let meter = SdkMeter::new( Scope::default(), Arc::new(Pipelines::new(Resource::default(), Vec::new(), Vec::new())), ) diff --git a/opentelemetry-sdk/src/metrics/meter_provider.rs b/opentelemetry-sdk/src/metrics/meter_provider.rs index 9492cef76c..6f3178083c 100644 --- a/opentelemetry-sdk/src/metrics/meter_provider.rs +++ b/opentelemetry-sdk/src/metrics/meter_provider.rs @@ -8,13 +8,15 @@ use std::{ }; use opentelemetry::{ - metrics::{noop::NoopMeterCore, InstrumentProvider, Meter as ApiMeter, MetricsError, Result}, + metrics::{ + noop::NoopMeterCore, InstrumentProvider, Meter, MeterProvider, MetricsError, Result, + }, KeyValue, }; use crate::{instrumentation::Scope, Resource}; -use super::{meter::Meter as SdkMeter, pipeline::Pipelines, reader::MetricReader, view::View}; +use super::{meter::SdkMeter, pipeline::Pipelines, reader::MetricReader, view::View}; /// Handles the creation and coordination of [Meter]s. /// @@ -22,20 +24,20 @@ use super::{meter::Meter as SdkMeter, pipeline::Pipelines, reader::MetricReader, /// [Resource], have the same [View]s applied to them, and have their produced /// metric telemetry passed to the configured [MetricReader]s. /// -/// [Meter]: crate::metrics::Meter +/// [Meter]: opentelemetry::metrics::Meter #[derive(Clone, Debug)] -pub struct MeterProvider { +pub struct SdkMeterProvider { pipes: Arc, is_shutdown: Arc, } -impl Default for MeterProvider { +impl Default for SdkMeterProvider { fn default() -> Self { - MeterProvider::builder().build() + SdkMeterProvider::builder().build() } } -impl MeterProvider { +impl SdkMeterProvider { /// Flushes all pending telemetry. /// /// There is no guaranteed that all telemetry be flushed or all resources have @@ -53,10 +55,10 @@ impl MeterProvider { /// /// ``` /// use opentelemetry::{global, Context}; - /// use opentelemetry_sdk::metrics::MeterProvider; + /// use opentelemetry_sdk::metrics::SdkMeterProvider; /// - /// fn init_metrics() -> MeterProvider { - /// let provider = MeterProvider::default(); + /// fn init_metrics() -> SdkMeterProvider { + /// let provider = SdkMeterProvider::default(); /// /// // Set provider to be used as global meter provider /// let _ = global::set_meter_provider(provider.clone()); @@ -113,14 +115,14 @@ impl MeterProvider { } } -impl opentelemetry::metrics::MeterProvider for MeterProvider { +impl MeterProvider for SdkMeterProvider { fn versioned_meter( &self, name: impl Into>, version: Option>>, schema_url: Option>>, attributes: Option>, - ) -> ApiMeter { + ) -> Meter { let inst_provider: Arc = if !self.is_shutdown.load(Ordering::Relaxed) { let scope = Scope::new(name, version, schema_url, attributes); @@ -129,7 +131,7 @@ impl opentelemetry::metrics::MeterProvider for MeterProvider { Arc::new(NoopMeterCore::new()) }; - ApiMeter::new(inst_provider) + Meter::new(inst_provider) } } @@ -149,7 +151,7 @@ impl MeterProviderBuilder { /// /// By default, if this option is not used, the default [Resource] will be used. /// - /// [Meter]: crate::metrics::Meter + /// [Meter]: opentelemetry::metrics::Meter pub fn with_resource(mut self, resource: Resource) -> Self { self.resource = Some(resource); self @@ -177,8 +179,8 @@ impl MeterProviderBuilder { } /// Construct a new [MeterProvider] with this configuration. - pub fn build(self) -> MeterProvider { - MeterProvider { + pub fn build(self) -> SdkMeterProvider { + SdkMeterProvider { pipes: Arc::new(Pipelines::new( self.resource.unwrap_or_default(), self.readers, @@ -209,7 +211,8 @@ mod tests { #[test] fn test_meter_provider_resource() { // If users didn't provide a resource and there isn't a env var set. Use default one. - let assert_service_name = |provider: super::MeterProvider, expect: Option<&'static str>| { + let assert_service_name = |provider: super::SdkMeterProvider, + expect: Option<&'static str>| { assert_eq!( provider.pipes.0[0] .resource @@ -219,12 +222,14 @@ mod tests { ); }; let reader = TestMetricReader {}; - let default_meter_provider = super::MeterProvider::builder().with_reader(reader).build(); + let default_meter_provider = super::SdkMeterProvider::builder() + .with_reader(reader) + .build(); assert_service_name(default_meter_provider, Some("unknown_service")); // If user provided a resource, use that. let reader2 = TestMetricReader {}; - let custom_meter_provider = super::MeterProvider::builder() + let custom_meter_provider = super::SdkMeterProvider::builder() .with_reader(reader2) .with_resource(Resource::new(vec![KeyValue::new( "service.name", @@ -236,7 +241,9 @@ mod tests { // If `OTEL_RESOURCE_ATTRIBUTES` is set, read them automatically let reader3 = TestMetricReader {}; env::set_var("OTEL_RESOURCE_ATTRIBUTES", "key1=value1, k2, k3=value2"); - let env_resource_provider = super::MeterProvider::builder().with_reader(reader3).build(); + let env_resource_provider = super::SdkMeterProvider::builder() + .with_reader(reader3) + .build(); assert_eq!( env_resource_provider.pipes.0[0].resource, Resource::new(vec![ @@ -255,7 +262,7 @@ mod tests { "my-custom-key=env-val,k2=value2", ); let reader4 = TestMetricReader {}; - let user_provided_resource_config_provider = super::MeterProvider::builder() + let user_provided_resource_config_provider = super::SdkMeterProvider::builder() .with_reader(reader4) .with_resource( Resource::default().merge(&mut Resource::new(vec![KeyValue::new( @@ -279,7 +286,7 @@ mod tests { // If user provided a resource, it takes priority during collision. let reader5 = TestMetricReader {}; - let no_service_name = super::MeterProvider::builder() + let no_service_name = super::SdkMeterProvider::builder() .with_reader(reader5) .with_resource(Resource::empty()) .build(); diff --git a/opentelemetry-sdk/src/metrics/mod.rs b/opentelemetry-sdk/src/metrics/mod.rs index ece677ca42..c74e71bcd5 100644 --- a/opentelemetry-sdk/src/metrics/mod.rs +++ b/opentelemetry-sdk/src/metrics/mod.rs @@ -2,7 +2,7 @@ //! //! ## Configuration //! -//! The metrics SDK configuration is stored with each [MeterProvider]. +//! The metrics SDK configuration is stored with each [SdkMeterProvider]. //! Configuration for [Resource]s, [View]s, and [ManualReader] or //! [PeriodicReader] instances can be specified. //! @@ -10,16 +10,16 @@ //! //! ``` //! use opentelemetry::{ -//! metrics::{MeterProvider as _, Unit}, +//! metrics::{MeterProvider, Unit}, //! KeyValue, //! }; -//! use opentelemetry_sdk::{metrics::MeterProvider, Resource}; +//! use opentelemetry_sdk::{metrics::SdkMeterProvider, Resource}; //! //! // Generate SDK configuration, resource, views, etc //! let resource = Resource::default(); // default attributes about the current process //! //! // Create a meter provider with the desired config -//! let provider = MeterProvider::builder().with_resource(resource).build(); +//! let provider = SdkMeterProvider::builder().with_resource(resource).build(); //! //! // Use the meter provider to create meter instances //! let meter = provider.meter("my_app"); diff --git a/opentelemetry-sdk/src/metrics/pipeline.rs b/opentelemetry-sdk/src/metrics/pipeline.rs index 703cd4a040..6ebd7c9e05 100644 --- a/opentelemetry-sdk/src/metrics/pipeline.rs +++ b/opentelemetry-sdk/src/metrics/pipeline.rs @@ -98,7 +98,7 @@ impl Pipeline { Ok(move |this: &Pipeline| { let mut inner = this.inner.lock()?; - // can't compare trait objects so use index + toumbstones to drop + // can't compare trait objects so use index + tombstones to drop inner.multi_callbacks[idx] = None; Ok(()) }) diff --git a/opentelemetry-sdk/src/metrics/view.rs b/opentelemetry-sdk/src/metrics/view.rs index ceba51e1e9..23758f3bda 100644 --- a/opentelemetry-sdk/src/metrics/view.rs +++ b/opentelemetry-sdk/src/metrics/view.rs @@ -34,7 +34,7 @@ fn empty_view(_inst: &Instrument) -> Option { /// View is implemented for all `Fn(&Instrument) -> Option`. /// /// ``` -/// use opentelemetry_sdk::metrics::{Instrument, MeterProvider, Stream}; +/// use opentelemetry_sdk::metrics::{Instrument, SdkMeterProvider, Stream}; /// /// // return streams for the given instrument /// let my_view = |i: &Instrument| { @@ -42,7 +42,7 @@ fn empty_view(_inst: &Instrument) -> Option { /// None /// }; /// -/// let provider = MeterProvider::builder().with_view(my_view).build(); +/// let provider = SdkMeterProvider::builder().with_view(my_view).build(); /// # drop(provider) /// ``` pub trait View: Send + Sync + 'static { diff --git a/opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs b/opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs index 8699c8e190..d28cd4062f 100644 --- a/opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs +++ b/opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs @@ -39,7 +39,7 @@ use std::sync::{Arc, Mutex}; /// let exporter = InMemoryMetricsExporter::default(); /// /// // Create a MeterProvider and register the exporter -/// let meter_provider = metrics::MeterProvider::builder() +/// let meter_provider = metrics::SdkMeterProvider::builder() /// .with_reader(PeriodicReader::builder(exporter.clone(), runtime::Tokio).build()) /// .build(); /// diff --git a/opentelemetry-stdout/examples/basic.rs b/opentelemetry-stdout/examples/basic.rs index 53a0993109..d24fba671b 100644 --- a/opentelemetry-stdout/examples/basic.rs +++ b/opentelemetry-stdout/examples/basic.rs @@ -8,7 +8,7 @@ use opentelemetry::{ }; #[cfg(all(feature = "metrics", feature = "trace"))] use opentelemetry_sdk::{ - metrics::{MeterProvider, PeriodicReader}, + metrics::{PeriodicReader, SdkMeterProvider}, runtime, trace::TracerProvider, }; @@ -22,10 +22,10 @@ fn init_trace() -> TracerProvider { } #[cfg(all(feature = "metrics", feature = "trace"))] -fn init_metrics() -> MeterProvider { +fn init_metrics() -> SdkMeterProvider { let exporter = opentelemetry_stdout::MetricsExporter::default(); let reader = PeriodicReader::builder(exporter, runtime::Tokio).build(); - MeterProvider::builder().with_reader(reader).build() + SdkMeterProvider::builder().with_reader(reader).build() } #[tokio::main] diff --git a/opentelemetry-stdout/src/lib.rs b/opentelemetry-stdout/src/lib.rs index e80270af19..22690da2c3 100644 --- a/opentelemetry-stdout/src/lib.rs +++ b/opentelemetry-stdout/src/lib.rs @@ -5,11 +5,11 @@ //! ```no_run //! # #[cfg(all(feature = "metrics", feature = "trace"))] //! { -//! use opentelemetry::metrics::MeterProvider as _; +//! use opentelemetry::metrics::MeterProvider; //! use opentelemetry::trace::{Span, Tracer, TracerProvider as _}; //! use opentelemetry::{Context, KeyValue}; //! -//! use opentelemetry_sdk::metrics::{MeterProvider, PeriodicReader}; +//! use opentelemetry_sdk::metrics::{SdkMeterProvider, PeriodicReader}; //! use opentelemetry_sdk::runtime; //! use opentelemetry_sdk::trace::TracerProvider; //! @@ -20,10 +20,10 @@ //! .build() //! } //! -//! fn init_metrics() -> MeterProvider { +//! fn init_metrics() -> SdkMeterProvider { //! let exporter = opentelemetry_stdout::MetricsExporter::default(); //! let reader = PeriodicReader::builder(exporter, runtime::Tokio).build(); -//! MeterProvider::builder().with_reader(reader).build() +//! SdkMeterProvider::builder().with_reader(reader).build() //! } //! //! let tracer_provider = init_trace(); diff --git a/opentelemetry-user-events-metrics/examples/basic.rs b/opentelemetry-user-events-metrics/examples/basic.rs index b9d14e8baf..82fde30cca 100644 --- a/opentelemetry-user-events-metrics/examples/basic.rs +++ b/opentelemetry-user-events-metrics/examples/basic.rs @@ -4,14 +4,14 @@ use opentelemetry::{ KeyValue, }; use opentelemetry_sdk::{ - metrics::{MeterProvider, PeriodicReader}, + metrics::{PeriodicReader, SdkMeterProvider}, runtime, Resource, }; use opentelemetry_user_events_metrics::MetricsExporter; -fn init_metrics(exporter: MetricsExporter) -> MeterProvider { +fn init_metrics(exporter: MetricsExporter) -> SdkMeterProvider { let reader = PeriodicReader::builder(exporter, runtime::Tokio).build(); - MeterProvider::builder() + SdkMeterProvider::builder() .with_resource(Resource::new(vec![KeyValue::new( "service.name", "metric-demo", diff --git a/stress/src/metrics.rs b/stress/src/metrics.rs index 3072b6cff2..9f7d2d2349 100644 --- a/stress/src/metrics.rs +++ b/stress/src/metrics.rs @@ -3,14 +3,14 @@ use opentelemetry::{ metrics::{Counter, MeterProvider as _}, KeyValue, }; -use opentelemetry_sdk::metrics::{ManualReader, MeterProvider}; +use opentelemetry_sdk::metrics::{ManualReader, SdkMeterProvider}; use rand::{rngs::SmallRng, Rng, SeedableRng}; use std::borrow::Cow; mod throughput; lazy_static! { - static ref PROVIDER: MeterProvider = MeterProvider::builder() + static ref PROVIDER: SdkMeterProvider = SdkMeterProvider::builder() .with_reader(ManualReader::builder().build()) .build(); static ref ATTRIBUTE_VALUES: [&'static str; 10] = [