Skip to content
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

Merged
merged 37 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6f42c3a
Move attribute set to root crate
Nov 30, 2023
92977cd
Attribute set passed in as argument
Nov 30, 2023
9f9905b
Removed idea files
Nov 30, 2023
8fe79b9
From trait utilizing iterators instead of reference slices
Nov 30, 2023
4b61676
Initial mod comment
Nov 30, 2023
d47e9df
Update benchmarks
Dec 1, 2023
3cf8b13
Add benchmark and stress test
Dec 1, 2023
1cb67fc
counter compile error
Dec 1, 2023
dbf2c16
Merge branch 'main' into premade_attribute_sets
Dec 1, 2023
3160dad
Unignore spatial aggregation test as requested
Dec 1, 2023
5ad5b7b
Fmt fix
Dec 1, 2023
c0e9878
Fixed compile errors after merge
Dec 1, 2023
c442a1a
Merge branch 'main' into premade_attribute_sets
KallDrexx Dec 5, 2023
5b2ef1a
Counters and histograms can now have any value that can be converted …
Dec 5, 2023
26afa6e
Added attribute set test coverage
Dec 5, 2023
8417984
Removed unused `from_key_values()` function
Dec 5, 2023
9e85ddc
Added more value types for better equality checks
Dec 5, 2023
4f4d4de
Merge branch 'main' into premade_attribute_sets
KallDrexx Dec 6, 2023
747c40e
Merge branch 'main' into premade_attribute_sets
Dec 13, 2023
46994cb
Post merge clippy
Dec 13, 2023
83f10d3
Take shared reference iterators, one less clone
Dec 20, 2023
6b6e9e0
Removed unneeded `AttributeSet::from` calls
Dec 20, 2023
377e8b0
revert of some minor changes to lower diff volume
Dec 20, 2023
7869fa7
Fixed doc tests
Dec 20, 2023
70cbdbb
missed doctest
Dec 20, 2023
18a001f
missed fmt
Dec 20, 2023
345a0e7
Merge branch 'main' into premade_attribute_sets
Jan 5, 2024
a37bf5a
Allow owned and borrowed key value iterators to create attribute sets
Jan 8, 2024
ae482c3
Moved `AttributeSet` reference to opentelemetry root
Jan 8, 2024
440a5bc
Added missing docs
Jan 8, 2024
a34194d
Fix doctests pointing to changed module location
Jan 8, 2024
4d5dcdd
Add changelog entry
Jan 23, 2024
0a1899a
Merge branch 'main' into premade_attribute_sets
KallDrexx Jan 23, 2024
892b6f1
Minor nits
Jan 23, 2024
d2f7ad0
Histogram is backward compatible
Jan 24, 2024
a3b1cf4
Gauges are compat, observables are not
Jan 24, 2024
ef330e1
Update opentelemetry/CHANGELOG.md
KallDrexx Jan 24, 2024
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
16 changes: 7 additions & 9 deletions examples/metrics-basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use opentelemetry::metrics::Unit;
use opentelemetry::AttributeSet;
use opentelemetry::{metrics::MeterProvider as _, KeyValue};
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
use opentelemetry_sdk::{runtime, Resource};
Expand Down Expand Up @@ -52,11 +53,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
observer.observe_u64(
&observable_counter,
100,
[
AttributeSet::from(&[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
]),
)
})?;

Expand Down Expand Up @@ -84,11 +84,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
observer.observe_i64(
&observable_up_down_counter,
100,
[
AttributeSet::from(&[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
]),
)
})?;

Expand Down Expand Up @@ -142,11 +141,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
observer.observe_f64(
&observable_gauge,
1.0,
[
AttributeSet::from(&[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
]),
)
})?;

Expand Down
9 changes: 5 additions & 4 deletions opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use opentelemetry_sdk::metrics as sdkmetrics;
use opentelemetry_sdk::resource;
use opentelemetry_sdk::trace as sdktrace;

use opentelemetry::AttributeSet;
use std::error::Error;
use tracing::info;
use tracing_subscriber::prelude::*;
Expand Down Expand Up @@ -62,13 +63,13 @@ fn init_metrics() -> metrics::Result<sdkmetrics::SdkMeterProvider> {
const LEMONS_KEY: Key = Key::from_static_str("ex.com/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]
Expand Down Expand Up @@ -104,7 +105,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
info!(target: "my-target", "hello from {}. My price is {}", "apple", 1.99);

let histogram = meter.f64_histogram("ex.com.two").init();
histogram.record(5.5, COMMON_ATTRIBUTES.as_ref());
histogram.record(5.5, COMMON_ATTRIBUTES.clone());

global::shutdown_tracer_provider();
global::shutdown_logger_provider();
Expand Down
11 changes: 6 additions & 5 deletions opentelemetry-otlp/examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use opentelemetry::global;
use opentelemetry::global::{logger_provider, shutdown_logger_provider, shutdown_tracer_provider};
use opentelemetry::logs::LogError;
use opentelemetry::trace::TraceError;
use opentelemetry::AttributeSet;
use opentelemetry::{
metrics,
trace::{TraceContextExt, Tracer},
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 6 additions & 4 deletions opentelemetry-prometheus/examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use hyper::{
Body, Method, Request, Response, Server,
};
use once_cell::sync::Lazy;
use opentelemetry::AttributeSet;
use opentelemetry::{
metrics::{Counter, Histogram, MeterProvider as _, Unit},
KeyValue,
Expand All @@ -14,7 +15,8 @@ use std::convert::Infallible;
use std::sync::Arc;
use std::time::SystemTime;

static HANDLER_ALL: Lazy<[KeyValue; 1]> = Lazy::new(|| [KeyValue::new("handler", "all")]);
static HANDLER_ALL: Lazy<AttributeSet> =
Lazy::new(|| AttributeSet::from(&[KeyValue::new("handler", "all")]));

async fn serve_req(
req: Request<Body>,
Expand All @@ -23,7 +25,7 @@ async fn serve_req(
println!("Receiving request at path {}", req.uri());
let request_start = SystemTime::now();

state.http_counter.add(1, HANDLER_ALL.as_ref());
state.http_counter.add(1, HANDLER_ALL.clone());

let response = match (req.method(), req.uri().path()) {
(&Method::GET, "/metrics") => {
Expand All @@ -33,7 +35,7 @@ async fn serve_req(
encoder.encode(&metric_families, &mut buffer).unwrap();
state
.http_body_gauge
.record(buffer.len() as u64, HANDLER_ALL.as_ref());
.record(buffer.len() as u64, HANDLER_ALL.clone());

Response::builder()
.status(200)
Expand All @@ -53,7 +55,7 @@ async fn serve_req(

state.http_req_histogram.record(
request_start.elapsed().map_or(0.0, |d| d.as_secs_f64()),
&[],
AttributeSet::default(),
);
Ok(response)
}
Expand Down
9 changes: 6 additions & 3 deletions opentelemetry-prometheus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
//! [Prometheus]: https://prometheus.io
//!
//! ```
//! use opentelemetry::{metrics::MeterProvider, KeyValue};
//! use opentelemetry::{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::AttributeSet;
//! let registry = prometheus::Registry::new();
//!
//! // configure OpenTelemetry to use this registry
Expand All @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The 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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this example uses a histogram, and histograms couldn't be updated to take a Into<AttributeSet> either, the precreated attribute set is required here for at least that one.

//! histogram.record(100, attributes);
//!
//! // Encode data as text or protobuf
//! let encoder = TextEncoder::new();
Expand Down
Loading
Loading