Skip to content

Commit

Permalink
Chore/metrics advanced (open-telemetry#2204)
Browse files Browse the repository at this point in the history
Co-authored-by: Cijo Thomas <[email protected]>
  • Loading branch information
pitoniak32 and cijothomas authored Oct 14, 2024
1 parent 42685e8 commit 3f5c230
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 56 deletions.
2 changes: 1 addition & 1 deletion examples/metrics-advanced/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ publish = false
[dependencies]
opentelemetry = { path = "../../opentelemetry", features = ["metrics"] }
opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["metrics", "rt-tokio"] }
opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["metrics"]}
opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["metrics"] }
tokio = { workspace = true, features = ["full"] }
serde_json = { workspace = true }
7 changes: 6 additions & 1 deletion examples/metrics-advanced/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use opentelemetry::global;
use opentelemetry::Key;
use opentelemetry::KeyValue;
use opentelemetry_sdk::metrics::reader::DeltaTemporalitySelector;
use opentelemetry_sdk::metrics::{
Aggregation, Instrument, PeriodicReader, SdkMeterProvider, Stream,
};
Expand Down Expand Up @@ -44,7 +45,11 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
}
};

let exporter = opentelemetry_stdout::MetricsExporterBuilder::default().build();
// Build exporter using Delta Temporality.
let exporter = opentelemetry_stdout::MetricsExporterBuilder::default()
.with_temporality_selector(DeltaTemporalitySelector::new())
.build();

let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
let provider = SdkMeterProvider::builder()
.with_reader(reader)
Expand Down
33 changes: 2 additions & 31 deletions opentelemetry-otlp/src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use opentelemetry_sdk::{
metrics::{
data::{ResourceMetrics, Temporality},
exporter::PushMetricsExporter,
reader::{DefaultTemporalitySelector, TemporalitySelector},
reader::{DefaultTemporalitySelector, DeltaTemporalitySelector, TemporalitySelector},
InstrumentKind, PeriodicReader, SdkMeterProvider,
},
runtime::Runtime,
Expand Down Expand Up @@ -169,7 +169,7 @@ where
///
/// [exporter-docs]: https://github.com/open-telemetry/opentelemetry-specification/blob/a1c13d59bb7d0fb086df2b3e1eaec9df9efef6cc/specification/metrics/sdk_exporters/otlp.md#additional-configuration
pub fn with_delta_temporality(self) -> Self {
self.with_temporality_selector(DeltaTemporalitySelector)
self.with_temporality_selector(DeltaTemporalitySelector::new())
}
}

Expand Down Expand Up @@ -237,35 +237,6 @@ impl<RT, EB: Debug> Debug for OtlpMetricPipeline<RT, EB> {
}
}

/// A temporality selector that returns [`Delta`][Temporality::Delta] for all
/// instruments except `UpDownCounter` and `ObservableUpDownCounter`.
///
/// This temporality selector is equivalent to OTLP Metrics Exporter's
/// `Delta` temporality preference (see [its documentation][exporter-docs]).
///
/// [exporter-docs]: https://github.com/open-telemetry/opentelemetry-specification/blob/a1c13d59bb7d0fb086df2b3e1eaec9df9efef6cc/specification/metrics/sdk_exporters/otlp.md#additional-configuration
#[derive(Debug)]
struct DeltaTemporalitySelector;

impl TemporalitySelector for DeltaTemporalitySelector {
#[rustfmt::skip]
fn temporality(&self, kind: InstrumentKind) -> Temporality {
match kind {
InstrumentKind::Counter
| InstrumentKind::Histogram
| InstrumentKind::ObservableCounter
| InstrumentKind::Gauge
| InstrumentKind::ObservableGauge => {
Temporality::Delta
}
InstrumentKind::UpDownCounter
| InstrumentKind::ObservableUpDownCounter => {
Temporality::Cumulative
}
}
}
}

/// An interface for OTLP metrics clients
#[async_trait]
pub trait MetricsClient: fmt::Debug + Send + Sync + 'static {
Expand Down
24 changes: 1 addition & 23 deletions opentelemetry-sdk/benches/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use opentelemetry_sdk::{
metrics::{
data::{ResourceMetrics, Temporality},
new_view,
reader::{MetricReader, TemporalitySelector},
reader::{DeltaTemporalitySelector, MetricReader, TemporalitySelector},
Aggregation, Instrument, InstrumentKind, ManualReader, Pipeline, SdkMeterProvider, Stream,
View,
},
Expand Down Expand Up @@ -44,28 +44,6 @@ impl MetricReader for SharedReader {
}
}

/// Configure delta temporality for all [InstrumentKind]
///
/// [Temporality::Delta] will be used for all instrument kinds if this
/// [TemporalitySelector] is used.
#[derive(Clone, Default, Debug)]
pub struct DeltaTemporalitySelector {
pub(crate) _private: (),
}

impl DeltaTemporalitySelector {
/// Create a new default temporality selector.
pub fn new() -> Self {
Self::default()
}
}

impl TemporalitySelector for DeltaTemporalitySelector {
fn temporality(&self, _kind: InstrumentKind) -> Temporality {
Temporality::Delta
}
}

// * Summary *

// rustc 1.68.0 (2c8cc3432 2023-03-06)
Expand Down
38 changes: 38 additions & 0 deletions opentelemetry-sdk/src/metrics/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,41 @@ impl TemporalitySelector for DefaultTemporalitySelector {
Temporality::Cumulative
}
}

/// A temporality selector that returns [`Delta`][Temporality::Delta] for all
/// instruments except `UpDownCounter` and `ObservableUpDownCounter`.
///
/// This temporality selector is equivalent to OTLP Metrics Exporter's
/// `Delta` temporality preference (see [its documentation][exporter-docs]).
///
/// [exporter-docs]: https://github.com/open-telemetry/opentelemetry-specification/blob/a1c13d59bb7d0fb086df2b3e1eaec9df9efef6cc/specification/metrics/sdk_exporters/otlp.md#additional-configuration
#[derive(Clone, Default, Debug)]
pub struct DeltaTemporalitySelector {
pub(crate) _private: (),
}

impl DeltaTemporalitySelector {
/// Create a new default temporality selector.
pub fn new() -> Self {
Self::default()
}
}

impl TemporalitySelector for DeltaTemporalitySelector {
#[rustfmt::skip]
fn temporality(&self, kind: InstrumentKind) -> Temporality {
match kind {
InstrumentKind::Counter
| InstrumentKind::Histogram
| InstrumentKind::ObservableCounter
| InstrumentKind::Gauge
| InstrumentKind::ObservableGauge => {
Temporality::Delta
}
InstrumentKind::UpDownCounter
| InstrumentKind::ObservableUpDownCounter => {
Temporality::Cumulative
}
}
}
}

0 comments on commit 3f5c230

Please sign in to comment.