-
Notifications
You must be signed in to change notification settings - Fork 452
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
Allow precreation of AttributeSets for metrics #1421
Changes from 27 commits
6f42c3a
92977cd
9f9905b
8fe79b9
4b61676
d47e9df
3cf8b13
1cb67fc
dbf2c16
3160dad
5ad5b7b
c0e9878
c442a1a
5b2ef1a
26afa6e
8417984
9e85ddc
4f4d4de
747c40e
46994cb
83f10d3
6b6e9e0
377e8b0
7869fa7
70cbdbb
18a001f
345a0e7
a37bf5a
ae482c3
440a5bc
a34194d
4d5dcdd
0a1899a
892b6f1
d2f7ad0
a3b1cf4
ef330e1
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use log::{info, Level}; | ||
use once_cell::sync::Lazy; | ||
use opentelemetry::attributes::AttributeSet; | ||
use opentelemetry::global; | ||
use opentelemetry::global::{logger_provider, shutdown_logger_provider, shutdown_tracer_provider}; | ||
use opentelemetry::logs::LogError; | ||
|
@@ -72,13 +73,13 @@ fn init_logs() -> Result<opentelemetry_sdk::logs::Logger, LogError> { | |
const LEMONS_KEY: Key = Key::from_static_str("lemons"); | ||
const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another"); | ||
|
||
static COMMON_ATTRIBUTES: Lazy<[KeyValue; 4]> = Lazy::new(|| { | ||
[ | ||
static COMMON_ATTRIBUTES: Lazy<AttributeSet> = Lazy::new(|| { | ||
AttributeSet::from(&[ | ||
LEMONS_KEY.i64(10), | ||
KeyValue::new("A", "1"), | ||
KeyValue::new("B", "2"), | ||
KeyValue::new("C", "3"), | ||
] | ||
]) | ||
}); | ||
|
||
#[tokio::main] | ||
|
@@ -109,11 +110,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | |
.init(); | ||
|
||
meter.register_callback(&[gauge.as_any()], move |observer| { | ||
observer.observe_f64(&gauge, 1.0, COMMON_ATTRIBUTES.as_ref()) | ||
observer.observe_f64(&gauge, 1.0, COMMON_ATTRIBUTES.clone()) | ||
})?; | ||
|
||
let histogram = meter.f64_histogram("ex.com.two").init(); | ||
histogram.record(5.5, COMMON_ATTRIBUTES.as_ref()); | ||
histogram.record(5.5, COMMON_ATTRIBUTES.clone()); | ||
|
||
tracer.in_span("operation", |cx| { | ||
let span = cx.span(); | ||
|
@@ -131,7 +132,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | |
|
||
span.add_event("Sub span event", vec![]); | ||
|
||
histogram.record(1.3, &[]); | ||
histogram.record(1.3, AttributeSet::default()); | ||
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. not in this PR, but just realized that this is somewhat awkward having to pass empty/default when no dimensions! 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. This is kind of an artifact of the original state of the PR being backward incompatible. In the current state of the code |
||
}); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,14 @@ | |
//! [Prometheus]: https://prometheus.io | ||
//! | ||
//! ``` | ||
//! use opentelemetry::{metrics::MeterProvider, KeyValue}; | ||
//! use opentelemetry::{attributes::AttributeSet, metrics::MeterProvider, KeyValue}; | ||
//! use opentelemetry_sdk::metrics::SdkMeterProvider; | ||
//! use prometheus::{Encoder, TextEncoder}; | ||
//! | ||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
//! | ||
//! // create a new prometheus registry | ||
//! use opentelemetry::attributes::AttributeSet; | ||
//! let registry = prometheus::Registry::new(); | ||
//! | ||
//! // configure OpenTelemetry to use this registry | ||
|
@@ -31,8 +32,10 @@ | |
//! .with_description("Records values") | ||
//! .init(); | ||
//! | ||
//! counter.add(100, &[KeyValue::new("key", "value")]); | ||
//! histogram.record(100, &[KeyValue::new("key", "value")]); | ||
//! let attributes = AttributeSet::from(&[KeyValue::new("key", "value")]); | ||
//! | ||
//! counter.add(100, attributes.clone()); | ||
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. i am not sure how common is it to have 2 instruments used with same attributes.. maybe better to stick with existing examples only. (Also counter's functionality is already provided by histogram, so this example of showing both could be confusing! But that is unrelated to this PR) 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. Since this example uses a |
||
//! histogram.record(100, attributes); | ||
//! | ||
//! // Encode data as text or protobuf | ||
//! let encoder = TextEncoder::new(); | ||
|
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.
Small nit, but the other non-signal-prefixed types are exported at the
opentelemetry
root, could be more consistent to expose this asopentelemetry::AttributeSet;
?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.
Moved
AttributeSet
reference toopentelemetry
root