Skip to content

Commit

Permalink
More distinct naming of metrics constructs in the SDK (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaun-cox authored Nov 8, 2023
1 parent 4c3f8a0 commit 5d5296b
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 101 deletions.
8 changes: 5 additions & 3 deletions examples/metrics-advanced/src/main.rs
Original file line number Diff line number Diff line change
@@ -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" {
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions examples/metrics-basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
.install_batch(opentelemetry_sdk::runtime::Tokio)
}

fn init_metrics() -> metrics::Result<sdkmetrics::MeterProvider> {
fn init_metrics() -> metrics::Result<sdkmetrics::SdkMeterProvider> {
let export_config = opentelemetry_otlp::ExportConfig {
endpoint: "http://localhost:4318/v1/metrics".to_string(),
..opentelemetry_otlp::ExportConfig::default()
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<sdktrace::Tracer, TraceError> {
Expand All @@ -32,7 +32,7 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
.install_batch(runtime::Tokio)
}

fn init_metrics() -> metrics::Result<MeterProvider> {
fn init_metrics() -> metrics::Result<SdkMeterProvider> {
let export_config = ExportConfig {
endpoint: "http://localhost:4317".to_string(),
..ExportConfig::default()
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-otlp/src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use opentelemetry_sdk::{
AggregationSelector, DefaultAggregationSelector, DefaultTemporalitySelector,
TemporalitySelector,
},
Aggregation, InstrumentKind, MeterProvider, PeriodicReader,
Aggregation, InstrumentKind, PeriodicReader, SdkMeterProvider,
},
runtime::Runtime,
Resource,
Expand Down Expand Up @@ -215,7 +215,7 @@ where
RT: Runtime,
{
/// Build MeterProvider
pub fn build(self) -> Result<MeterProvider> {
pub fn build(self) -> Result<SdkMeterProvider> {
let exporter = self.exporter_pipeline.build_metrics_exporter(
self.temporality_selector
.unwrap_or_else(|| Box::new(DefaultTemporalitySelector::new())),
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-prometheus/examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,7 +71,7 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
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 {
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-prometheus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-prometheus/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -336,7 +336,7 @@ fn prometheus_exporter_integration() {
))
};

let provider = MeterProvider::builder()
let provider = SdkMeterProvider::builder()
.with_resource(res)
.with_reader(exporter)
.with_view(
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-sdk/benches/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -150,7 +150,7 @@ fn bench_counter(view: Option<Box<dyn View>>, 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);
}
Expand Down Expand Up @@ -367,7 +367,7 @@ fn bench_histogram(bound_count: usize) -> (SharedReader, Histogram<i64>) {
);

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);
}
Expand Down Expand Up @@ -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");
Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-sdk/src/metrics/instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,27 +248,27 @@ impl InstrumentId {
}
}

pub(crate) struct InstrumentImpl<T> {
pub(crate) struct ResolvedMeasures<T> {
pub(crate) measures: Vec<Arc<dyn Measure<T>>>,
}

impl<T: Copy + 'static> SyncCounter<T> for InstrumentImpl<T> {
impl<T: Copy + 'static> SyncCounter<T> for ResolvedMeasures<T> {
fn add(&self, val: T, attrs: &[KeyValue]) {
for measure in &self.measures {
measure.call(val, AttributeSet::from(attrs))
}
}
}

impl<T: Copy + 'static> SyncUpDownCounter<T> for InstrumentImpl<T> {
impl<T: Copy + 'static> SyncUpDownCounter<T> for ResolvedMeasures<T> {
fn add(&self, val: T, attrs: &[KeyValue]) {
for measure in &self.measures {
measure.call(val, AttributeSet::from(attrs))
}
}
}

impl<T: Copy + 'static> SyncHistogram<T> for InstrumentImpl<T> {
impl<T: Copy + 'static> SyncHistogram<T> for ResolvedMeasures<T> {
fn record(&self, val: T, attrs: &[KeyValue]) {
for measure in &self.measures {
measure.call(val, AttributeSet::from(attrs))
Expand Down
Loading

0 comments on commit 5d5296b

Please sign in to comment.