Skip to content

Commit 9f0df17

Browse files
committed
PR feedback
1 parent 9df046d commit 9f0df17

File tree

6 files changed

+52
-60
lines changed

6 files changed

+52
-60
lines changed

rust-runtime/aws-smithy-observability-otel/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mod tests {
4646

4747
// Set the global TelemetryProvider and then get it back out
4848
let _ = set_telemetry_provider(sdk_tp);
49-
let global_tp = get_telemetry_provider();
49+
let global_tp = get_telemetry_provider().unwrap();
5050

5151
// Create an instrument and record a value
5252
let global_meter = global_tp

rust-runtime/aws-smithy-observability-otel/src/meter.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
use std::fmt::Debug;
99
use std::ops::Deref;
10+
use std::sync::Arc;
1011

1112
use crate::attributes::kv_from_option_attr;
1213
use aws_smithy_observability::attributes::{Attributes, Context};
@@ -147,7 +148,7 @@ impl Meter for MeterWrap {
147148
callback: Box<dyn Fn(&dyn AsyncMeasure<Value = f64>) + Send + Sync>,
148149
units: Option<String>,
149150
description: Option<String>,
150-
) -> Box<dyn AsyncMeasure<Value = f64>> {
151+
) -> Arc<dyn AsyncMeasure<Value = f64>> {
151152
let mut builder = self.f64_observable_gauge(name).with_callback(
152153
move |input: &dyn OtelAsyncInstrument<f64>| {
153154
callback(&AsyncInstrumentWrap(input));
@@ -162,15 +163,15 @@ impl Meter for MeterWrap {
162163
builder = builder.with_unit(u);
163164
}
164165

165-
Box::new(GaugeWrap(builder.init()))
166+
Arc::new(GaugeWrap(builder.init()))
166167
}
167168

168169
fn create_up_down_counter(
169170
&self,
170171
name: String,
171172
units: Option<String>,
172173
description: Option<String>,
173-
) -> Box<dyn UpDownCounter> {
174+
) -> Arc<dyn UpDownCounter> {
174175
let mut builder = self.i64_up_down_counter(name);
175176
if let Some(desc) = description {
176177
builder = builder.with_description(desc);
@@ -180,7 +181,7 @@ impl Meter for MeterWrap {
180181
builder = builder.with_unit(u);
181182
}
182183

183-
Box::new(UpDownCounterWrap(builder.init()))
184+
Arc::new(UpDownCounterWrap(builder.init()))
184185
}
185186

186187
fn create_async_up_down_counter(
@@ -189,7 +190,7 @@ impl Meter for MeterWrap {
189190
callback: Box<dyn Fn(&dyn AsyncMeasure<Value = i64>) + Send + Sync>,
190191
units: Option<String>,
191192
description: Option<String>,
192-
) -> Box<dyn AsyncMeasure<Value = i64>> {
193+
) -> Arc<dyn AsyncMeasure<Value = i64>> {
193194
let mut builder = self.i64_observable_up_down_counter(name).with_callback(
194195
move |input: &dyn OtelAsyncInstrument<i64>| {
195196
callback(&AsyncInstrumentWrap(input));
@@ -204,15 +205,15 @@ impl Meter for MeterWrap {
204205
builder = builder.with_unit(u);
205206
}
206207

207-
Box::new(AsyncUpDownCounterWrap(builder.init()))
208+
Arc::new(AsyncUpDownCounterWrap(builder.init()))
208209
}
209210

210211
fn create_monotonic_counter(
211212
&self,
212213
name: String,
213214
units: Option<String>,
214215
description: Option<String>,
215-
) -> Box<dyn MonotonicCounter> {
216+
) -> Arc<dyn MonotonicCounter> {
216217
let mut builder = self.u64_counter(name);
217218
if let Some(desc) = description {
218219
builder = builder.with_description(desc);
@@ -222,7 +223,7 @@ impl Meter for MeterWrap {
222223
builder = builder.with_unit(u);
223224
}
224225

225-
Box::new(MonotonicCounterWrap(builder.init()))
226+
Arc::new(MonotonicCounterWrap(builder.init()))
226227
}
227228

228229
fn create_async_monotonic_counter(
@@ -231,7 +232,7 @@ impl Meter for MeterWrap {
231232
callback: Box<dyn Fn(&dyn AsyncMeasure<Value = u64>) + Send + Sync>,
232233
units: Option<String>,
233234
description: Option<String>,
234-
) -> Box<dyn AsyncMeasure<Value = u64>> {
235+
) -> Arc<dyn AsyncMeasure<Value = u64>> {
235236
let mut builder = self.u64_observable_counter(name).with_callback(
236237
move |input: &dyn OtelAsyncInstrument<u64>| {
237238
callback(&AsyncInstrumentWrap(input));
@@ -246,15 +247,15 @@ impl Meter for MeterWrap {
246247
builder = builder.with_unit(u);
247248
}
248249

249-
Box::new(AsyncMonotonicCounterWrap(builder.init()))
250+
Arc::new(AsyncMonotonicCounterWrap(builder.init()))
250251
}
251252

252253
fn create_histogram(
253254
&self,
254255
name: String,
255256
units: Option<String>,
256257
description: Option<String>,
257-
) -> Box<dyn Histogram> {
258+
) -> Arc<dyn Histogram> {
258259
let mut builder = self.f64_histogram(name);
259260
if let Some(desc) = description {
260261
builder = builder.with_description(desc);
@@ -264,7 +265,7 @@ impl Meter for MeterWrap {
264265
builder = builder.with_unit(u);
265266
}
266267

267-
Box::new(HistogramWrap(builder.init()))
268+
Arc::new(HistogramWrap(builder.init()))
268269
}
269270
}
270271

@@ -301,8 +302,8 @@ impl AwsSdkOtelMeterProvider {
301302
}
302303

303304
impl ProvideMeter for AwsSdkOtelMeterProvider {
304-
fn get_meter(&self, scope: &'static str, _attributes: Option<&Attributes>) -> Box<dyn Meter> {
305-
Box::new(MeterWrap(self.meter_provider.meter(scope)))
305+
fn get_meter(&self, scope: &'static str, _attributes: Option<&Attributes>) -> Arc<dyn Meter> {
306+
Arc::new(MeterWrap(self.meter_provider.meter(scope)))
306307
}
307308

308309
fn as_any(&self) -> &dyn std::any::Any {

rust-runtime/aws-smithy-observability/src/attributes.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ pub enum AttributeValue {
2525

2626
/// Structured telemetry metadata.
2727
#[non_exhaustive]
28-
#[derive(Clone)]
28+
#[derive(Clone, Default)]
2929
pub struct Attributes {
3030
attrs: HashMap<String, AttributeValue>,
3131
}
3232

3333
impl Attributes {
3434
/// Create a new empty instance of [Attributes].
3535
pub fn new() -> Self {
36-
Self {
37-
attrs: HashMap::new(),
38-
}
36+
Self::default()
3937
}
4038

4139
/// Set an attribute.
@@ -59,12 +57,6 @@ impl Attributes {
5957
}
6058
}
6159

62-
impl Default for Attributes {
63-
fn default() -> Self {
64-
Self::new()
65-
}
66-
}
67-
6860
/// Delineates a logical scope that has some beginning and end
6961
/// (e.g. a function or block of code).
7062
pub trait Scope {

rust-runtime/aws-smithy-observability/src/global.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,16 @@ pub fn set_telemetry_provider(new_provider: TelemetryProvider) {
3333
let _ = mem::replace(&mut *old_provider, new_global_provider);
3434
}
3535

36-
/// Get an [Arc] reference to the current global [TelemetryProvider].
37-
///
38-
/// This can panic if called when another thread is calling [set_telemetry_provider].
39-
pub fn get_telemetry_provider() -> Arc<TelemetryProvider> {
40-
// TODO(smithyObservability): would probably be nicer to return a Result here, but the Guard held by the error from
36+
/// Get an [Arc] reference to the current global [TelemetryProvider]. [None] is returned if the [RwLock] containing
37+
/// the [GlobalTelemetryProvider] is poisoned or is currently locked by a writer.
38+
pub fn get_telemetry_provider() -> Option<Arc<TelemetryProvider>> {
39+
// TODO(smithyObservability): would probably make more sense to return a Result rather than an Option here, but the Guard held by the error from
4140
// .try_read is not Send so I struggled to build an ObservabilityError from it
42-
GLOBAL_TELEMETRY_PROVIDER
43-
.try_read()
44-
.expect("GLOBAL_TELEMETRY_PROVIDER RwLock Poisoned")
45-
.telemetry_provider()
46-
.clone()
41+
if let Ok(tp) = GLOBAL_TELEMETRY_PROVIDER.try_read() {
42+
Some(tp.telemetry_provider().clone())
43+
} else {
44+
None
45+
}
4746
}
4847

4948
#[cfg(test)]
@@ -66,7 +65,7 @@ mod tests {
6665
#[test]
6766
#[serial]
6867
fn can_get_global_telemetry_provider() {
69-
let curr_provider = get_telemetry_provider();
68+
let curr_provider = get_telemetry_provider().unwrap();
7069

7170
// Use the global provider to create an instrument and record a value with it
7271
let curr_mp = curr_provider.meter_provider();

rust-runtime/aws-smithy-observability/src/meter.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
//! real time.
88
99
use crate::attributes::{Attributes, Context};
10-
use std::fmt::Debug;
10+
use std::{fmt::Debug, sync::Arc};
1111

1212
/// Provides named instances of [Meter].
1313
pub trait ProvideMeter: Send + Sync + Debug {
1414
/// Get or create a named [Meter].
15-
fn get_meter(&self, scope: &'static str, attributes: Option<&Attributes>) -> Box<dyn Meter>;
15+
fn get_meter(&self, scope: &'static str, attributes: Option<&Attributes>) -> Arc<dyn Meter>;
1616

1717
/// Foo
1818
fn as_any(&self) -> &dyn std::any::Any;
@@ -28,15 +28,15 @@ pub trait Meter: Send + Sync + Debug {
2828
callback: Box<dyn Fn(&dyn AsyncMeasure<Value = f64>) + Send + Sync>,
2929
units: Option<String>,
3030
description: Option<String>,
31-
) -> Box<dyn AsyncMeasure<Value = f64>>;
31+
) -> Arc<dyn AsyncMeasure<Value = f64>>;
3232

3333
/// Create a new [UpDownCounter].
3434
fn create_up_down_counter(
3535
&self,
3636
name: String,
3737
units: Option<String>,
3838
description: Option<String>,
39-
) -> Box<dyn UpDownCounter>;
39+
) -> Arc<dyn UpDownCounter>;
4040

4141
/// Create a new AsyncUpDownCounter.
4242
#[allow(clippy::type_complexity)]
@@ -46,15 +46,15 @@ pub trait Meter: Send + Sync + Debug {
4646
callback: Box<dyn Fn(&dyn AsyncMeasure<Value = i64>) + Send + Sync>,
4747
units: Option<String>,
4848
description: Option<String>,
49-
) -> Box<dyn AsyncMeasure<Value = i64>>;
49+
) -> Arc<dyn AsyncMeasure<Value = i64>>;
5050

5151
/// Create a new [MonotonicCounter].
5252
fn create_monotonic_counter(
5353
&self,
5454
name: String,
5555
units: Option<String>,
5656
description: Option<String>,
57-
) -> Box<dyn MonotonicCounter>;
57+
) -> Arc<dyn MonotonicCounter>;
5858

5959
/// Create a new AsyncMonotonicCounter.
6060
#[allow(clippy::type_complexity)]
@@ -64,15 +64,15 @@ pub trait Meter: Send + Sync + Debug {
6464
callback: Box<dyn Fn(&dyn AsyncMeasure<Value = u64>) + Send + Sync>,
6565
units: Option<String>,
6666
description: Option<String>,
67-
) -> Box<dyn AsyncMeasure<Value = u64>>;
67+
) -> Arc<dyn AsyncMeasure<Value = u64>>;
6868

6969
/// Create a new [Histogram].
7070
fn create_histogram(
7171
&self,
7272
name: String,
7373
units: Option<String>,
7474
description: Option<String>,
75-
) -> Box<dyn Histogram>;
75+
) -> Arc<dyn Histogram>;
7676
}
7777

7878
/// Collects a set of events with an event count and sum for all events.
@@ -106,6 +106,6 @@ pub trait AsyncMeasure: Send + Sync + Debug {
106106
context: Option<&dyn Context>,
107107
);
108108

109-
/// Stop recording , unregister callback.
109+
/// Stop recording, unregister callback.
110110
fn stop(&self);
111111
}

rust-runtime/aws-smithy-observability/src/noop.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
//! An noop implementation of the Meter traits
77
8-
use std::fmt::Debug;
98
use std::marker::PhantomData;
9+
use std::{fmt::Debug, sync::Arc};
1010

1111
use crate::{
1212
attributes::{Attributes, Context},
@@ -16,8 +16,8 @@ use crate::{
1616
#[derive(Debug)]
1717
pub(crate) struct NoopMeterProvider;
1818
impl ProvideMeter for NoopMeterProvider {
19-
fn get_meter(&self, _scope: &'static str, _attributes: Option<&Attributes>) -> Box<dyn Meter> {
20-
Box::new(NoopMeter)
19+
fn get_meter(&self, _scope: &'static str, _attributes: Option<&Attributes>) -> Arc<dyn Meter> {
20+
Arc::new(NoopMeter)
2121
}
2222

2323
fn as_any(&self) -> &dyn std::any::Any {
@@ -34,17 +34,17 @@ impl Meter for NoopMeter {
3434
_callback: Box<dyn Fn(&dyn AsyncMeasure<Value = f64>) + Send + Sync>,
3535
_units: Option<String>,
3636
_description: Option<String>,
37-
) -> Box<dyn AsyncMeasure<Value = f64>> {
38-
Box::new(NoopAsyncMeasurement(PhantomData::<f64>))
37+
) -> Arc<dyn AsyncMeasure<Value = f64>> {
38+
Arc::new(NoopAsyncMeasurement(PhantomData::<f64>))
3939
}
4040

4141
fn create_up_down_counter(
4242
&self,
4343
_name: String,
4444
_units: Option<String>,
4545
_description: Option<String>,
46-
) -> Box<dyn UpDownCounter> {
47-
Box::new(NoopUpDownCounter)
46+
) -> Arc<dyn UpDownCounter> {
47+
Arc::new(NoopUpDownCounter)
4848
}
4949

5050
fn create_async_up_down_counter(
@@ -53,17 +53,17 @@ impl Meter for NoopMeter {
5353
_callback: Box<dyn Fn(&dyn AsyncMeasure<Value = i64>) + Send + Sync>,
5454
_units: Option<String>,
5555
_description: Option<String>,
56-
) -> Box<dyn AsyncMeasure<Value = i64>> {
57-
Box::new(NoopAsyncMeasurement(PhantomData::<i64>))
56+
) -> Arc<dyn AsyncMeasure<Value = i64>> {
57+
Arc::new(NoopAsyncMeasurement(PhantomData::<i64>))
5858
}
5959

6060
fn create_monotonic_counter(
6161
&self,
6262
_name: String,
6363
_units: Option<String>,
6464
_description: Option<String>,
65-
) -> Box<dyn MonotonicCounter> {
66-
Box::new(NoopMonotonicCounter)
65+
) -> Arc<dyn MonotonicCounter> {
66+
Arc::new(NoopMonotonicCounter)
6767
}
6868

6969
fn create_async_monotonic_counter(
@@ -72,17 +72,17 @@ impl Meter for NoopMeter {
7272
_callback: Box<dyn Fn(&dyn AsyncMeasure<Value = u64>) + Send + Sync>,
7373
_units: Option<String>,
7474
_description: Option<String>,
75-
) -> Box<dyn AsyncMeasure<Value = u64>> {
76-
Box::new(NoopAsyncMeasurement(PhantomData::<u64>))
75+
) -> Arc<dyn AsyncMeasure<Value = u64>> {
76+
Arc::new(NoopAsyncMeasurement(PhantomData::<u64>))
7777
}
7878

7979
fn create_histogram(
8080
&self,
8181
_name: String,
8282
_units: Option<String>,
8383
_description: Option<String>,
84-
) -> Box<dyn Histogram> {
85-
Box::new(NoopHistogram)
84+
) -> Arc<dyn Histogram> {
85+
Arc::new(NoopHistogram)
8686
}
8787
}
8888

0 commit comments

Comments
 (0)