You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The way these variable names are worded make it sound like that's the only effect each view has and neglects to mention that the implementation of these closures also drops all pieces of information about each instrument that aren't specified again via Stream combinators. To demonstrate:
$ cargo run -qp metrics-advanced | jq '.resourceMetrics.scopeMetrics[].metrics[].name'"my_histogram_renamed"""""
It's quite surprising that the names are dropped despite no indication that this would happen.
Here's a small program that demonstrates how this is problematic and also demonstrates how to achieve the desired (and implied) behavior:
// [dependencies]// opentelemetry = { version = "0.23.0", features = ["metrics"] }// opentelemetry-prometheus = "0.16.0"// opentelemetry_sdk = "0.23.0"// prometheus = "0.13.4"use std::error::Error;use opentelemetry::metrics::{MeterProvider,Unit};use opentelemetry_prometheus::exporter;use opentelemetry_sdk::metrics::{
new_view,Aggregation,Instrument,SdkMeterProvider,Stream,};use prometheus::{Registry,TextEncoder};fnmain(){ifletErr(e) = minimal_repro(){eprintln!("error: {e}");}ifthis_works().is_err(){unreachable!()}}// This does what the example implies you should do to only change the// boundaries but leave everything else the same.fnminimal_repro() -> Result<(),Box<dynError>>{let registry = Registry::new();let exporter = exporter().with_registry(registry.clone()).build()?;let provider = SdkMeterProvider::builder().with_reader(exporter).with_view(|_:&Instrument| {Some(Stream::new().aggregation(Aggregation::ExplicitBucketHistogram{boundaries:vec![1.,2.,3.],record_min_max:true,},))}).build();let meter = provider.meter("example");let histogram =
meter.u64_histogram("histogram").with_unit(Unit::new("seconds")).init();
histogram.record(1,&[]);let encoded = TextEncoder::new().encode_to_string(®istry.gather())?;println!("{encoded}");Ok(())}// This version actually works. The secret sauce is to use the `new_view`// function.fnthis_works() -> Result<(),Box<dynError>>{let registry = Registry::new();let exporter = exporter().with_registry(registry.clone()).build()?;let provider = SdkMeterProvider::builder().with_reader(exporter).with_view(new_view(Instrument::new().name("histogram"),Stream::new().aggregation(Aggregation::ExplicitBucketHistogram{boundaries:vec![1.,2.,3.],record_min_max:true,}),)?).build();let meter = provider.meter("example");let histogram =
meter.u64_histogram("histogram").with_unit(Unit::new("seconds")).init();
histogram.record(1,&[]);let encoded = TextEncoder::new().encode_to_string(®istry.gather())?;println!("{encoded}");Ok(())}
I've pasted the output of this program in the "Relevant log output" section.
Thanks for reporting! It looks like a bug in view implementation, not just an issue with doc.
We won't be fixing the View bug, as Views are about to be removed for the initial stable release.
For customizing histogram buckets, #1241 will be offered instead.
What happened?
The way these variable names are worded make it sound like that's the only effect each view has and neglects to mention that the implementation of these closures also drops all pieces of information about each instrument that aren't specified again via
Stream
combinators. To demonstrate:It's quite surprising that the names are dropped despite no indication that this would happen.
Here's a small program that demonstrates how this is problematic and also demonstrates how to achieve the desired (and implied) behavior:
I've pasted the output of this program in the "Relevant log output" section.
API Version
0.23.0
SDK Version
0.23.0
What Exporter(s) are you seeing the problem on?
Prometheus
Relevant log output
The text was updated successfully, but these errors were encountered: