Skip to content

Commit

Permalink
Revert "Allow precreation of AttributeSets for metrics (#1421)" (#1512)
Browse files Browse the repository at this point in the history
  • Loading branch information
KallDrexx committed Feb 6, 2024
1 parent dd4c13b commit aba0af5
Show file tree
Hide file tree
Showing 41 changed files with 503 additions and 795 deletions.
16 changes: 9 additions & 7 deletions examples/metrics-basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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 @@ -53,10 +52,11 @@ 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,10 +84,11 @@ 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 @@ -141,10 +142,11 @@ 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: 4 additions & 5 deletions opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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 @@ -63,13 +62,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<AttributeSet> = Lazy::new(|| {
AttributeSet::from(&[
static COMMON_ATTRIBUTES: Lazy<[KeyValue; 4]> = Lazy::new(|| {
[
LEMONS_KEY.i64(10),
KeyValue::new("A", "1"),
KeyValue::new("B", "2"),
KeyValue::new("C", "3"),
])
]
});

#[tokio::main]
Expand Down Expand Up @@ -105,7 +104,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.clone());
histogram.record(5.5, COMMON_ATTRIBUTES.as_ref());

global::shutdown_tracer_provider();
global::shutdown_logger_provider();
Expand Down
11 changes: 5 additions & 6 deletions opentelemetry-otlp/examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 @@ -73,13 +72,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<AttributeSet> = Lazy::new(|| {
AttributeSet::from(&[
static COMMON_ATTRIBUTES: Lazy<[KeyValue; 4]> = Lazy::new(|| {
[
LEMONS_KEY.i64(10),
KeyValue::new("A", "1"),
KeyValue::new("B", "2"),
KeyValue::new("C", "3"),
])
]
});

#[tokio::main]
Expand Down Expand Up @@ -110,11 +109,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.clone())
observer.observe_f64(&gauge, 1.0, COMMON_ATTRIBUTES.as_ref())
})?;

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

tracer.in_span("operation", |cx| {
let span = cx.span();
Expand Down
10 changes: 4 additions & 6 deletions opentelemetry-prometheus/examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 @@ -15,8 +14,7 @@ use std::convert::Infallible;
use std::sync::Arc;
use std::time::SystemTime;

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

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

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

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

Response::builder()
.status(200)
Expand All @@ -55,7 +53,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: 3 additions & 6 deletions opentelemetry-prometheus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
//! [Prometheus]: https://prometheus.io
//!
//! ```
//! use opentelemetry::{AttributeSet, metrics::MeterProvider, KeyValue};
//! use opentelemetry::{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 @@ -32,10 +31,8 @@
//! .with_description("Records values")
//! .init();
//!
//! let attributes = AttributeSet::from(&[KeyValue::new("key", "value")]);
//!
//! counter.add(100, attributes.clone());
//! histogram.record(100, attributes);
//! counter.add(100, &[KeyValue::new("key", "value")]);
//! histogram.record(100, &[KeyValue::new("key", "value")]);
//!
//! // Encode data as text or protobuf
//! let encoder = TextEncoder::new();
Expand Down
Loading

0 comments on commit aba0af5

Please sign in to comment.