-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simpler usage of Observable instruments #1715
Changes from all commits
b481cf2
9d8b1d1
b5d7f1c
1c466cc
c9e9a26
5a41a1f
3e4c1b3
465e66f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,23 +43,21 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | |
); | ||
|
||
// Create a ObservableCounter instrument and register a callback that reports the measurement. | ||
let observable_counter = meter | ||
let _observable_counter = meter | ||
.u64_observable_counter("my_observable_counter") | ||
.with_description("My observable counter example description") | ||
.with_unit(Unit::new("myunit")) | ||
.with_callback(|observer| { | ||
observer.observe( | ||
100, | ||
&[ | ||
KeyValue::new("mykey1", "myvalue1"), | ||
KeyValue::new("mykey2", "myvalue2"), | ||
], | ||
) | ||
}) | ||
.init(); | ||
|
||
meter.register_callback(&[observable_counter.as_any()], move |observer| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From Community Meeting 5/7: Lalit, Zhongyang:
Zhongyang: Opposed to removing it, because it was part of documented API, not because it is required. Lalit: +1 to above. |
||
observer.observe_u64( | ||
&observable_counter, | ||
100, | ||
&[ | ||
KeyValue::new("mykey1", "myvalue1"), | ||
KeyValue::new("mykey2", "myvalue2"), | ||
], | ||
) | ||
})?; | ||
|
||
// Create a UpCounter Instrument. | ||
let updown_counter = meter.i64_up_down_counter("my_updown_counter").init(); | ||
|
||
|
@@ -73,23 +71,21 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | |
); | ||
|
||
// Create a Observable UpDownCounter instrument and register a callback that reports the measurement. | ||
let observable_up_down_counter = meter | ||
let _observable_up_down_counter = meter | ||
.i64_observable_up_down_counter("my_observable_updown_counter") | ||
.with_description("My observable updown counter example description") | ||
.with_unit(Unit::new("myunit")) | ||
.with_callback(|observer| { | ||
observer.observe( | ||
100, | ||
&[ | ||
KeyValue::new("mykey1", "myvalue1"), | ||
KeyValue::new("mykey2", "myvalue2"), | ||
], | ||
) | ||
}) | ||
.init(); | ||
|
||
meter.register_callback(&[observable_up_down_counter.as_any()], move |observer| { | ||
observer.observe_i64( | ||
&observable_up_down_counter, | ||
100, | ||
&[ | ||
KeyValue::new("mykey1", "myvalue1"), | ||
KeyValue::new("mykey2", "myvalue2"), | ||
], | ||
) | ||
})?; | ||
|
||
// Create a Histogram Instrument. | ||
let histogram = meter | ||
.f64_histogram("my_histogram") | ||
|
@@ -108,41 +104,36 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | |
// Note that there is no ObservableHistogram instrument. | ||
|
||
// Create a Gauge Instrument. | ||
{ | ||
let gauge = meter | ||
.f64_gauge("my_gauge") | ||
.with_description("A gauge set to 1.0") | ||
.with_unit(Unit::new("myunit")) | ||
.init(); | ||
|
||
gauge.record( | ||
1.0, | ||
&[ | ||
KeyValue::new("mykey1", "myvalue1"), | ||
KeyValue::new("mykey2", "myvalue2"), | ||
], | ||
); | ||
} | ||
let gauge = meter | ||
.f64_gauge("my_gauge") | ||
.with_description("A gauge set to 1.0") | ||
.with_unit(Unit::new("myunit")) | ||
.init(); | ||
|
||
gauge.record( | ||
1.0, | ||
&[ | ||
KeyValue::new("mykey1", "myvalue1"), | ||
KeyValue::new("mykey2", "myvalue2"), | ||
], | ||
); | ||
|
||
// Create a ObservableGauge instrument and register a callback that reports the measurement. | ||
let observable_gauge = meter | ||
let _observable_gauge = meter | ||
.f64_observable_gauge("my_observable_gauge") | ||
.with_description("An observable gauge set to 1.0") | ||
.with_unit(Unit::new("myunit")) | ||
.with_callback(|observer| { | ||
observer.observe( | ||
1.0, | ||
&[ | ||
KeyValue::new("mykey1", "myvalue1"), | ||
KeyValue::new("mykey2", "myvalue2"), | ||
], | ||
) | ||
}) | ||
.init(); | ||
|
||
// Register a callback that reports the measurement. | ||
meter.register_callback(&[observable_gauge.as_any()], move |observer| { | ||
observer.observe_f64( | ||
&observable_gauge, | ||
1.0, | ||
&[ | ||
KeyValue::new("mykey1", "myvalue1"), | ||
KeyValue::new("mykey2", "myvalue2"), | ||
], | ||
) | ||
})?; | ||
|
||
// Metrics are exported by default every 30 seconds when using stdout exporter, | ||
// however shutting down the MeterProvider here instantly flushes | ||
// the metrics, instead of waiting for the 30 sec interval. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is still type-safe. i.e passing 100.0 will be blocked by compiler. But the API itself is more consistent with sync versions, where we don't use record_i64 or record_u64 etc. instead they are just record,add.