Skip to content

Commit

Permalink
Allow precreation of AttributeSets for metrics (#1421)
Browse files Browse the repository at this point in the history
  • Loading branch information
KallDrexx authored Jan 24, 2024
1 parent 5b456dd commit 16fd1ab
Show file tree
Hide file tree
Showing 41 changed files with 795 additions and 503 deletions.
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());
//! histogram.record(100, attributes);
//!
//! // Encode data as text or protobuf
//! let encoder = TextEncoder::new();
Expand Down
Loading

0 comments on commit 16fd1ab

Please sign in to comment.