Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/exporter/http/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use http::{header::CONTENT_TYPE, Method};
use opentelemetry::otel_debug;
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
use opentelemetry_sdk::metrics::data::ResourceMetrics;
use opentelemetry_sdk::metrics::exporter::ResourceMetrics;

use super::OtlpHttpClient;

impl MetricsClient for OtlpHttpClient {
async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult {
async fn export(&self, metrics: ResourceMetrics<'_>) -> OTelSdkResult {

Check warning on line 12 in opentelemetry-otlp/src/exporter/http/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/http/metrics.rs#L12

Added line #L12 was not covered by tests
let client = self
.client
.lock()
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-otlp/src/exporter/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
mod metrics;

#[cfg(feature = "metrics")]
use opentelemetry_sdk::metrics::data::ResourceMetrics;
use opentelemetry_sdk::metrics::exporter::ResourceMetrics;

#[cfg(feature = "logs")]
pub(crate) mod logs;
Expand Down Expand Up @@ -326,11 +326,11 @@
#[cfg(feature = "metrics")]
fn build_metrics_export_body(
&self,
metrics: &mut ResourceMetrics,
metrics: ResourceMetrics<'_>,

Check warning on line 329 in opentelemetry-otlp/src/exporter/http/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/http/mod.rs#L329

Added line #L329 was not covered by tests
) -> Option<(Vec<u8>, &'static str)> {
use opentelemetry_proto::tonic::collector::metrics::v1::ExportMetricsServiceRequest;

let req: ExportMetricsServiceRequest = (&*metrics).into();
let req: ExportMetricsServiceRequest = metrics.into();

Check warning on line 333 in opentelemetry-otlp/src/exporter/http/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/http/mod.rs#L333

Added line #L333 was not covered by tests

match self.protocol {
#[cfg(feature = "http-json")]
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-otlp/src/exporter/tonic/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
metrics_service_client::MetricsServiceClient, ExportMetricsServiceRequest,
};
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
use opentelemetry_sdk::metrics::data::ResourceMetrics;
use opentelemetry_sdk::metrics::exporter::ResourceMetrics;
use tonic::{codegen::CompressionEncoding, service::Interceptor, transport::Channel, Request};

use super::BoxInterceptor;
Expand Down Expand Up @@ -52,7 +52,7 @@
}

impl MetricsClient for TonicMetricsClient {
async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult {
async fn export(&self, metrics: ResourceMetrics<'_>) -> OTelSdkResult {

Check warning on line 55 in opentelemetry-otlp/src/exporter/tonic/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/tonic/metrics.rs#L55

Added line #L55 was not covered by tests
let (mut client, metadata, extensions) = self
.inner
.lock()
Expand Down Expand Up @@ -81,7 +81,7 @@
.export(Request::from_parts(
metadata,
extensions,
ExportMetricsServiceRequest::from(&*metrics),
ExportMetricsServiceRequest::from(metrics),

Check warning on line 84 in opentelemetry-otlp/src/exporter/tonic/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/tonic/metrics.rs#L84

Added line #L84 was not covered by tests
))
.await
.map_err(|e| OTelSdkError::InternalFailure(format!("{e:?}")))?;
Expand Down
9 changes: 4 additions & 5 deletions opentelemetry-otlp/src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
use core::fmt;
use opentelemetry_sdk::error::OTelSdkResult;

use opentelemetry_sdk::metrics::{
data::ResourceMetrics, exporter::PushMetricExporter, Temporality,
};
use opentelemetry_sdk::metrics::exporter::ResourceMetrics;
use opentelemetry_sdk::metrics::{exporter::PushMetricExporter, Temporality};
use std::fmt::{Debug, Formatter};
use std::time::Duration;

Expand Down Expand Up @@ -123,7 +122,7 @@
pub(crate) trait MetricsClient: fmt::Debug + Send + Sync + 'static {
fn export(
&self,
metrics: &mut ResourceMetrics,
metrics: ResourceMetrics<'_>,
) -> impl std::future::Future<Output = OTelSdkResult> + Send;
fn shutdown(&self) -> OTelSdkResult;
}
Expand All @@ -149,7 +148,7 @@
}

impl PushMetricExporter for MetricExporter {
async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult {
async fn export(&self, metrics: ResourceMetrics<'_>) -> OTelSdkResult {

Check warning on line 151 in opentelemetry-otlp/src/metric.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/metric.rs#L151

Added line #L151 was not covered by tests
match &self.client {
#[cfg(feature = "grpc-tonic")]
SupportedTransportClient::Tonic(client) => client.export(metrics).await,
Expand Down
38 changes: 23 additions & 15 deletions opentelemetry-proto/src/transform/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use opentelemetry_sdk::metrics::data::{
AggregatedMetrics, Exemplar as SdkExemplar,
ExponentialHistogram as SdkExponentialHistogram, Gauge as SdkGauge,
Histogram as SdkHistogram, Metric as SdkMetric, MetricData, ResourceMetrics,
ScopeMetrics as SdkScopeMetrics, Sum as SdkSum,
Histogram as SdkHistogram, MetricData, Sum as SdkSum,
};
use opentelemetry_sdk::metrics::exporter::{Metric, ResourceMetrics, ScopeMetrics};
use opentelemetry_sdk::metrics::Temporality;
use opentelemetry_sdk::Resource as SdkResource;

Expand Down Expand Up @@ -110,12 +110,16 @@
}
}

impl From<&ResourceMetrics> for ExportMetricsServiceRequest {
fn from(rm: &ResourceMetrics) -> Self {
impl From<ResourceMetrics<'_>> for ExportMetricsServiceRequest {
fn from(mut rm: ResourceMetrics<'_>) -> Self {
let mut scope_metrics = Vec::new();
while let Some(scope_metric) = rm.scope_metrics.next_scope_metric() {
scope_metrics.push(scope_metric.into());
}

Check warning on line 118 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L114-L118

Added lines #L114 - L118 were not covered by tests
ExportMetricsServiceRequest {
resource_metrics: vec![TonicResourceMetrics {
resource: Some((&rm.resource).into()),
scope_metrics: rm.scope_metrics.iter().map(Into::into).collect(),
resource: Some(rm.resource.into()),
scope_metrics,

Check warning on line 122 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L121-L122

Added lines #L121 - L122 were not covered by tests
schema_url: rm.resource.schema_url().map(Into::into).unwrap_or_default(),
}],
}
Expand All @@ -131,11 +135,15 @@
}
}

impl From<&SdkScopeMetrics> for TonicScopeMetrics {
fn from(sm: &SdkScopeMetrics) -> Self {
impl From<ScopeMetrics<'_>> for TonicScopeMetrics {
fn from(mut sm: ScopeMetrics<'_>) -> Self {
let mut metrics = Vec::new();
while let Some(metric) = sm.metrics.next_metric() {
metrics.push(metric.into());
}

Check warning on line 143 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L139-L143

Added lines #L139 - L143 were not covered by tests
TonicScopeMetrics {
scope: Some((&sm.scope, None).into()),
metrics: sm.metrics.iter().map(Into::into).collect(),
scope: Some((sm.scope, None).into()),
metrics,

Check warning on line 146 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L145-L146

Added lines #L145 - L146 were not covered by tests
schema_url: sm
.scope
.schema_url()
Expand All @@ -145,12 +153,12 @@
}
}

impl From<&SdkMetric> for TonicMetric {
fn from(metric: &SdkMetric) -> Self {
impl From<Metric<'_>> for TonicMetric {
fn from(metric: Metric<'_>) -> Self {

Check warning on line 157 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L157

Added line #L157 was not covered by tests
TonicMetric {
name: metric.name.to_string(),
description: metric.description.to_string(),
unit: metric.unit.to_string(),
name: metric.instrument.name.to_string(),
description: metric.instrument.description.to_string(),
unit: metric.instrument.unit.to_string(),

Check warning on line 161 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L159-L161

Added lines #L159 - L161 were not covered by tests
metadata: vec![], // internal and currently unused
data: Some(match &metric.data {
AggregatedMetrics::F64(data) => data.into(),
Expand Down
10 changes: 10 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## vNext

- *Breaking* change for `PushMetricExporter::export` from accepting
`metrics: &mut ResourceMetrics`, to accepting `metrics: ResourceMetrics<'_>`.
In addition, `ResourceMetrics` was also changed to allow improving underlying
metric collection without any allocations in the future.
[#2921](https://github.com/open-telemetry/opentelemetry-rust/pull/2921)
- *Breaking* change for `Metric::data` field: From dynamic `Box<dyn Aggregation>`
to new enum `AggregatedMetrics`.
[#2857](https://github.com/open-telemetry/opentelemetry-rust/pull/2857)

[#2868](https://github.com/open-telemetry/opentelemetry-rust/pull/2868)
`SdkLogger`, `SdkTracer` modified to respect telemetry suppression based on
`Context`. In other words, if the current context has telemetry suppression
Expand Down Expand Up @@ -40,6 +49,7 @@ also modified to suppress telemetry before invoking exporters.
"spec_unstable_metrics_views". This was only required when using Views.
[2928](https://github.com/open-telemetry/opentelemetry-rust/pull/2928)


## 0.29.0

Released 2025-Mar-21
Expand Down
12 changes: 7 additions & 5 deletions opentelemetry-sdk/benches/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use opentelemetry::{
use opentelemetry_sdk::{
error::OTelSdkResult,
metrics::{
data::ResourceMetrics, new_view, reader::MetricReader, Aggregation, Instrument,
InstrumentKind, ManualReader, Pipeline, SdkMeterProvider, Stream, Temporality, View,
new_view,
reader::{MetricReader, ResourceMetricsData},
Aggregation, Instrument, InstrumentKind, ManualReader, Pipeline, SdkMeterProvider, Stream,
Temporality, View,
},
Resource,
};
Expand All @@ -23,7 +25,7 @@ impl MetricReader for SharedReader {
self.0.register_pipeline(pipeline)
}

fn collect(&self, rm: &mut ResourceMetrics) -> OTelSdkResult {
fn collect(&self, rm: &mut ResourceMetricsData) -> OTelSdkResult {
self.0.collect(rm)
}

Expand Down Expand Up @@ -240,7 +242,7 @@ fn counters(c: &mut Criterion) {
});

let (rdr, cntr) = bench_counter(None, "cumulative");
let mut rm = ResourceMetrics {
let mut rm = ResourceMetricsData {
resource: Resource::builder_empty().build(),
scope_metrics: Vec::new(),
};
Expand Down Expand Up @@ -337,7 +339,7 @@ fn benchmark_collect_histogram(b: &mut Bencher, n: usize) {
h.record(1, &[]);
}

let mut rm = ResourceMetrics {
let mut rm = ResourceMetricsData {
resource: Resource::builder_empty().build(),
scope_metrics: Vec::new(),
};
Expand Down
39 changes: 2 additions & 37 deletions opentelemetry-sdk/src/metrics/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,11 @@
//! Types for delivery of pre-aggregated metric time series data.

use std::{borrow::Cow, time::SystemTime};
use std::time::SystemTime;

use opentelemetry::{InstrumentationScope, KeyValue};

use crate::Resource;
use opentelemetry::KeyValue;

use super::Temporality;

/// A collection of [ScopeMetrics] and the associated [Resource] that created them.
#[derive(Debug)]
pub struct ResourceMetrics {
/// The entity that collected the metrics.
pub resource: Resource,
/// The collection of metrics with unique [InstrumentationScope]s.
pub scope_metrics: Vec<ScopeMetrics>,
}

/// A collection of metrics produced by a meter.
#[derive(Default, Debug)]
pub struct ScopeMetrics {
/// The [InstrumentationScope] that the meter was created with.
pub scope: InstrumentationScope,
/// The list of aggregations created by the meter.
pub metrics: Vec<Metric>,
}

/// A collection of one or more aggregated time series from an [Instrument].
///
/// [Instrument]: crate::metrics::Instrument
#[derive(Debug)]
pub struct Metric {
/// The name of the instrument that created this data.
pub name: Cow<'static, str>,
/// The description of the instrument, which can be used in documentation.
pub description: Cow<'static, str>,
/// The unit in which the instrument reports.
pub unit: Cow<'static, str>,
/// The aggregated data from an instrument.
pub data: AggregatedMetrics,
}

/// Aggregated metrics data from an instrument
#[derive(Debug)]
pub enum AggregatedMetrics {
Expand Down
Loading
Loading