Skip to content

feat(metrics): add public constructors for metric data types#3489

Open
darklight3it wants to merge 5 commits intoopen-telemetry:mainfrom
darklight3it:add-public-constructor-to-metric-data-types
Open

feat(metrics): add public constructors for metric data types#3489
darklight3it wants to merge 5 commits intoopen-telemetry:mainfrom
darklight3it:add-public-constructor-to-metric-data-types

Conversation

@darklight3it
Copy link
Copy Markdown

@darklight3it darklight3it commented May 3, 2026

Fixes #3469

Changes

Added public constructors to metric data types, allowing users to use the public PushMetricExporter::export.

Rationale:

  • Types with one or more optional fields use the builder pattern, where the builder explicitly lists mandatory fields. This matches the established pattern in opentelemetry-sdk, opentelemetry-otlp, and other crates.
  • Types with only required fields use a new method.

Summary:

Type Constructor Required fields Optional (with_*) fields
ResourceMetrics builder() resource, scope_metrics
ScopeMetrics builder() scope, metrics
Metric builder(name, data) name, data description, unit
Gauge<T> builder(data_points, time) data_points, time start_time
GaugeDataPoint<T> builder(value) value attributes, exemplars
Sum<T> new(...) data_points, temporality, is_monotonic, start_time, time
SumDataPoint<T> builder(value) value attributes, exemplars
Histogram<T> new(...) data_points, temporality, start_time, time
HistogramDataPoint<T> builder(count, sum, bounds, bucket_counts) count, sum, bounds, bucket_counts attributes, min, max, exemplars
ExponentialHistogram<T> new(...) data_points, temporality, start_time, time
ExponentialHistogramDataPoint<T> builder(count, sum, scale, zero_count, positive_bucket, negative_bucket) count, sum, scale, zero_count, positive_bucket, negative_bucket attributes, min, max, zero_threshold, exemplars
ExponentialBucket new(offset, counts) offset, counts
Exemplar<T> builder(value, time) value, time filtered_attributes, span_id, trace_id

Example usage:

use opentelemetry::{InstrumentationScope, KeyValue};
use opentelemetry::time::now;
use opentelemetry_sdk::metrics::data::*;
use opentelemetry_sdk::metrics::{Temporality, PushMetricExporter};
use opentelemetry_sdk::Resource;

let gauge_dp = GaugeDataPoint::builder(42.0_f64)
    .with_attributes(vec![KeyValue::new("host", "localhost")])
    .build();

let gauge = Gauge::builder(vec![gauge_dp], now()).build();

let metric = Metric::builder("cpu.usage", AggregatedMetrics::F64(MetricData::Gauge(gauge)))
    .with_description("CPU usage percentage")
    .with_unit("%")
    .build();

let scope_metrics = ScopeMetrics::builder()
    .with_scope(InstrumentationScope::builder("my-library").build())
    .with_metrics(vec![metric])
    .build();

let resource_metrics = ResourceMetrics::builder()
    .with_resource(Resource::builder().with_service_name("my-service").build())
    .with_scope_metrics(vec![scope_metrics])
    .build();

exporter.export(&resource_metrics).await?;

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented May 3, 2026

CLA Not Signed

@darklight3it darklight3it changed the title Add public constructors for metric data types feat(metrics): add public constructors for metric data types May 3, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 3, 2026

Codecov Report

❌ Patch coverage is 99.08884% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.4%. Comparing base (cef3317) to head (e42bbdc).

Files with missing lines Patch % Lines
opentelemetry-sdk/src/metrics/data/mod.rs 99.0% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #3489     +/-   ##
=======================================
+ Coverage   83.7%   84.4%   +0.7%     
=======================================
  Files        126     126             
  Lines      25386   25825    +439     
=======================================
+ Hits       21255   21818    +563     
+ Misses      4131    4007    -124     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@darklight3it darklight3it force-pushed the add-public-constructor-to-metric-data-types branch from f3fc113 to d3e1646 Compare May 3, 2026 12:30
@darklight3it darklight3it marked this pull request as ready for review May 5, 2026 09:47
@darklight3it darklight3it requested a review from a team as a code owner May 5, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add public constructors for metric data types

1 participant