Skip to content

Commit

Permalink
Add Send + Sync + Debug supertraits to obs traits
Browse files Browse the repository at this point in the history
  • Loading branch information
landonxjames committed Nov 18, 2024
1 parent a5dd8db commit fe50a9d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
allowed_external_types = [
"aws_smithy_observability::error::ObservabilityError",
"aws_smithy_observability::meter::AsyncMeasurement",
"aws_smithy_observability::meter::Histogram",
"aws_smithy_observability::meter::Meter",
Expand Down
16 changes: 16 additions & 0 deletions rust-runtime/aws-smithy-observability-otel/src/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

//! OpenTelemetry based implementations of the Smithy Observability Meter traits.

use std::fmt::Debug;
use std::ops::Deref;

use crate::attributes::kv_from_option_attr;
Expand All @@ -22,27 +23,31 @@ use opentelemetry::metrics::{
};
use opentelemetry_sdk::metrics::SdkMeterProvider as OtelSdkMeterProvider;

#[derive(Debug)]
struct UpDownCounterWrap(OtelUpDownCounter<i64>);
impl UpDownCounter for UpDownCounterWrap {
fn add(&self, value: i64, attributes: Option<&Attributes>, _context: Option<&dyn Context>) {
self.0.add(value, &kv_from_option_attr(attributes));
}
}

#[derive(Debug)]
struct HistogramWrap(OtelHistogram<f64>);
impl Histogram for HistogramWrap {
fn record(&self, value: f64, attributes: Option<&Attributes>, _context: Option<&dyn Context>) {
self.0.record(value, &kv_from_option_attr(attributes));
}
}

#[derive(Debug)]
struct MonotonicCounterWrap(OtelCounter<u64>);
impl MonotonicCounter for MonotonicCounterWrap {
fn add(&self, value: u64, attributes: Option<&Attributes>, _context: Option<&dyn Context>) {
self.0.add(value, &kv_from_option_attr(attributes));
}
}

#[derive(Debug)]
struct GaugeWrap(OtelObservableGauge<f64>);
impl AsyncMeasurement for GaugeWrap {
type Value = f64;
Expand All @@ -61,6 +66,7 @@ impl AsyncMeasurement for GaugeWrap {
fn stop(&self) {}
}

#[derive(Debug)]
struct AsyncUpDownCounterWrap(OtelObservableUpDownCounter<i64>);
impl AsyncMeasurement for AsyncUpDownCounterWrap {
type Value = i64;
Expand All @@ -79,6 +85,7 @@ impl AsyncMeasurement for AsyncUpDownCounterWrap {
fn stop(&self) {}
}

#[derive(Debug)]
struct AsyncMonotonicCounterWrap(OtelObservableCounter<u64>);
impl AsyncMeasurement for AsyncMonotonicCounterWrap {
type Value = u64;
Expand Down Expand Up @@ -115,6 +122,15 @@ impl<T> AsyncMeasurement for AsyncInstrumentWrap<'_, T> {
fn stop(&self) {}
}

// The OtelAsyncInstrument trait does not have Debug as a supertrait, so we impl a minimal version
// for our wrapper struct
impl<T> Debug for AsyncInstrumentWrap<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("AsyncInstrumentWrap").finish()
}
}

#[derive(Debug)]
struct MeterWrap(OtelMeter);
impl Deref for MeterWrap {
type Target = OtelMeter;
Expand Down
13 changes: 7 additions & 6 deletions rust-runtime/aws-smithy-observability/src/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
//! real time.

use crate::attributes::{Attributes, Context};
use std::fmt::Debug;

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

Expand All @@ -18,7 +19,7 @@ pub trait MeterProvider {
}

/// The entry point to creating instruments. A grouping of related metrics.
pub trait Meter {
pub trait Meter: Send + Sync + Debug {
/// Create a new Gauge.
#[allow(clippy::type_complexity)]
fn create_gauge(
Expand Down Expand Up @@ -75,25 +76,25 @@ pub trait Meter {
}

/// Collects a set of events with an event count and sum for all events.
pub trait Histogram {
pub trait Histogram: Send + Sync + Debug {
/// Record a value.
fn record(&self, value: f64, attributes: Option<&Attributes>, context: Option<&dyn Context>);
}

/// A counter that monotonically increases.
pub trait MonotonicCounter {
pub trait MonotonicCounter: Send + Sync + Debug {
/// Increment a counter by a fixed amount.
fn add(&self, value: u64, attributes: Option<&Attributes>, context: Option<&dyn Context>);
}

/// A counter that can increase or decrease.
pub trait UpDownCounter {
pub trait UpDownCounter: Send + Sync + Debug {
/// Increment or decrement a counter by a fixed amount.
fn add(&self, value: i64, attributes: Option<&Attributes>, context: Option<&dyn Context>);
}

/// A measurement that can be taken asynchronously.
pub trait AsyncMeasurement {
pub trait AsyncMeasurement: Send + Sync + Debug {
/// The type recorded by the measurement.
type Value;

Expand Down
11 changes: 9 additions & 2 deletions rust-runtime/aws-smithy-observability/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

//! An noop implementation of the Meter traits

use std::fmt::Debug;
use std::marker::PhantomData;

use crate::{
attributes::{Attributes, Context},
meter::{AsyncMeasurement, Histogram, Meter, MeterProvider, MonotonicCounter, UpDownCounter},
};

#[derive(Debug)]
pub(crate) struct NoopMeterProvider;
impl MeterProvider for NoopMeterProvider {
fn get_meter(&self, _scope: &'static str, _attributes: Option<&Attributes>) -> Box<dyn Meter> {
Expand All @@ -23,6 +25,7 @@ impl MeterProvider for NoopMeterProvider {
}
}

#[derive(Debug)]
pub(crate) struct NoopMeter;
impl Meter for NoopMeter {
fn create_gauge(
Expand Down Expand Up @@ -83,25 +86,29 @@ impl Meter for NoopMeter {
}
}

struct NoopAsyncMeasurement<T>(PhantomData<T>);
impl<T> AsyncMeasurement for NoopAsyncMeasurement<T> {
#[derive(Debug)]
struct NoopAsyncMeasurement<T: Send + Sync + Debug>(PhantomData<T>);
impl<T: Send + Sync + Debug> AsyncMeasurement for NoopAsyncMeasurement<T> {
type Value = T;

fn record(&self, _value: T, _attributes: Option<&Attributes>, _context: Option<&dyn Context>) {}

fn stop(&self) {}
}

#[derive(Debug)]
struct NoopUpDownCounter;
impl UpDownCounter for NoopUpDownCounter {
fn add(&self, _value: i64, _attributes: Option<&Attributes>, _context: Option<&dyn Context>) {}
}

#[derive(Debug)]
struct NoopMonotonicCounter;
impl MonotonicCounter for NoopMonotonicCounter {
fn add(&self, _value: u64, _attributes: Option<&Attributes>, _context: Option<&dyn Context>) {}
}

#[derive(Debug)]
struct NoopHistogram;
impl Histogram for NoopHistogram {
fn record(
Expand Down

0 comments on commit fe50a9d

Please sign in to comment.