Skip to content

Commit a08c640

Browse files
authored
Remove Context from sync instruments (#1076)
1 parent b29fa99 commit a08c640

File tree

21 files changed

+166
-203
lines changed

21 files changed

+166
-203
lines changed

examples/metrics-basic/src/main.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use opentelemetry_api::metrics::Unit;
2-
use opentelemetry_api::{metrics::MeterProvider as _, Context, KeyValue};
2+
use opentelemetry_api::{metrics::MeterProvider as _, KeyValue};
33
use opentelemetry_sdk::metrics::{MeterProvider, PeriodicReader};
44
use opentelemetry_sdk::{runtime, Resource};
55
use std::error::Error;
@@ -29,7 +29,6 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
2929

3030
// Record measurements using the Counter instrument.
3131
counter.add(
32-
&Context::current(),
3332
10,
3433
[
3534
KeyValue::new("mykey1", "myvalue1"),
@@ -62,7 +61,6 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
6261

6362
// Record measurements using the UpCounter instrument.
6463
updown_counter.add(
65-
&Context::current(),
6664
-10,
6765
[
6866
KeyValue::new("mykey1", "myvalue1"),
@@ -98,7 +96,6 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
9896

9997
// Record measurements using the histogram instrument.
10098
histogram.record(
101-
&Context::current(),
10299
10.5,
103100
[
104101
KeyValue::new("mykey1", "myvalue1"),

opentelemetry-api/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
### Removed
6+
7+
- Synchronous instruments no longer accepts `Context` while reporting
8+
measurements. [#1076](https://github.com/open-telemetry/opentelemetry-rust/pull/1076).
9+
510
### Fixed
611

712
- Fix `SpanRef::set_attributes` mutability requirement. [#1038](https://github.com/open-telemetry/opentelemetry-rust/pull/1038)

opentelemetry-api/src/global/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
//! # #[cfg(feature="metrics")]
9393
//! # {
9494
//! use opentelemetry_api::metrics::{Meter, noop::NoopMeterProvider};
95-
//! use opentelemetry_api::{global, Context, KeyValue};
95+
//! use opentelemetry_api::{global, KeyValue};
9696
//!
9797
//! fn init_meter() {
9898
//! let provider = NoopMeterProvider::new();
@@ -106,10 +106,9 @@
106106
//! // Then you can get a named tracer instance anywhere in your codebase.
107107
//! let meter = global::meter("my-component");
108108
//! let counter = meter.u64_counter("my_counter").init();
109-
//! let cx = Context::current();
110109
//!
111110
//! // record metrics
112-
//! counter.add(&cx, 1, &[KeyValue::new("mykey", "myvalue")]);
111+
//! counter.add(1, &[KeyValue::new("mykey", "myvalue")]);
113112
//! }
114113
//!
115114
//! // in main or other app start
@@ -123,17 +122,16 @@
123122
//! ```
124123
//! # #[cfg(feature="metrics")]
125124
//! # {
126-
//! use opentelemetry_api::{global, Context, KeyValue};
125+
//! use opentelemetry_api::{global, KeyValue};
127126
//!
128127
//! pub fn my_traced_library_function() {
129128
//! // End users of your library will configure their global meter provider
130129
//! // so you can use the global meter without any setup
131130
//! let tracer = global::meter("my-library-name");
132131
//! let counter = tracer.u64_counter("my_counter").init();
133-
//! let cx = Context::current();
134132
//!
135133
//! // record metrics
136-
//! counter.add(&cx, 1, &[KeyValue::new("mykey", "myvalue")]);
134+
//! counter.add(1, &[KeyValue::new("mykey", "myvalue")]);
137135
//! }
138136
//! # }
139137
//! ```

opentelemetry-api/src/metrics/instruments/counter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
metrics::{AsyncInstrument, AsyncInstrumentBuilder, InstrumentBuilder, MetricsError},
3-
Context, KeyValue,
3+
KeyValue,
44
};
55
use core::fmt;
66
use std::sync::Arc;
@@ -9,7 +9,7 @@ use std::{any::Any, convert::TryFrom};
99
/// An SDK implemented instrument that records increasing values.
1010
pub trait SyncCounter<T> {
1111
/// Records an increment to the counter.
12-
fn add(&self, cx: &Context, value: T, attributes: &[KeyValue]);
12+
fn add(&self, value: T, attributes: &[KeyValue]);
1313
}
1414

1515
/// An instrument that records increasing values.
@@ -32,8 +32,8 @@ impl<T> Counter<T> {
3232
}
3333

3434
/// Records an increment to the counter.
35-
pub fn add(&self, cx: &Context, value: T, attributes: &[KeyValue]) {
36-
self.0.add(cx, value, attributes)
35+
pub fn add(&self, value: T, attributes: &[KeyValue]) {
36+
self.0.add(value, attributes)
3737
}
3838
}
3939

opentelemetry-api/src/metrics/instruments/histogram.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
metrics::{InstrumentBuilder, MetricsError},
3-
Context, KeyValue,
3+
KeyValue,
44
};
55
use core::fmt;
66
use std::convert::TryFrom;
@@ -9,7 +9,7 @@ use std::sync::Arc;
99
/// An SDK implemented instrument that records a distribution of values.
1010
pub trait SyncHistogram<T> {
1111
/// Adds an additional value to the distribution.
12-
fn record(&self, cx: &Context, value: T, attributes: &[KeyValue]);
12+
fn record(&self, value: T, attributes: &[KeyValue]);
1313
}
1414

1515
/// An instrument that records a distribution of values.
@@ -32,8 +32,8 @@ impl<T> Histogram<T> {
3232
}
3333

3434
/// Adds an additional value to the distribution.
35-
pub fn record(&self, cx: &Context, value: T, attributes: &[KeyValue]) {
36-
self.0.record(cx, value, attributes)
35+
pub fn record(&self, value: T, attributes: &[KeyValue]) {
36+
self.0.record(value, attributes)
3737
}
3838
}
3939

opentelemetry-api/src/metrics/instruments/up_down_counter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
metrics::{InstrumentBuilder, MetricsError},
3-
Context, KeyValue,
3+
KeyValue,
44
};
55
use core::fmt;
66
use std::sync::Arc;
@@ -11,7 +11,7 @@ use super::{AsyncInstrument, AsyncInstrumentBuilder};
1111
/// An SDK implemented instrument that records increasing or decreasing values.
1212
pub trait SyncUpDownCounter<T> {
1313
/// Records an increment or decrement to the counter.
14-
fn add(&self, cx: &Context, value: T, attributes: &[KeyValue]);
14+
fn add(&self, value: T, attributes: &[KeyValue]);
1515
}
1616

1717
/// An instrument that records increasing or decreasing values.
@@ -37,8 +37,8 @@ impl<T> UpDownCounter<T> {
3737
}
3838

3939
/// Records an increment or decrement to the counter.
40-
pub fn add(&self, cx: &Context, value: T, attributes: &[KeyValue]) {
41-
self.0.add(cx, value, attributes)
40+
pub fn add(&self, value: T, attributes: &[KeyValue]) {
41+
self.0.add(value, attributes)
4242
}
4343
}
4444

opentelemetry-api/src/metrics/noop.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
AsyncInstrument, CallbackRegistration, InstrumentProvider, Meter, MeterProvider, Observer,
99
Result, SyncCounter, SyncHistogram, SyncUpDownCounter,
1010
},
11-
Context, KeyValue,
11+
KeyValue,
1212
};
1313
use std::{any::Any, borrow::Cow, sync::Arc};
1414

@@ -93,19 +93,19 @@ impl NoopSyncInstrument {
9393
}
9494

9595
impl<T> SyncCounter<T> for NoopSyncInstrument {
96-
fn add(&self, _cx: &Context, _value: T, _attributes: &[KeyValue]) {
96+
fn add(&self, _value: T, _attributes: &[KeyValue]) {
9797
// Ignored
9898
}
9999
}
100100

101101
impl<T> SyncUpDownCounter<T> for NoopSyncInstrument {
102-
fn add(&self, _cx: &Context, _value: T, _attributes: &[KeyValue]) {
102+
fn add(&self, _value: T, _attributes: &[KeyValue]) {
103103
// Ignored
104104
}
105105
}
106106

107107
impl<T> SyncHistogram<T> for NoopSyncInstrument {
108-
fn record(&self, _cx: &Context, _value: T, _attributes: &[KeyValue]) {
108+
fn record(&self, _value: T, _attributes: &[KeyValue]) {
109109
// Ignored
110110
}
111111
}

opentelemetry-otlp/examples/basic-otlp-http/src/main.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use opentelemetry_api::trace::TraceError;
44
use opentelemetry_api::{
55
metrics,
66
trace::{TraceContextExt, Tracer},
7-
Context, Key, KeyValue,
7+
Key, KeyValue,
88
};
99
use opentelemetry_otlp::WithExportConfig;
1010
use opentelemetry_sdk::metrics as sdkmetrics;
@@ -58,8 +58,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
5858
let meter = global::meter("ex.com/basic");
5959

6060
let histogram = meter.f64_histogram("ex.com.two").init();
61-
let cx = Context::new();
62-
histogram.record(&cx, 5.5, COMMON_ATTRIBUTES.as_ref());
61+
histogram.record(5.5, COMMON_ATTRIBUTES.as_ref());
6362

6463
tracer.in_span("operation", |cx| {
6564
let span = cx.span();

opentelemetry-otlp/examples/basic-otlp/src/main.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use opentelemetry_api::trace::TraceError;
55
use opentelemetry_api::{
66
metrics,
77
trace::{TraceContextExt, Tracer},
8-
Context, Key, KeyValue,
8+
Key, KeyValue,
99
};
1010
use opentelemetry_otlp::{ExportConfig, WithExportConfig};
1111
use opentelemetry_sdk::{metrics::MeterProvider, runtime, trace as sdktrace, Resource};
@@ -62,7 +62,6 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
6262
// execution.
6363
let _ = init_tracer()?;
6464
let meter_provider = init_metrics()?;
65-
let cx = Context::new();
6665

6766
let tracer = global::tracer("ex.com/basic");
6867
let meter = global::meter("ex.com/basic");
@@ -77,7 +76,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
7776
})?;
7877

7978
let histogram = meter.f64_histogram("ex.com.two").init();
80-
histogram.record(&cx, 5.5, COMMON_ATTRIBUTES.as_ref());
79+
histogram.record(5.5, COMMON_ATTRIBUTES.as_ref());
8180

8281
tracer.in_span("operation", |cx| {
8382
let span = cx.span();
@@ -93,7 +92,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
9392

9493
span.add_event("Sub span event", vec![]);
9594

96-
histogram.record(&cx, 1.3, &[]);
95+
histogram.record(1.3, &[]);
9796
});
9897
});
9998

opentelemetry-prometheus/examples/hyper.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hyper::{
66
use once_cell::sync::Lazy;
77
use opentelemetry_api::{
88
metrics::{Counter, Histogram, MeterProvider as _, Unit},
9-
Context, KeyValue,
9+
KeyValue,
1010
};
1111
use opentelemetry_sdk::metrics::MeterProvider;
1212
use prometheus::{Encoder, Registry, TextEncoder};
@@ -17,14 +17,13 @@ use std::time::SystemTime;
1717
static HANDLER_ALL: Lazy<[KeyValue; 1]> = Lazy::new(|| [KeyValue::new("handler", "all")]);
1818

1919
async fn serve_req(
20-
cx: Context,
2120
req: Request<Body>,
2221
state: Arc<AppState>,
2322
) -> Result<Response<Body>, hyper::Error> {
2423
println!("Receiving request at path {}", req.uri());
2524
let request_start = SystemTime::now();
2625

27-
state.http_counter.add(&cx, 1, HANDLER_ALL.as_ref());
26+
state.http_counter.add(1, HANDLER_ALL.as_ref());
2827

2928
let response = match (req.method(), req.uri().path()) {
3029
(&Method::GET, "/metrics") => {
@@ -34,7 +33,7 @@ async fn serve_req(
3433
encoder.encode(&metric_families, &mut buffer).unwrap();
3534
state
3635
.http_body_gauge
37-
.record(&cx, buffer.len() as u64, HANDLER_ALL.as_ref());
36+
.record(buffer.len() as u64, HANDLER_ALL.as_ref());
3837

3938
Response::builder()
4039
.status(200)
@@ -53,7 +52,6 @@ async fn serve_req(
5352
};
5453

5554
state.http_req_histogram.record(
56-
&cx,
5755
request_start.elapsed().map_or(0.0, |d| d.as_secs_f64()),
5856
&[],
5957
);
@@ -74,7 +72,6 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
7472
.with_registry(registry.clone())
7573
.build()?;
7674
let provider = MeterProvider::builder().with_reader(exporter).build();
77-
let cx = Context::new();
7875

7976
let meter = provider.meter("hyper-example");
8077
let state = Arc::new(AppState {
@@ -99,15 +96,10 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
9996
// incoming HTTP requests on said connection.
10097
let make_svc = make_service_fn(move |_conn| {
10198
let state = state.clone();
102-
let cx = cx.clone();
10399
// This is the `Service` that will handle the connection.
104100
// `service_fn` is a helper to convert a function that
105101
// returns a Response into a `Service`.
106-
async move {
107-
Ok::<_, Infallible>(service_fn(move |req| {
108-
serve_req(cx.clone(), req, state.clone())
109-
}))
110-
}
102+
async move { Ok::<_, Infallible>(service_fn(move |req| serve_req(req, state.clone()))) }
111103
});
112104

113105
let addr = ([127, 0, 0, 1], 3000).into();

opentelemetry-prometheus/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
//! [Prometheus]: https://prometheus.io
44
//!
55
//! ```
6-
//! use opentelemetry_api::{metrics::MeterProvider as _, Context, KeyValue};
6+
//! use opentelemetry_api::{metrics::MeterProvider as _, KeyValue};
77
//! use opentelemetry_sdk::metrics::MeterProvider;
88
//! use prometheus::{Encoder, TextEncoder};
99
//!
1010
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
11-
//! let cx = Context::current();
1211
//!
1312
//! // create a new prometheus registry
1413
//! let registry = prometheus::Registry::new();
@@ -32,8 +31,8 @@
3231
//! .with_description("Records values")
3332
//! .init();
3433
//!
35-
//! counter.add(&cx, 100, &[KeyValue::new("key", "value")]);
36-
//! histogram.record(&cx, 100, &[KeyValue::new("key", "value")]);
34+
//! counter.add(100, &[KeyValue::new("key", "value")]);
35+
//! histogram.record(100, &[KeyValue::new("key", "value")]);
3736
//!
3837
//! // Encode data as text or protobuf
3938
//! let encoder = TextEncoder::new();

0 commit comments

Comments
 (0)