From 91f0a1dac6dcdf8e8c613ac9fae882344aa5d6d4 Mon Sep 17 00:00:00 2001 From: Utkarsh Umesan Pillai <66651184+utpilla@users.noreply.github.com> Date: Mon, 9 Sep 2024 23:53:39 +0000 Subject: [PATCH] Update KeyValue --- .../examples/basic-otlp-http/src/main.rs | 4 +- .../examples/basic-otlp/src/main.rs | 4 +- .../tests/integration_test/tests/traces.rs | 2 +- opentelemetry-proto/src/transform/common.rs | 22 +-- opentelemetry-proto/src/transform/metrics.rs | 8 +- opentelemetry-sdk/benches/trace.rs | 44 ++--- opentelemetry-sdk/src/export/trace.rs | 2 +- opentelemetry-sdk/src/logs/log_emitter.rs | 2 +- opentelemetry-sdk/src/metrics/data/mod.rs | 8 +- opentelemetry-sdk/src/metrics/instrument.rs | 10 +- .../src/metrics/internal/aggregate.rs | 22 +-- .../metrics/internal/exponential_histogram.rs | 2 +- .../src/metrics/internal/histogram.rs | 2 +- .../src/metrics/internal/last_value.rs | 2 +- opentelemetry-sdk/src/metrics/internal/mod.rs | 11 +- .../src/metrics/internal/precomputed_sum.rs | 4 +- opentelemetry-sdk/src/metrics/internal/sum.rs | 2 +- .../src/metrics/meter_provider.rs | 2 +- opentelemetry-sdk/src/metrics/mod.rs | 16 +- opentelemetry-sdk/src/metrics/pipeline.rs | 7 +- opentelemetry-sdk/src/propagation/baggage.rs | 20 +-- opentelemetry-sdk/src/resource/mod.rs | 14 +- opentelemetry-sdk/src/trace/mod.rs | 2 +- opentelemetry-sdk/src/trace/provider.rs | 2 +- opentelemetry-sdk/src/trace/sampler.rs | 4 +- .../trace/sampler/jaeger_remote/sampler.rs | 2 +- opentelemetry-sdk/src/trace/span.rs | 10 +- opentelemetry-sdk/src/trace/tracer.rs | 4 +- opentelemetry-stdout/src/common.rs | 22 +-- opentelemetry-zipkin/src/exporter/mod.rs | 2 +- .../src/exporter/model/mod.rs | 2 +- opentelemetry/benches/anyvalue.rs | 2 +- opentelemetry/benches/attributes.rs | 2 +- opentelemetry/src/baggage.rs | 50 +++--- opentelemetry/src/common.rs | 160 ++++++++---------- opentelemetry/src/global/metrics.rs | 8 +- opentelemetry/src/global/trace.rs | 18 +- opentelemetry/src/logs/logger.rs | 4 +- opentelemetry/src/logs/noop.rs | 2 +- opentelemetry/src/logs/record.rs | 19 +-- .../src/metrics/instruments/counter.rs | 8 +- .../src/metrics/instruments/gauge.rs | 8 +- .../src/metrics/instruments/histogram.rs | 4 +- opentelemetry/src/metrics/instruments/mod.rs | 2 +- .../metrics/instruments/up_down_counter.rs | 8 +- opentelemetry/src/metrics/meter.rs | 2 +- opentelemetry/src/metrics/mod.rs | 10 +- opentelemetry/src/metrics/noop.rs | 12 +- opentelemetry/src/testing/trace.rs | 6 +- opentelemetry/src/trace/context.rs | 8 +- opentelemetry/src/trace/mod.rs | 8 +- opentelemetry/src/trace/noop.rs | 8 +- opentelemetry/src/trace/span.rs | 10 +- opentelemetry/src/trace/tracer.rs | 6 +- opentelemetry/src/trace/tracer_provider.rs | 4 +- 55 files changed, 307 insertions(+), 322 deletions(-) diff --git a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs index 610268294e..006d8e4e2e 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs @@ -3,7 +3,7 @@ use opentelemetry::{ global, metrics::MetricsError, trace::{TraceContextExt, TraceError, Tracer, TracerProvider as _}, - Key, KeyValue, + KeyValue, }; use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge; use opentelemetry_otlp::Protocol; @@ -146,7 +146,7 @@ async fn main() -> Result<(), Box> { let span = cx.span(); span.add_event( "Nice operation!".to_string(), - vec![Key::new("bogons").i64(100)], + vec![KeyValue::new("bogons", 100)], ); span.set_attribute(KeyValue::new("another.key", "yes")); diff --git a/opentelemetry-otlp/examples/basic-otlp/src/main.rs b/opentelemetry-otlp/examples/basic-otlp/src/main.rs index f18de642f9..f931e592e2 100644 --- a/opentelemetry-otlp/examples/basic-otlp/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp/src/main.rs @@ -5,7 +5,7 @@ use opentelemetry::metrics::MetricsError; use opentelemetry::trace::{TraceError, TracerProvider}; use opentelemetry::{ trace::{TraceContextExt, Tracer}, - Key, KeyValue, + KeyValue, }; use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge; use opentelemetry_otlp::{ExportConfig, WithExportConfig}; @@ -136,7 +136,7 @@ async fn main() -> Result<(), Box> { let span = cx.span(); span.add_event( "Nice operation!".to_string(), - vec![Key::new("bogons").i64(100)], + vec![KeyValue::new("bogons", 100)], ); span.set_attribute(KeyValue::new("another.key", "yes")); diff --git a/opentelemetry-otlp/tests/integration_test/tests/traces.rs b/opentelemetry-otlp/tests/integration_test/tests/traces.rs index a6a3980e01..f7e3b3a510 100644 --- a/opentelemetry-otlp/tests/integration_test/tests/traces.rs +++ b/opentelemetry-otlp/tests/integration_test/tests/traces.rs @@ -41,7 +41,7 @@ pub async fn traces() -> Result<(), Box> { let span = cx.span(); span.add_event( "Nice operation!".to_string(), - vec![Key::new("bogons").i64(100)], + vec![KeyValue::new("bogons", 100)], ); span.set_attribute(KeyValue::new(ANOTHER_KEY, "yes")); diff --git a/opentelemetry-proto/src/transform/common.rs b/opentelemetry-proto/src/transform/common.rs index ff42479288..e86cc76b5c 100644 --- a/opentelemetry-proto/src/transform/common.rs +++ b/opentelemetry-proto/src/transform/common.rs @@ -112,8 +112,8 @@ pub mod tonic { #[derive(Default, Debug)] pub struct Attributes(pub ::std::vec::Vec); - impl From> for Attributes { - fn from(kvs: Vec) -> Self { + impl From>> for Attributes { + fn from(kvs: Vec>) -> Self { Attributes( kvs.into_iter() .map(|api_kv| KeyValue { @@ -139,19 +139,19 @@ pub mod tonic { } } - impl From for AnyValue { + impl From> for AnyValue { fn from(value: Value) -> Self { AnyValue { value: match value { - Value::Bool(val) => Some(any_value::Value::BoolValue(val)), - Value::I64(val) => Some(any_value::Value::IntValue(val)), - Value::F64(val) => Some(any_value::Value::DoubleValue(val)), + Value::Bool(val) => Some(any_value::Value::BoolValue(*val)), + Value::I64(val) => Some(any_value::Value::IntValue(*val)), + Value::F64(val) => Some(any_value::Value::DoubleValue(*val)), Value::String(val) => Some(any_value::Value::StringValue(val.to_string())), Value::Array(array) => Some(any_value::Value::ArrayValue(match array { - Array::Bool(vals) => array_into_proto(vals), - Array::I64(vals) => array_into_proto(vals), - Array::F64(vals) => array_into_proto(vals), - Array::String(vals) => array_into_proto(vals), + Array::Bool(vals) => array_into_proto(vals.to_vec()), + Array::I64(vals) => array_into_proto(vals.to_vec()), + Array::F64(vals) => array_into_proto(vals.to_vec()), + Array::String(vals) => array_into_proto(vals.to_vec()), })), }, } @@ -160,7 +160,7 @@ pub mod tonic { fn array_into_proto(vals: Vec) -> ArrayValue where - Value: From, + Value<'static>: From, { let values = vals .into_iter() diff --git a/opentelemetry-proto/src/transform/metrics.rs b/opentelemetry-proto/src/transform/metrics.rs index f718c96280..7111c43c12 100644 --- a/opentelemetry-proto/src/transform/metrics.rs +++ b/opentelemetry-proto/src/transform/metrics.rs @@ -73,8 +73,8 @@ pub mod tonic { } } - impl From<(&Key, &Value)> for KeyValue { - fn from(kv: (&Key, &Value)) -> Self { + impl From<(&Key, &Value<'static>)> for KeyValue { + fn from(kv: (&Key, &Value<'static>)) -> Self { KeyValue { key: kv.0.to_string(), value: Some(kv.1.clone().into()), @@ -82,8 +82,8 @@ pub mod tonic { } } - impl From<&opentelemetry::KeyValue> for KeyValue { - fn from(kv: &opentelemetry::KeyValue) -> Self { + impl From<&opentelemetry::KeyValue<'static>> for KeyValue { + fn from(kv: &opentelemetry::KeyValue<'static>) -> Self { KeyValue { key: kv.key.to_string(), value: Some(kv.value.clone().into()), diff --git a/opentelemetry-sdk/benches/trace.rs b/opentelemetry-sdk/benches/trace.rs index ed4d3add82..9022c1fe3f 100644 --- a/opentelemetry-sdk/benches/trace.rs +++ b/opentelemetry-sdk/benches/trace.rs @@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Criterion}; use futures_util::future::BoxFuture; use opentelemetry::{ trace::{Span, Tracer, TracerProvider}, - Key, + KeyValue, }; use opentelemetry_sdk::{ export::trace::{ExportResult, SpanData, SpanExporter}, @@ -16,42 +16,42 @@ fn criterion_benchmark(c: &mut Criterion) { trace_benchmark_group(c, "start-end-span-4-attrs", |tracer| { let mut span = tracer.start("foo"); - span.set_attribute(Key::new("key1").bool(false)); - span.set_attribute(Key::new("key2").string("hello")); - span.set_attribute(Key::new("key4").f64(123.456)); + span.set_attribute(KeyValue::new("key1", false)); + span.set_attribute(KeyValue::new("key2", "hello")); + span.set_attribute(KeyValue::new("key4", 123.456)); span.end(); }); trace_benchmark_group(c, "start-end-span-8-attrs", |tracer| { let mut span = tracer.start("foo"); - span.set_attribute(Key::new("key1").bool(false)); - span.set_attribute(Key::new("key2").string("hello")); - span.set_attribute(Key::new("key4").f64(123.456)); - span.set_attribute(Key::new("key11").bool(false)); - span.set_attribute(Key::new("key12").string("hello")); - span.set_attribute(Key::new("key14").f64(123.456)); + span.set_attribute(KeyValue::new("key1", false)); + span.set_attribute(KeyValue::new("key2", "hello")); + span.set_attribute(KeyValue::new("key4", 123.456)); + span.set_attribute(KeyValue::new("key11", false)); + span.set_attribute(KeyValue::new("key12", "hello")); + span.set_attribute(KeyValue::new("key14", 123.456)); span.end(); }); trace_benchmark_group(c, "start-end-span-all-attr-types", |tracer| { let mut span = tracer.start("foo"); - span.set_attribute(Key::new("key1").bool(false)); - span.set_attribute(Key::new("key2").string("hello")); - span.set_attribute(Key::new("key3").i64(123)); - span.set_attribute(Key::new("key5").f64(123.456)); + span.set_attribute(KeyValue::new("key1", false)); + span.set_attribute(KeyValue::new("key2", "hello")); + span.set_attribute(KeyValue::new("key3", 123)); + span.set_attribute(KeyValue::new("key5", 123.456)); span.end(); }); trace_benchmark_group(c, "start-end-span-all-attr-types-2x", |tracer| { let mut span = tracer.start("foo"); - span.set_attribute(Key::new("key1").bool(false)); - span.set_attribute(Key::new("key2").string("hello")); - span.set_attribute(Key::new("key3").i64(123)); - span.set_attribute(Key::new("key5").f64(123.456)); - span.set_attribute(Key::new("key11").bool(false)); - span.set_attribute(Key::new("key12").string("hello")); - span.set_attribute(Key::new("key13").i64(123)); - span.set_attribute(Key::new("key15").f64(123.456)); + span.set_attribute(KeyValue::new("key1", false)); + span.set_attribute(KeyValue::new("key2", "hello")); + span.set_attribute(KeyValue::new("key3", 123)); + span.set_attribute(KeyValue::new("key5", 123.456)); + span.set_attribute(KeyValue::new("key11", false)); + span.set_attribute(KeyValue::new("key12", "hello")); + span.set_attribute(KeyValue::new("key13", 123)); + span.set_attribute(KeyValue::new("key15", 123.456)); span.end(); }); } diff --git a/opentelemetry-sdk/src/export/trace.rs b/opentelemetry-sdk/src/export/trace.rs index 4b43e00c36..5dbf5137e4 100644 --- a/opentelemetry-sdk/src/export/trace.rs +++ b/opentelemetry-sdk/src/export/trace.rs @@ -85,7 +85,7 @@ pub struct SpanData { /// Span end time pub end_time: SystemTime, /// Span attributes - pub attributes: Vec, + pub attributes: Vec>, /// The number of attributes that were above the configured limit, and thus /// dropped. pub dropped_attributes_count: u32, diff --git a/opentelemetry-sdk/src/logs/log_emitter.rs b/opentelemetry-sdk/src/logs/log_emitter.rs index 7463b19059..42ea88146f 100644 --- a/opentelemetry-sdk/src/logs/log_emitter.rs +++ b/opentelemetry-sdk/src/logs/log_emitter.rs @@ -46,7 +46,7 @@ impl opentelemetry::logs::LoggerProvider for LoggerProvider { name: impl Into>, version: Option>, schema_url: Option>, - attributes: Option>, + attributes: Option>>, ) -> Logger { let name = name.into(); diff --git a/opentelemetry-sdk/src/metrics/data/mod.rs b/opentelemetry-sdk/src/metrics/data/mod.rs index 64a1879d26..fcf4c32f72 100644 --- a/opentelemetry-sdk/src/metrics/data/mod.rs +++ b/opentelemetry-sdk/src/metrics/data/mod.rs @@ -97,7 +97,7 @@ impl Aggregation for Sum { pub struct DataPoint { /// Attributes is the set of key value pairs that uniquely identify the /// time series. - pub attributes: Vec, + pub attributes: Vec>, /// The time when the time series was started. pub start_time: Option, /// The time when the time series was recorded. @@ -143,7 +143,7 @@ impl Aggregation for Histogram { #[derive(Debug)] pub struct HistogramDataPoint { /// The set of key value pairs that uniquely identify the time series. - pub attributes: Vec, + pub attributes: Vec>, /// The time when the time series was started. pub start_time: SystemTime, /// The time when the time series was recorded. @@ -210,7 +210,7 @@ impl Aggregation for ExponentialHistogram #[derive(Debug)] pub struct ExponentialHistogramDataPoint { /// The set of key value pairs that uniquely identify the time series. - pub attributes: Vec, + pub attributes: Vec>, /// When the time series was started. pub start_time: SystemTime, /// The time when the time series was recorded. @@ -273,7 +273,7 @@ pub struct ExponentialBucket { pub struct Exemplar { /// The attributes recorded with the measurement but filtered out of the /// time series' aggregated data. - pub filtered_attributes: Vec, + pub filtered_attributes: Vec>, /// The time when the measurement was recorded. pub time: SystemTime, /// The measured value. diff --git a/opentelemetry-sdk/src/metrics/instrument.rs b/opentelemetry-sdk/src/metrics/instrument.rs index 8f4797f8f8..00c877d2f6 100644 --- a/opentelemetry-sdk/src/metrics/instrument.rs +++ b/opentelemetry-sdk/src/metrics/instrument.rs @@ -253,7 +253,7 @@ pub(crate) struct ResolvedMeasures { } impl SyncCounter for ResolvedMeasures { - fn add(&self, val: T, attrs: &[KeyValue]) { + fn add(&self, val: T, attrs: &[KeyValue<'_>]) { for measure in &self.measures { measure.call(val, attrs) } @@ -261,7 +261,7 @@ impl SyncCounter for ResolvedMeasures { } impl SyncUpDownCounter for ResolvedMeasures { - fn add(&self, val: T, attrs: &[KeyValue]) { + fn add(&self, val: T, attrs: &[KeyValue<'_>]) { for measure in &self.measures { measure.call(val, attrs) } @@ -269,7 +269,7 @@ impl SyncUpDownCounter for ResolvedMeasures { } impl SyncGauge for ResolvedMeasures { - fn record(&self, val: T, attrs: &[KeyValue]) { + fn record(&self, val: T, attrs: &[KeyValue<'_>]) { for measure in &self.measures { measure.call(val, attrs) } @@ -277,7 +277,7 @@ impl SyncGauge for ResolvedMeasures { } impl SyncHistogram for ResolvedMeasures { - fn record(&self, val: T, attrs: &[KeyValue]) { + fn record(&self, val: T, attrs: &[KeyValue<'_>]) { for measure in &self.measures { measure.call(val, attrs) } @@ -296,7 +296,7 @@ impl Observable { } impl AsyncInstrument for Observable { - fn observe(&self, measurement: T, attrs: &[KeyValue]) { + fn observe(&self, measurement: T, attrs: &[KeyValue<'_>]) { for measure in &self.measures { measure.call(measurement, attrs) } diff --git a/opentelemetry-sdk/src/metrics/internal/aggregate.rs b/opentelemetry-sdk/src/metrics/internal/aggregate.rs index 79a06f7242..a2e3212025 100644 --- a/opentelemetry-sdk/src/metrics/internal/aggregate.rs +++ b/opentelemetry-sdk/src/metrics/internal/aggregate.rs @@ -18,14 +18,14 @@ pub(crate) fn is_under_cardinality_limit(size: usize) -> bool { /// Receives measurements to be aggregated. pub(crate) trait Measure: Send + Sync + 'static { - fn call(&self, measurement: T, attrs: &[KeyValue]); + fn call(&self, measurement: T, attrs: &[KeyValue<'_>]); } impl Measure for F where - F: Fn(T, &[KeyValue]) + Send + Sync + 'static, + F: Fn(T, &[KeyValue<'_>]) + Send + Sync + 'static, { - fn call(&self, measurement: T, attrs: &[KeyValue]) { + fn call(&self, measurement: T, attrs: &[KeyValue<'_>]) { self(measurement, attrs) } } @@ -69,7 +69,7 @@ pub(crate) struct AggregateBuilder { _marker: marker::PhantomData, } -type Filter = Arc bool + Send + Sync>; +type Filter = Arc) -> bool + Send + Sync>; impl> AggregateBuilder { pub(crate) fn new(temporality: Option, filter: Option) -> Self { @@ -83,9 +83,9 @@ impl> AggregateBuilder { /// Wraps the passed in measure with an attribute filtering function. fn filter(&self, f: impl Measure) -> impl Measure { let filter = self.filter.clone(); - move |n, attrs: &[KeyValue]| { + move |n, attrs: &[KeyValue<'_>]| { if let Some(filter) = &filter { - let filtered_attrs: Vec = + let filtered_attrs: Vec> = attrs.iter().filter(|kv| filter(kv)).cloned().collect(); f.call(n, &filtered_attrs); } else { @@ -101,7 +101,7 @@ impl> AggregateBuilder { let t = self.temporality; ( - self.filter(move |n, a: &[KeyValue]| lv_filter.measure(n, a)), + self.filter(move |n, a: &[KeyValue<'_>]| lv_filter.measure(n, a)), move |dest: Option<&mut dyn Aggregation>| { let g = dest.and_then(|d| d.as_mut().downcast_mut::>()); let mut new_agg = if g.is_none() { @@ -135,7 +135,7 @@ impl> AggregateBuilder { let t = self.temporality; ( - self.filter(move |n, a: &[KeyValue]| s.measure(n, a)), + self.filter(move |n, a: &[KeyValue<'_>]| s.measure(n, a)), move |dest: Option<&mut dyn Aggregation>| match t { Some(Temporality::Delta) => agg_sum.delta(dest), _ => agg_sum.cumulative(dest), @@ -150,7 +150,7 @@ impl> AggregateBuilder { let t = self.temporality; ( - self.filter(move |n, a: &[KeyValue]| s.measure(n, a)), + self.filter(move |n, a: &[KeyValue<'_>]| s.measure(n, a)), move |dest: Option<&mut dyn Aggregation>| match t { Some(Temporality::Delta) => agg_sum.delta(dest), _ => agg_sum.cumulative(dest), @@ -170,7 +170,7 @@ impl> AggregateBuilder { let t = self.temporality; ( - self.filter(move |n, a: &[KeyValue]| h.measure(n, a)), + self.filter(move |n, a: &[KeyValue<'_>]| h.measure(n, a)), move |dest: Option<&mut dyn Aggregation>| match t { Some(Temporality::Delta) => agg_h.delta(dest), _ => agg_h.cumulative(dest), @@ -196,7 +196,7 @@ impl> AggregateBuilder { let t = self.temporality; ( - self.filter(move |n, a: &[KeyValue]| h.measure(n, a)), + self.filter(move |n, a: &[KeyValue<'_>]| h.measure(n, a)), move |dest: Option<&mut dyn Aggregation>| match t { Some(Temporality::Delta) => agg_h.delta(dest), _ => agg_h.cumulative(dest), diff --git a/opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs b/opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs index bf44beb59c..416f580927 100644 --- a/opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs +++ b/opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs @@ -340,7 +340,7 @@ impl> ExpoHistogram { } } - pub(crate) fn measure(&self, value: T, attrs: &[KeyValue]) { + pub(crate) fn measure(&self, value: T, attrs: &[KeyValue<'_>]) { let f_value = value.into_float(); // Ignore NaN and infinity. if f_value.is_infinite() || f_value.is_nan() { diff --git a/opentelemetry-sdk/src/metrics/internal/histogram.rs b/opentelemetry-sdk/src/metrics/internal/histogram.rs index 8fc5bf59c1..a7bcd869bf 100644 --- a/opentelemetry-sdk/src/metrics/internal/histogram.rs +++ b/opentelemetry-sdk/src/metrics/internal/histogram.rs @@ -120,7 +120,7 @@ impl> Histogram { histogram } - pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue]) { + pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue<'_>]) { let f = measurement.into_float(); // This search will return an index in the range `[0, bounds.len()]`, where diff --git a/opentelemetry-sdk/src/metrics/internal/last_value.rs b/opentelemetry-sdk/src/metrics/internal/last_value.rs index cce14c5c42..14bbeeed0f 100644 --- a/opentelemetry-sdk/src/metrics/internal/last_value.rs +++ b/opentelemetry-sdk/src/metrics/internal/last_value.rs @@ -23,7 +23,7 @@ impl> LastValue { } } - pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue]) { + pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue<'_>]) { // The argument index is not applicable to LastValue. self.value_map.measure(measurement, attrs, 0); } diff --git a/opentelemetry-sdk/src/metrics/internal/mod.rs b/opentelemetry-sdk/src/metrics/internal/mod.rs index 41f97aa20b..772e0b85d8 100644 --- a/opentelemetry-sdk/src/metrics/internal/mod.rs +++ b/opentelemetry-sdk/src/metrics/internal/mod.rs @@ -21,7 +21,7 @@ use opentelemetry::{global, KeyValue}; use crate::metrics::AttributeSet; -pub(crate) static STREAM_OVERFLOW_ATTRIBUTES: Lazy> = +pub(crate) static STREAM_OVERFLOW_ATTRIBUTES: Lazy>> = Lazy::new(|| vec![KeyValue::new("otel.metric.overflow", "true")]); /// Abstracts the update operation for a measurement. @@ -51,7 +51,7 @@ impl Operation for Assign { /// updates to the underlying value trackers should be performed. pub(crate) struct ValueMap, T: Number, O> { /// Trackers store the values associated with different attribute sets. - trackers: RwLock, Arc>>, + trackers: RwLock>, Arc>>, /// Number of different attribute set stored in the `trackers` map. count: AtomicUsize, /// Indicates whether a value with no attributes has been stored. @@ -94,7 +94,7 @@ impl, T: Number, O> ValueMap { } impl, T: Number, O: Operation> ValueMap { - fn measure(&self, measurement: T, attributes: &[KeyValue], index: usize) { + fn measure(&self, measurement: T, attributes: &[KeyValue<'_>], index: usize) { if attributes.is_empty() { O::update_tracker(&self.no_attribute_tracker, measurement, index); self.has_no_attribute_value.store(true, Ordering::Release); @@ -136,7 +136,10 @@ impl, T: Number, O: Operation> ValueMap { O::update_tracker(&*new_tracker, measurement, index); // Insert tracker with the attributes in the provided and sorted orders - trackers.insert(attributes.to_vec(), new_tracker.clone()); + trackers.insert( + attributes.iter().map(|kv| kv.to_owned()).collect(), + new_tracker.clone(), + ); trackers.insert(sorted_attrs, new_tracker); self.count.fetch_add(1, Ordering::SeqCst); diff --git a/opentelemetry-sdk/src/metrics/internal/precomputed_sum.rs b/opentelemetry-sdk/src/metrics/internal/precomputed_sum.rs index 14e9c19b25..ffd319265c 100644 --- a/opentelemetry-sdk/src/metrics/internal/precomputed_sum.rs +++ b/opentelemetry-sdk/src/metrics/internal/precomputed_sum.rs @@ -14,7 +14,7 @@ pub(crate) struct PrecomputedSum> { value_map: ValueMap, monotonic: bool, start: Mutex, - reported: Mutex, T>>, + reported: Mutex>, T>>, } impl> PrecomputedSum { @@ -27,7 +27,7 @@ impl> PrecomputedSum { } } - pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue]) { + pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue<'_>]) { // The argument index is not applicable to PrecomputedSum. self.value_map.measure(measurement, attrs, 0); } diff --git a/opentelemetry-sdk/src/metrics/internal/sum.rs b/opentelemetry-sdk/src/metrics/internal/sum.rs index 68a58d1e8d..6f2893aef4 100644 --- a/opentelemetry-sdk/src/metrics/internal/sum.rs +++ b/opentelemetry-sdk/src/metrics/internal/sum.rs @@ -31,7 +31,7 @@ impl> Sum { } } - pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue]) { + pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue<'_>]) { // The argument index is not applicable to Sum. self.value_map.measure(measurement, attrs, 0); } diff --git a/opentelemetry-sdk/src/metrics/meter_provider.rs b/opentelemetry-sdk/src/metrics/meter_provider.rs index c693b2aa56..a833a8f42d 100644 --- a/opentelemetry-sdk/src/metrics/meter_provider.rs +++ b/opentelemetry-sdk/src/metrics/meter_provider.rs @@ -142,7 +142,7 @@ impl MeterProvider for SdkMeterProvider { name: impl Into>, version: Option>>, schema_url: Option>>, - attributes: Option>, + attributes: Option>>, ) -> Meter { if self.inner.is_shutdown.load(Ordering::Relaxed) { return Meter::new(Arc::new(NoopMeterCore::new())); diff --git a/opentelemetry-sdk/src/metrics/mod.rs b/opentelemetry-sdk/src/metrics/mod.rs index 0e1f978525..393fda3074 100644 --- a/opentelemetry-sdk/src/metrics/mod.rs +++ b/opentelemetry-sdk/src/metrics/mod.rs @@ -72,17 +72,17 @@ use opentelemetry::{Key, KeyValue, Value}; /// This must implement [Hash], [PartialEq], and [Eq] so it may be used as /// HashMap keys and other de-duplication methods. #[derive(Clone, Default, Debug, PartialEq, Eq)] -pub(crate) struct AttributeSet(Vec, u64); +pub(crate) struct AttributeSet(Vec>, u64); -impl From<&[KeyValue]> for AttributeSet { - fn from(values: &[KeyValue]) -> Self { +impl From<&[KeyValue<'_>]> for AttributeSet { + fn from(values: &[KeyValue<'_>]) -> Self { let mut seen_keys = HashSet::with_capacity(values.len()); let vec = values .iter() .rev() .filter_map(|kv| { if seen_keys.insert(kv.key.clone()) { - Some(kv.clone()) + Some(kv.to_owned()) } else { None } @@ -93,7 +93,7 @@ impl From<&[KeyValue]> for AttributeSet { } } -fn calculate_hash(values: &[KeyValue]) -> u64 { +fn calculate_hash(values: &[KeyValue<'static>]) -> u64 { let mut hasher = DefaultHasher::new(); values.iter().fold(&mut hasher, |mut hasher, item| { item.hash(&mut hasher); @@ -103,19 +103,19 @@ fn calculate_hash(values: &[KeyValue]) -> u64 { } impl AttributeSet { - fn new(mut values: Vec) -> Self { + fn new(mut values: Vec>) -> Self { values.sort_unstable(); let hash = calculate_hash(&values); AttributeSet(values, hash) } /// Iterate over key value pairs in the set - pub(crate) fn iter(&self) -> impl Iterator { + pub(crate) fn iter(&self) -> impl Iterator)> { self.0.iter().map(|kv| (&kv.key, &kv.value)) } /// Returns the underlying Vec of KeyValue pairs - pub(crate) fn into_vec(self) -> Vec { + pub(crate) fn into_vec(self) -> Vec> { self.0 } } diff --git a/opentelemetry-sdk/src/metrics/pipeline.rs b/opentelemetry-sdk/src/metrics/pipeline.rs index c55b407638..ffd8104a83 100644 --- a/opentelemetry-sdk/src/metrics/pipeline.rs +++ b/opentelemetry-sdk/src/metrics/pipeline.rs @@ -367,10 +367,9 @@ where let mut cache = self.aggregators.lock()?; let cached = cache.entry(id).or_insert_with(|| { - let filter = stream - .allowed_attribute_keys - .clone() - .map(|allowed| Arc::new(move |kv: &KeyValue| allowed.contains(&kv.key)) as Arc<_>); + let filter = stream.allowed_attribute_keys.clone().map(|allowed| { + Arc::new(move |kv: &KeyValue<'_>| allowed.contains(&kv.key)) as Arc<_> + }); let b = AggregateBuilder::new(Some(self.pipeline.reader.temporality(kind)), filter); let (m, ca) = match aggregate_fn(b, &agg, kind) { diff --git a/opentelemetry-sdk/src/propagation/baggage.rs b/opentelemetry-sdk/src/propagation/baggage.rs index 9c0d77d0ee..72c7a97e74 100644 --- a/opentelemetry-sdk/src/propagation/baggage.rs +++ b/opentelemetry-sdk/src/propagation/baggage.rs @@ -25,7 +25,7 @@ static BAGGAGE_FIELDS: Lazy<[String; 1]> = Lazy::new(|| [BAGGAGE_HEADER.to_owned /// # Examples /// /// ``` -/// use opentelemetry::{baggage::BaggageExt, Key, propagation::TextMapPropagator}; +/// use opentelemetry::{baggage::BaggageExt, Key, KeyValue, propagation::TextMapPropagator}; /// use opentelemetry_sdk::propagation::BaggagePropagator; /// use std::collections::HashMap; /// @@ -43,7 +43,7 @@ static BAGGAGE_FIELDS: Lazy<[String; 1]> = Lazy::new(|| [BAGGAGE_HEADER.to_owned /// } /// /// // Add new baggage -/// let cx_with_additions = cx.with_baggage(vec![Key::new("server_id").i64(42)]); +/// let cx_with_additions = cx.with_baggage(vec![KeyValue::new("server_id", 42)]); /// /// // Inject baggage into http request /// propagator.inject_context(&cx_with_additions, &mut headers); @@ -159,7 +159,7 @@ mod tests { use std::collections::HashMap; #[rustfmt::skip] - fn valid_extract_data() -> Vec<(&'static str, HashMap)> { + fn valid_extract_data() -> Vec<(&'static str, HashMap>)> { vec![ // "valid w3cHeader" ("key1=val1,key2=val2", vec![(Key::new("key1"), Value::from("val1")), (Key::new("key2"), Value::from("val2"))].into_iter().collect()), @@ -176,7 +176,7 @@ mod tests { #[rustfmt::skip] #[allow(clippy::type_complexity)] - fn valid_extract_data_with_metadata() -> Vec<(&'static str, HashMap)> { + fn valid_extract_data_with_metadata() -> Vec<(&'static str, HashMap, BaggageMetadata)>)> { vec![ // "valid w3cHeader with properties" ("key1=val1,key2=val2;prop=1", vec![(Key::new("key1"), (Value::from("val1"), BaggageMetadata::default())), (Key::new("key2"), (Value::from("val2"), BaggageMetadata::from("prop=1")))].into_iter().collect()), @@ -192,7 +192,7 @@ mod tests { } #[rustfmt::skip] - fn valid_inject_data() -> Vec<(Vec, Vec<&'static str>)> { + fn valid_inject_data() -> Vec<(Vec>, Vec<&'static str>)> { vec![ // "two simple values" (vec![KeyValue::new("key1", "val1"), KeyValue::new("key2", "val2")], vec!["key1=val1", "key2=val2"]), @@ -202,8 +202,8 @@ mod tests { ( vec![ KeyValue::new("key1", true), - KeyValue::new("key2", Value::I64(123)), - KeyValue::new("key3", Value::F64(123.567)), + KeyValue::new("key2", 123), + KeyValue::new("key3", 123.567), ], vec![ "key1=true", @@ -214,9 +214,9 @@ mod tests { // "values of array types" ( vec![ - KeyValue::new("key1", Value::Array(vec![true, false].into())), - KeyValue::new("key2", Value::Array(vec![123, 456].into())), - KeyValue::new("key3", Value::Array(vec![StringValue::from("val1"), StringValue::from("val2")].into())), + KeyValue::new("key1", Value::Array(vec![true, false].as_slice().into())), + KeyValue::new("key2", Value::Array(vec![123, 456].as_slice().into())), + KeyValue::new("key3", Value::Array(vec![StringValue::from("val1"), StringValue::from("val2")].as_slice().into())), ], vec![ "key1=[true%2Cfalse]", diff --git a/opentelemetry-sdk/src/resource/mod.rs b/opentelemetry-sdk/src/resource/mod.rs index dc9bbe316b..f7e647ba08 100644 --- a/opentelemetry-sdk/src/resource/mod.rs +++ b/opentelemetry-sdk/src/resource/mod.rs @@ -41,7 +41,7 @@ use std::time::Duration; /// This structure is designed to be shared among `Resource` instances via `Arc`. #[derive(Debug, Clone, PartialEq)] struct ResourceInner { - attrs: HashMap, + attrs: HashMap>, schema_url: Option>, } @@ -81,7 +81,7 @@ impl Resource { /// /// Values are de-duplicated by key, and the first key-value pair with a non-empty string value /// will be retained - pub fn new>(kvs: T) -> Self { + pub fn new>>(kvs: T) -> Self { let mut attrs = HashMap::new(); for kv in kvs { attrs.insert(kv.key, kv.value); @@ -105,7 +105,7 @@ impl Resource { /// [schema url]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/schemas/overview.md#schema-url pub fn from_schema_url(kvs: KV, schema_url: S) -> Self where - KV: IntoIterator, + KV: IntoIterator>, S: Into>, { let schema_url_str = schema_url.into(); @@ -215,17 +215,17 @@ impl Resource { } /// Retrieve the value from resource associate with given key. - pub fn get(&self, key: Key) -> Option { + pub fn get(&self, key: Key) -> Option> { self.inner.attrs.get(&key).cloned() } } /// An iterator over the entries of a `Resource`. #[derive(Debug)] -pub struct Iter<'a>(hash_map::Iter<'a, Key, Value>); +pub struct Iter<'a>(hash_map::Iter<'a, Key, Value<'static>>); impl<'a> Iterator for Iter<'a> { - type Item = (&'a Key, &'a Value); + type Item = (&'a Key, &'a Value<'static>); fn next(&mut self) -> Option { self.0.next() @@ -233,7 +233,7 @@ impl<'a> Iterator for Iter<'a> { } impl<'a> IntoIterator for &'a Resource { - type Item = (&'a Key, &'a Value); + type Item = (&'a Key, &'a Value<'static>); type IntoIter = Iter<'a>; fn into_iter(self) -> Self::IntoIter { diff --git a/opentelemetry-sdk/src/trace/mod.rs b/opentelemetry-sdk/src/trace/mod.rs index ccaae00638..8f5391632c 100644 --- a/opentelemetry-sdk/src/trace/mod.rs +++ b/opentelemetry-sdk/src/trace/mod.rs @@ -271,7 +271,7 @@ mod tests { _trace_id: TraceId, _name: &str, _span_kind: &SpanKind, - _attributes: &[KeyValue], + _attributes: &[KeyValue<'static>], _links: &[Link], ) -> SamplingResult { let trace_state = parent_context diff --git a/opentelemetry-sdk/src/trace/provider.rs b/opentelemetry-sdk/src/trace/provider.rs index 9550ce11d2..e332102951 100644 --- a/opentelemetry-sdk/src/trace/provider.rs +++ b/opentelemetry-sdk/src/trace/provider.rs @@ -189,7 +189,7 @@ impl opentelemetry::trace::TracerProvider for TracerProvider { name: impl Into>, version: Option>>, schema_url: Option>>, - attributes: Option>, + attributes: Option>>, ) -> Self::Tracer { // Use default value if name is invalid empty string let name = name.into(); diff --git a/opentelemetry-sdk/src/trace/sampler.rs b/opentelemetry-sdk/src/trace/sampler.rs index 572a17b554..f0c6d13517 100644 --- a/opentelemetry-sdk/src/trace/sampler.rs +++ b/opentelemetry-sdk/src/trace/sampler.rs @@ -77,7 +77,7 @@ pub trait ShouldSample: CloneShouldSample + Send + Sync + std::fmt::Debug { trace_id: TraceId, name: &str, span_kind: &SpanKind, - attributes: &[KeyValue], + attributes: &[KeyValue<'static>], links: &[Link], ) -> SamplingResult; } @@ -169,7 +169,7 @@ impl ShouldSample for Sampler { trace_id: TraceId, name: &str, span_kind: &SpanKind, - attributes: &[KeyValue], + attributes: &[KeyValue<'static>], links: &[Link], ) -> SamplingResult { let decision = match self { diff --git a/opentelemetry-sdk/src/trace/sampler/jaeger_remote/sampler.rs b/opentelemetry-sdk/src/trace/sampler/jaeger_remote/sampler.rs index 6f942cbd7f..bd1184bc22 100644 --- a/opentelemetry-sdk/src/trace/sampler/jaeger_remote/sampler.rs +++ b/opentelemetry-sdk/src/trace/sampler/jaeger_remote/sampler.rs @@ -251,7 +251,7 @@ impl ShouldSample for JaegerRemoteSampler { trace_id: TraceId, name: &str, span_kind: &SpanKind, - attributes: &[KeyValue], + attributes: &[KeyValue<'static>], links: &[Link], ) -> SamplingResult { self.inner diff --git a/opentelemetry-sdk/src/trace/span.rs b/opentelemetry-sdk/src/trace/span.rs index ea03d9ab53..a1c1c53286 100644 --- a/opentelemetry-sdk/src/trace/span.rs +++ b/opentelemetry-sdk/src/trace/span.rs @@ -36,7 +36,7 @@ pub(crate) struct SpanData { /// Span end time pub(crate) end_time: SystemTime, /// Span attributes - pub(crate) attributes: Vec, + pub(crate) attributes: Vec>, /// The number of attributes that were above the configured limit, and thus /// dropped. pub(crate) dropped_attributes_count: u32, @@ -93,7 +93,7 @@ impl opentelemetry::trace::Span for Span { &mut self, name: T, timestamp: SystemTime, - mut attributes: Vec, + mut attributes: Vec>, ) where T: Into>, { @@ -134,7 +134,7 @@ impl opentelemetry::trace::Span for Span { /// Note that the OpenTelemetry project documents certain ["standard /// attributes"](https://github.com/open-telemetry/opentelemetry-specification/tree/v0.5.0/specification/trace/semantic_conventions/README.md) /// that have prescribed semantic meanings. - fn set_attribute(&mut self, attribute: KeyValue) { + fn set_attribute(&mut self, attribute: KeyValue<'static>) { let span_attribute_limit = self.span_limits.max_attributes_per_span as usize; self.with_data(|data| { if data.attributes.len() < span_attribute_limit { @@ -170,7 +170,7 @@ impl opentelemetry::trace::Span for Span { /// Add `Link` to this `Span` /// - fn add_link(&mut self, span_context: SpanContext, attributes: Vec) { + fn add_link(&mut self, span_context: SpanContext, attributes: Vec>) { let span_links_limit = self.span_limits.max_links_per_span as usize; let link_attributes_limit = self.span_limits.max_attributes_per_link as usize; self.with_data(|data| { @@ -391,7 +391,7 @@ mod tests { let attributes = KeyValue::new("k", "v"); span.set_attribute(attributes.clone()); span.with_data(|data| { - let matching_attribute: Vec<&KeyValue> = data + let matching_attribute: Vec<&KeyValue<'static>> = data .attributes .iter() .filter(|kv| kv.key.as_str() == attributes.key.as_str()) diff --git a/opentelemetry-sdk/src/trace/tracer.rs b/opentelemetry-sdk/src/trace/tracer.rs index f3182d388e..29360760ce 100644 --- a/opentelemetry-sdk/src/trace/tracer.rs +++ b/opentelemetry-sdk/src/trace/tracer.rs @@ -67,7 +67,7 @@ impl Tracer { psc: &SpanContext, sc: SpanContext, mut builder: SpanBuilder, - attrs: Vec, + attrs: Vec>, span_limits: SpanLimits, ) -> Span { let mut attribute_options = builder.attributes.take().unwrap_or_default(); @@ -315,7 +315,7 @@ mod tests { _trace_id: TraceId, _name: &str, _span_kind: &SpanKind, - _attributes: &[KeyValue], + _attributes: &[KeyValue<'static>], _links: &[Link], ) -> SamplingResult { let trace_state = parent_context diff --git a/opentelemetry-stdout/src/common.rs b/opentelemetry-stdout/src/common.rs index 236accea7c..cce1570f51 100644 --- a/opentelemetry-stdout/src/common.rs +++ b/opentelemetry-stdout/src/common.rs @@ -124,25 +124,25 @@ impl Hash for Value { } } -impl From for Value { +impl From> for Value { fn from(value: opentelemetry::Value) -> Self { match value { - opentelemetry::Value::Bool(b) => Value::Bool(b), - opentelemetry::Value::I64(i) => Value::Int(i), - opentelemetry::Value::F64(f) => Value::Double(f), + opentelemetry::Value::Bool(b) => Value::Bool(*b), + opentelemetry::Value::I64(i) => Value::Int(*i), + opentelemetry::Value::F64(f) => Value::Double(*f), opentelemetry::Value::String(s) => Value::String(s.into()), opentelemetry::Value::Array(a) => match a { opentelemetry::Array::Bool(b) => { - Value::Array(b.into_iter().map(Value::Bool).collect()) + Value::Array(b.iter().map(|b| Value::Bool(*b)).collect()) } opentelemetry::Array::I64(i) => { - Value::Array(i.into_iter().map(Value::Int).collect()) + Value::Array(i.iter().map(|i| Value::Int(*i)).collect()) } opentelemetry::Array::F64(f) => { - Value::Array(f.into_iter().map(Value::Double).collect()) + Value::Array(f.iter().map(|f| Value::Double(*f)).collect()) } opentelemetry::Array::String(s) => { - Value::Array(s.into_iter().map(|s| Value::String(s.into())).collect()) + Value::Array(s.iter().map(|s| Value::String(s.clone().into())).collect()) } }, } @@ -190,7 +190,7 @@ impl From<(opentelemetry::Key, opentelemetry::logs::AnyValue)> for KeyValue { } } -impl From for KeyValue { +impl From> for KeyValue { fn from(value: opentelemetry::KeyValue) -> Self { KeyValue { key: value.key.into(), @@ -199,7 +199,7 @@ impl From for KeyValue { } } -impl From<&opentelemetry::KeyValue> for KeyValue { +impl From<&opentelemetry::KeyValue<'_>> for KeyValue { fn from(value: &opentelemetry::KeyValue) -> Self { KeyValue { key: value.key.clone().into(), @@ -208,7 +208,7 @@ impl From<&opentelemetry::KeyValue> for KeyValue { } } -impl From<(opentelemetry::Key, opentelemetry::Value)> for KeyValue { +impl From<(opentelemetry::Key, opentelemetry::Value<'_>)> for KeyValue { fn from((key, value): (opentelemetry::Key, opentelemetry::Value)) -> Self { KeyValue { key: key.into(), diff --git a/opentelemetry-zipkin/src/exporter/mod.rs b/opentelemetry-zipkin/src/exporter/mod.rs index 88edcfb025..2438dea066 100644 --- a/opentelemetry-zipkin/src/exporter/mod.rs +++ b/opentelemetry-zipkin/src/exporter/mod.rs @@ -102,7 +102,7 @@ impl ZipkinPipelineBuilder { .iter() .filter(|(k, _v)| k.as_str() != semcov::resource::SERVICE_NAME) .map(|(k, v)| KeyValue::new(k.clone(), v.clone())) - .collect::>(), + .collect::>>(), )); cfg } else { diff --git a/opentelemetry-zipkin/src/exporter/model/mod.rs b/opentelemetry-zipkin/src/exporter/model/mod.rs index b143848f31..f46645b0a7 100644 --- a/opentelemetry-zipkin/src/exporter/model/mod.rs +++ b/opentelemetry-zipkin/src/exporter/model/mod.rs @@ -104,7 +104,7 @@ pub(crate) fn into_zipkin_span(local_endpoint: Endpoint, span_data: SpanData) -> fn map_from_kvs(kvs: T) -> HashMap where - T: IntoIterator, + T: IntoIterator>, { let mut map: HashMap = HashMap::new(); for kv in kvs { diff --git a/opentelemetry/benches/anyvalue.rs b/opentelemetry/benches/anyvalue.rs index bc93023c76..01e9905775 100644 --- a/opentelemetry/benches/anyvalue.rs +++ b/opentelemetry/benches/anyvalue.rs @@ -28,7 +28,7 @@ fn attributes_creation(c: &mut Criterion) { c.bench_function("CreateOTelValueInt", |b| { b.iter(|| { - let _v = black_box(Value::I64(123)); + let _v = black_box(Into::::into(123)); }); }); diff --git a/opentelemetry/benches/attributes.rs b/opentelemetry/benches/attributes.rs index 14eff1af2d..8899046054 100644 --- a/opentelemetry/benches/attributes.rs +++ b/opentelemetry/benches/attributes.rs @@ -10,7 +10,7 @@ RAM: 64.0 GB | CreateTupleKeyValue | 671 ps | | CreateOtelKeyValueArray | 18.4 ns | | CreateOtelKeyValueArrayWithMixedValueTypes | 18.1 ns | -| CreateOtelKeyValueArrayWithNonStaticValues | 90.1 ns | +| CreateOtelKeyValueArrayWithNonStaticValues | 61.2 ns | | CreateTupleKeyValueArray | 2.73 ns | */ diff --git a/opentelemetry/src/baggage.rs b/opentelemetry/src/baggage.rs index 37ba28e682..0907769cfd 100644 --- a/opentelemetry/src/baggage.rs +++ b/opentelemetry/src/baggage.rs @@ -50,7 +50,7 @@ const MAX_LEN_OF_ALL_PAIRS: usize = 8192; /// [RFC2616, Section 2.2]: https://tools.ietf.org/html/rfc2616#section-2.2 #[derive(Debug, Default)] pub struct Baggage { - inner: HashMap, + inner: HashMap, BaggageMetadata)>, kv_content_len: usize, // the length of key-value-metadata string in `inner` } @@ -75,7 +75,7 @@ impl Baggage { /// /// assert_eq!(cc.get("my-name"), Some(&Value::from("my-value"))) /// ``` - pub fn get>(&self, key: K) -> Option<&Value> { + pub fn get>(&self, key: K) -> Option<&Value<'static>> { self.inner.get(key.as_ref()).map(|(value, _metadata)| value) } @@ -91,7 +91,10 @@ impl Baggage { /// // By default, the metadata is empty /// assert_eq!(cc.get_with_metadata("my-name"), Some(&(Value::from("my-value"), BaggageMetadata::from("")))) /// ``` - pub fn get_with_metadata>(&self, key: K) -> Option<&(Value, BaggageMetadata)> { + pub fn get_with_metadata>( + &self, + key: K, + ) -> Option<&(Value<'static>, BaggageMetadata)> { self.inner.get(key.as_ref()) } @@ -110,10 +113,10 @@ impl Baggage { /// /// assert_eq!(cc.get("my-name"), Some(&Value::from("my-value"))) /// ``` - pub fn insert(&mut self, key: K, value: V) -> Option + pub fn insert(&mut self, key: K, value: V) -> Option> where K: Into, - V: Into, + V: Into>, { self.insert_with_metadata(key, value, BaggageMetadata::default()) .map(|pair| pair.0) @@ -139,10 +142,10 @@ impl Baggage { key: K, value: V, metadata: S, - ) -> Option<(Value, BaggageMetadata)> + ) -> Option<(Value<'static>, BaggageMetadata)> where K: Into, - V: Into, + V: Into>, S: Into, { let (key, value, metadata) = (key.into(), value.into(), metadata.into()); @@ -155,7 +158,7 @@ impl Baggage { /// Removes a name from the baggage, returning the value /// corresponding to the name if the pair was previously in the map. - pub fn remove>(&mut self, key: K) -> Option<(Value, BaggageMetadata)> { + pub fn remove>(&mut self, key: K) -> Option<(Value<'static>, BaggageMetadata)> { self.inner.remove(&key.into()) } @@ -176,7 +179,12 @@ impl Baggage { /// Determine whether the key value pair exceed one of the [limits](https://w3c.github.io/baggage/#limits). /// If not, update the total length of key values - fn insertable(&mut self, key: &Key, value: &Value, metadata: &BaggageMetadata) -> bool { + fn insertable( + &mut self, + key: &Key, + value: &Value<'static>, + metadata: &BaggageMetadata, + ) -> bool { if !key.as_str().is_ascii() { return false; } @@ -231,10 +239,10 @@ fn key_value_metadata_bytes_size(key: &str, value: &str, metadata: &str) -> usiz /// An iterator over the entries of a [`Baggage`]. #[derive(Debug)] -pub struct Iter<'a>(hash_map::Iter<'a, Key, (Value, BaggageMetadata)>); +pub struct Iter<'a>(hash_map::Iter<'a, Key, (Value<'static>, BaggageMetadata)>); impl<'a> Iterator for Iter<'a> { - type Item = (&'a Key, &'a (Value, BaggageMetadata)); + type Item = (&'a Key, &'a (Value<'static>, BaggageMetadata)); fn next(&mut self) -> Option { self.0.next() @@ -242,7 +250,7 @@ impl<'a> Iterator for Iter<'a> { } impl<'a> IntoIterator for &'a Baggage { - type Item = (&'a Key, &'a (Value, BaggageMetadata)); + type Item = (&'a Key, &'a (Value<'static>, BaggageMetadata)); type IntoIter = Iter<'a>; fn into_iter(self) -> Self::IntoIter { @@ -250,8 +258,10 @@ impl<'a> IntoIterator for &'a Baggage { } } -impl FromIterator<(Key, (Value, BaggageMetadata))> for Baggage { - fn from_iter>(iter: I) -> Self { +impl FromIterator<(Key, (Value<'static>, BaggageMetadata))> for Baggage { + fn from_iter, BaggageMetadata))>>( + iter: I, + ) -> Self { let mut baggage = Baggage::default(); for (key, (value, metadata)) in iter.into_iter() { baggage.insert_with_metadata(key, value, metadata); @@ -260,8 +270,8 @@ impl FromIterator<(Key, (Value, BaggageMetadata))> for Baggage { } } -impl FromIterator for Baggage { - fn from_iter>(iter: I) -> Self { +impl FromIterator> for Baggage { + fn from_iter>>(iter: I) -> Self { let mut baggage = Baggage::default(); for kv in iter.into_iter() { baggage.insert(kv.key, kv.value); @@ -442,7 +452,7 @@ pub struct KeyValueMetadata { /// Dimension or event key pub key: Key, /// Dimension or event value - pub value: Value, + pub value: Value<'static>, /// Metadata associate with this key value pair pub metadata: BaggageMetadata, } @@ -452,7 +462,7 @@ impl KeyValueMetadata { pub fn new(key: K, value: V, metadata: S) -> Self where K: Into, - V: Into, + V: Into>, S: Into, { KeyValueMetadata { @@ -463,8 +473,8 @@ impl KeyValueMetadata { } } -impl From for KeyValueMetadata { - fn from(kv: KeyValue) -> Self { +impl From> for KeyValueMetadata { + fn from(kv: KeyValue<'static>) -> Self { KeyValueMetadata { key: kv.key, value: kv.value, diff --git a/opentelemetry/src/common.rs b/opentelemetry/src/common.rs index d99b1430cd..c90cdef199 100644 --- a/opentelemetry/src/common.rs +++ b/opentelemetry/src/common.rs @@ -32,46 +32,6 @@ impl Key { Key(OtelString::Static(value)) } - /// Create a `KeyValue` pair for `bool` values. - pub fn bool>(self, value: T) -> KeyValue { - KeyValue { - key: self, - value: Value::Bool(value.into()), - } - } - - /// Create a `KeyValue` pair for `i64` values. - pub fn i64(self, value: i64) -> KeyValue { - KeyValue { - key: self, - value: Value::I64(value), - } - } - - /// Create a `KeyValue` pair for `f64` values. - pub fn f64(self, value: f64) -> KeyValue { - KeyValue { - key: self, - value: Value::F64(value), - } - } - - /// Create a `KeyValue` pair for string-like values. - pub fn string(self, value: impl Into) -> KeyValue { - KeyValue { - key: self, - value: Value::String(value.into()), - } - } - - /// Create a `KeyValue` pair for arrays. - pub fn array>(self, value: T) -> KeyValue { - KeyValue { - key: self, - value: Value::Array(value.into()), - } - } - /// Returns a reference to the underlying key name pub fn as_str(&self) -> &str { self.0.as_str() @@ -190,18 +150,29 @@ impl hash::Hash for OtelString { /// A [Value::Array] containing homogeneous values. #[derive(Clone, Debug, PartialEq)] -pub enum Array { +pub enum Array<'a> { /// Array of bools - Bool(Vec), + Bool(Cow<'a, [bool]>), /// Array of integers - I64(Vec), + I64(Cow<'a, [i64]>), /// Array of floats - F64(Vec), + F64(Cow<'a, [f64]>), /// Array of strings - String(Vec), + String(Cow<'a, [StringValue]>), +} + +impl Array<'_> { + fn to_owned(&self) -> Array<'static> { + match self { + Array::Bool(v) => Array::Bool(Cow::Owned(v.clone().into_owned())), + Array::I64(v) => Array::I64(Cow::Owned(v.clone().into_owned())), + Array::F64(v) => Array::F64(Cow::Owned(v.clone().into_owned())), + Array::String(v) => Array::String(Cow::Owned(v.clone().into_owned())), + } + } } -impl fmt::Display for Array { +impl fmt::Display for Array<'_> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Array::Bool(values) => display_array_str(values, fmt), @@ -235,9 +206,9 @@ fn display_array_str(slice: &[T], fmt: &mut fmt::Formatter<'_>) macro_rules! into_array { ($(($t:ty, $val:expr),)+) => { $( - impl From<$t> for Array { + impl From<$t> for Array<'static> { fn from(t: $t) -> Self { - $val(t) + $val(Cow::Owned(t.to_vec())) } } )+ @@ -245,25 +216,37 @@ macro_rules! into_array { } into_array!( - (Vec, Array::Bool), - (Vec, Array::I64), - (Vec, Array::F64), - (Vec, Array::String), + (&[bool], Array::Bool), + (&[i64], Array::I64), + (&[f64], Array::F64), + (&[StringValue], Array::String), ); /// The value part of attribute [KeyValue] pairs. #[derive(Clone, Debug, PartialEq)] -pub enum Value { +pub enum Value<'a> { /// bool values - Bool(bool), + Bool(Cow<'a, bool>), /// i64 values - I64(i64), + I64(Cow<'a, i64>), /// f64 values - F64(f64), + F64(Cow<'a, f64>), /// String values - String(StringValue), + String(Cow<'a, str>), /// Array of homogeneous values - Array(Array), + Array(Array<'a>), +} + +impl Value<'_> { + fn to_owned(&self) -> Value<'static> { + match self { + Value::Bool(v) => Value::Bool(Cow::Owned(v.clone().into_owned())), + Value::I64(v) => Value::I64(Cow::Owned(v.clone().into_owned())), + Value::F64(v) => Value::F64(Cow::Owned(v.clone().into_owned())), + Value::String(v) => Value::String(Cow::Owned(v.clone().into_owned())), + Value::Array(v) => Value::Array(v.to_owned()), + } + } } /// Wrapper for string-like values @@ -336,7 +319,7 @@ impl From> for StringValue { } } -impl Value { +impl Value<'_> { /// String representation of the `Value` /// /// This will allocate iff the underlying value is not a `String`. @@ -345,7 +328,7 @@ impl Value { Value::Bool(v) => format!("{}", v).into(), Value::I64(v) => format!("{}", v).into(), Value::F64(v) => format!("{}", v).into(), - Value::String(v) => Cow::Borrowed(v.as_str()), + Value::String(v) => Cow::Borrowed(v), Value::Array(v) => format!("{}", v).into(), } } @@ -354,13 +337,13 @@ impl Value { macro_rules! from_values { ( $( - ($t:ty, $val:expr); + ($t:ty, $variant:path); )+ ) => { $( - impl From<$t> for Value { - fn from(t: $t) -> Self { - $val(t) + impl From<$t> for Value<'_> { + fn from(val: $t) -> Self { + $variant(Cow::Owned(val)) } } )+ @@ -371,40 +354,39 @@ from_values!( (bool, Value::Bool); (i64, Value::I64); (f64, Value::F64); - (StringValue, Value::String); ); -impl From<&'static str> for Value { - fn from(s: &'static str) -> Self { - Value::String(s.into()) +impl<'a> From<&'a str> for Value<'a> { + fn from(s: &'a str) -> Self { + Value::String(Cow::Borrowed(s)) } } -impl From for Value { +impl From for Value<'static> { fn from(s: String) -> Self { - Value::String(s.into()) + Value::String(Cow::Owned(s)) } } -impl From> for Value { - fn from(s: Arc) -> Self { - Value::String(s.into()) +impl From for Value<'static> { + fn from(s: StringValue) -> Self { + Value::String(Cow::Owned(s.into())) } } -impl From> for Value { +impl From> for Value<'_> { fn from(s: Cow<'static, str>) -> Self { - Value::String(s.into()) + Value::String(s) } } -impl fmt::Display for Value { +impl fmt::Display for Value<'_> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Value::Bool(v) => v.fmt(fmt), Value::I64(v) => v.fmt(fmt), Value::F64(v) => v.fmt(fmt), - Value::String(v) => fmt.write_str(v.as_str()), + Value::String(v) => fmt.write_str(v.as_ref()), Value::Array(v) => v.fmt(fmt), } } @@ -412,26 +394,34 @@ impl fmt::Display for Value { /// A key-value pair describing an attribute. #[derive(Clone, Debug, PartialEq)] -pub struct KeyValue { +pub struct KeyValue<'a> { /// The attribute name pub key: Key, /// The attribute value - pub value: Value, + pub value: Value<'a>, } -impl KeyValue { +impl<'a> KeyValue<'a> { /// Create a new `KeyValue` pair. pub fn new(key: K, value: V) -> Self where K: Into, - V: Into, + V: Into>, { KeyValue { key: key.into(), value: value.into(), } } + + /// Returns an owned version of the `KeyValue` + pub fn to_owned(&self) -> KeyValue<'static> { + KeyValue { + key: self.key.clone(), + value: self.value.to_owned(), + } + } } /// Marker trait for errors returned by exporters @@ -474,7 +464,7 @@ pub struct InstrumentationLibrary { pub schema_url: Option>, /// Specifies the instrumentation scope attributes to associate with emitted telemetry. - pub attributes: Vec, + pub attributes: Vec>, } // Uniqueness for InstrumentationLibrary/InstrumentationScope does not depend on attributes @@ -505,7 +495,7 @@ impl InstrumentationLibrary { name: impl Into>, version: Option>>, schema_url: Option>>, - attributes: Option>, + attributes: Option>>, ) -> InstrumentationLibrary { InstrumentationLibrary { name: name.into(), @@ -545,7 +535,7 @@ pub struct InstrumentationLibraryBuilder { schema_url: Option>, - attributes: Option>, + attributes: Option>>, } impl InstrumentationLibraryBuilder { @@ -590,7 +580,7 @@ impl InstrumentationLibraryBuilder { /// ``` pub fn with_attributes(mut self, attributes: I) -> Self where - I: IntoIterator, + I: IntoIterator>, { self.attributes = Some(attributes.into_iter().collect()); self diff --git a/opentelemetry/src/global/metrics.rs b/opentelemetry/src/global/metrics.rs index b85354bee4..88b57aac07 100644 --- a/opentelemetry/src/global/metrics.rs +++ b/opentelemetry/src/global/metrics.rs @@ -24,7 +24,7 @@ pub trait ObjectSafeMeterProvider { name: Cow<'static, str>, version: Option>, schema_url: Option>, - attributes: Option>, + attributes: Option>>, ) -> Meter; } @@ -38,7 +38,7 @@ where name: Cow<'static, str>, version: Option>, schema_url: Option>, - attributes: Option>, + attributes: Option>>, ) -> Meter { self.versioned_meter(name, version, schema_url, attributes) } @@ -63,7 +63,7 @@ impl MeterProvider for GlobalMeterProvider { name: impl Into>, version: Option>>, schema_url: Option>>, - attributes: Option>, + attributes: Option>>, ) -> Meter { self.provider.versioned_meter_cow( name.into(), @@ -141,7 +141,7 @@ pub fn meter_with_version( name: impl Into>, version: Option>>, schema_url: Option>>, - attributes: Option>, + attributes: Option>>, ) -> Meter { meter_provider().versioned_meter( name.into(), diff --git a/opentelemetry/src/global/trace.rs b/opentelemetry/src/global/trace.rs index d3058dc533..2033b219b4 100644 --- a/opentelemetry/src/global/trace.rs +++ b/opentelemetry/src/global/trace.rs @@ -23,7 +23,7 @@ pub trait ObjectSafeSpan { &mut self, name: Cow<'static, str>, timestamp: SystemTime, - attributes: Vec, + attributes: Vec>, ); /// Returns the `SpanContext` for the given `Span`. The returned value may be used even after @@ -61,7 +61,7 @@ pub trait ObjectSafeSpan { /// Note that the OpenTelemetry project documents certain ["standard /// attributes"](https://github.com/open-telemetry/opentelemetry-specification/tree/v0.5.0/specification/trace/semantic_conventions/README.md) /// that have prescribed semantic meanings. - fn set_attribute(&mut self, attribute: KeyValue); + fn set_attribute(&mut self, attribute: KeyValue<'static>); /// Sets the status of the `Span`. `message` MUST be ignored when the status is `OK` or /// `Unset`. @@ -88,7 +88,7 @@ pub trait ObjectSafeSpan { /// Adds a link to this span /// - fn add_link(&mut self, span_context: SpanContext, attributes: Vec); + fn add_link(&mut self, span_context: SpanContext, attributes: Vec>); /// Finishes the `Span`. /// @@ -117,7 +117,7 @@ impl ObjectSafeSpan for T { &mut self, name: Cow<'static, str>, timestamp: SystemTime, - attributes: Vec, + attributes: Vec>, ) { self.add_event_with_timestamp(name, timestamp, attributes) } @@ -130,7 +130,7 @@ impl ObjectSafeSpan for T { self.is_recording() } - fn set_attribute(&mut self, attribute: KeyValue) { + fn set_attribute(&mut self, attribute: KeyValue<'static>) { self.set_attribute(attribute) } @@ -142,7 +142,7 @@ impl ObjectSafeSpan for T { self.update_name(new_name) } - fn add_link(&mut self, span_context: SpanContext, attributes: Vec) { + fn add_link(&mut self, span_context: SpanContext, attributes: Vec>) { self.add_link(span_context, attributes) } @@ -182,7 +182,7 @@ impl trace::Span for BoxedSpan { &mut self, name: T, timestamp: SystemTime, - attributes: Vec, + attributes: Vec>, ) where T: Into>, { @@ -206,7 +206,7 @@ impl trace::Span for BoxedSpan { /// Note that the OpenTelemetry project documents certain ["standard /// attributes"](https://github.com/open-telemetry/opentelemetry-specification/tree/v0.5.0/specification/trace/semantic_conventions/README.md) /// that have prescribed semantic meanings. - fn set_attribute(&mut self, attribute: KeyValue) { + fn set_attribute(&mut self, attribute: KeyValue<'static>) { self.0.set_attribute(attribute) } @@ -226,7 +226,7 @@ impl trace::Span for BoxedSpan { /// Adds a link to this span /// - fn add_link(&mut self, span_context: trace::SpanContext, attributes: Vec) { + fn add_link(&mut self, span_context: trace::SpanContext, attributes: Vec>) { self.0.add_link(span_context, attributes) } diff --git a/opentelemetry/src/logs/logger.rs b/opentelemetry/src/logs/logger.rs index fd4e18e043..0e97a7cf0d 100644 --- a/opentelemetry/src/logs/logger.rs +++ b/opentelemetry/src/logs/logger.rs @@ -44,7 +44,7 @@ pub trait LoggerProvider { name: impl Into>, version: Option>, schema_url: Option>, - attributes: Option>, + attributes: Option>>, ) -> Self::Logger { let mut builder = self.logger_builder(name); if let Some(v) = version { @@ -144,7 +144,7 @@ impl<'a, T: LoggerProvider + ?Sized> LoggerBuilder<'a, T> { pub fn with_attributes(mut self, attributes: I) -> Self where - I: IntoIterator, + I: IntoIterator>, { self.library_builder = self.library_builder.with_attributes(attributes); self diff --git a/opentelemetry/src/logs/noop.rs b/opentelemetry/src/logs/noop.rs index 8c31328e5d..b564f03bc4 100644 --- a/opentelemetry/src/logs/noop.rs +++ b/opentelemetry/src/logs/noop.rs @@ -28,7 +28,7 @@ impl LoggerProvider for NoopLoggerProvider { _name: impl Into>, _version: Option>, _schema_url: Option>, - _attributes: Option>, + _attributes: Option>>, ) -> Self::Logger { NoopLogger(()) } diff --git a/opentelemetry/src/logs/record.rs b/opentelemetry/src/logs/record.rs index 2e171ef0a1..17b4071220 100644 --- a/opentelemetry/src/logs/record.rs +++ b/opentelemetry/src/logs/record.rs @@ -1,4 +1,4 @@ -use crate::{Array, Key, StringValue, Value}; +use crate::{Key, StringValue}; use std::{borrow::Cow, collections::HashMap, time::SystemTime}; /// SDK implemented trait for managing log records @@ -108,23 +108,6 @@ impl, V: Into> FromIterator<(K, V)> for AnyValue { } } -impl From for AnyValue { - fn from(value: Value) -> Self { - match value { - Value::Bool(b) => b.into(), - Value::I64(i) => i.into(), - Value::F64(f) => f.into(), - Value::String(s) => s.into(), - Value::Array(a) => match a { - Array::Bool(b) => AnyValue::from_iter(b), - Array::F64(f) => AnyValue::from_iter(f), - Array::I64(i) => AnyValue::from_iter(i), - Array::String(s) => AnyValue::from_iter(s), - }, - } - } -} - /// A normalized severity value. #[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)] pub enum Severity { diff --git a/opentelemetry/src/metrics/instruments/counter.rs b/opentelemetry/src/metrics/instruments/counter.rs index 3972580628..80e627f1fd 100644 --- a/opentelemetry/src/metrics/instruments/counter.rs +++ b/opentelemetry/src/metrics/instruments/counter.rs @@ -9,7 +9,7 @@ use std::sync::Arc; /// An SDK implemented instrument that records increasing values. pub trait SyncCounter { /// Records an increment to the counter. - fn add(&self, value: T, attributes: &[KeyValue]); + fn add(&self, value: T, attributes: &[KeyValue<'_>]); } /// An instrument that records increasing values. @@ -32,7 +32,7 @@ impl Counter { } /// Records an increment to the counter. - pub fn add(&self, value: T, attributes: &[KeyValue]) { + pub fn add(&self, value: T, attributes: &[KeyValue<'_>]) { self.0.add(value, attributes) } } @@ -83,7 +83,7 @@ impl ObservableCounter { /// It is only valid to call this within a callback. If called outside of the /// registered callback it should have no effect on the instrument, and an /// error will be reported via the error handler. - pub fn observe(&self, value: T, attributes: &[KeyValue]) { + pub fn observe(&self, value: T, attributes: &[KeyValue<'_>]) { self.0.observe(value, attributes) } @@ -94,7 +94,7 @@ impl ObservableCounter { } impl AsyncInstrument for ObservableCounter { - fn observe(&self, measurement: T, attributes: &[KeyValue]) { + fn observe(&self, measurement: T, attributes: &[KeyValue<'_>]) { self.0.observe(measurement, attributes) } diff --git a/opentelemetry/src/metrics/instruments/gauge.rs b/opentelemetry/src/metrics/instruments/gauge.rs index 274134aaba..d8d45c6f19 100644 --- a/opentelemetry/src/metrics/instruments/gauge.rs +++ b/opentelemetry/src/metrics/instruments/gauge.rs @@ -9,7 +9,7 @@ use std::sync::Arc; /// An SDK implemented instrument that records independent values pub trait SyncGauge { /// Records an independent value. - fn record(&self, value: T, attributes: &[KeyValue]); + fn record(&self, value: T, attributes: &[KeyValue<'_>]); } /// An instrument that records independent values @@ -32,7 +32,7 @@ impl Gauge { } /// Records an independent value. - pub fn record(&self, value: T, attributes: &[KeyValue]) { + pub fn record(&self, value: T, attributes: &[KeyValue<'_>]) { self.0.record(value, attributes) } } @@ -89,7 +89,7 @@ impl ObservableGauge { /// It is only valid to call this within a callback. If called outside of the /// registered callback it should have no effect on the instrument, and an /// error will be reported via the error handler. - pub fn observe(&self, measurement: T, attributes: &[KeyValue]) { + pub fn observe(&self, measurement: T, attributes: &[KeyValue<'_>]) { self.0.observe(measurement, attributes) } @@ -100,7 +100,7 @@ impl ObservableGauge { } impl AsyncInstrument for ObservableGauge { - fn observe(&self, measurement: M, attributes: &[KeyValue]) { + fn observe(&self, measurement: M, attributes: &[KeyValue<'_>]) { self.observe(measurement, attributes) } diff --git a/opentelemetry/src/metrics/instruments/histogram.rs b/opentelemetry/src/metrics/instruments/histogram.rs index 167da10f7c..97727eda4d 100644 --- a/opentelemetry/src/metrics/instruments/histogram.rs +++ b/opentelemetry/src/metrics/instruments/histogram.rs @@ -8,7 +8,7 @@ use std::sync::Arc; /// An SDK implemented instrument that records a distribution of values. pub trait SyncHistogram { /// Adds an additional value to the distribution. - fn record(&self, value: T, attributes: &[KeyValue]); + fn record(&self, value: T, attributes: &[KeyValue<'_>]); } /// An instrument that records a distribution of values. @@ -31,7 +31,7 @@ impl Histogram { } /// Adds an additional value to the distribution. - pub fn record(&self, value: T, attributes: &[KeyValue]) { + pub fn record(&self, value: T, attributes: &[KeyValue<'_>]) { self.0.record(value, attributes) } } diff --git a/opentelemetry/src/metrics/instruments/mod.rs b/opentelemetry/src/metrics/instruments/mod.rs index 3ee530453a..7c0100d26d 100644 --- a/opentelemetry/src/metrics/instruments/mod.rs +++ b/opentelemetry/src/metrics/instruments/mod.rs @@ -18,7 +18,7 @@ pub trait AsyncInstrument: Send + Sync { /// Observes the state of the instrument. /// /// It is only valid to call this within a callback. - fn observe(&self, measurement: T, attributes: &[KeyValue]); + fn observe(&self, measurement: T, attributes: &[KeyValue<'_>]); /// Used for SDKs to downcast instruments in callbacks. fn as_any(&self) -> Arc; diff --git a/opentelemetry/src/metrics/instruments/up_down_counter.rs b/opentelemetry/src/metrics/instruments/up_down_counter.rs index f7a2b5e8c2..4f30dabcda 100644 --- a/opentelemetry/src/metrics/instruments/up_down_counter.rs +++ b/opentelemetry/src/metrics/instruments/up_down_counter.rs @@ -11,7 +11,7 @@ use super::{AsyncInstrument, AsyncInstrumentBuilder}; /// An SDK implemented instrument that records increasing or decreasing values. pub trait SyncUpDownCounter { /// Records an increment or decrement to the counter. - fn add(&self, value: T, attributes: &[KeyValue]); + fn add(&self, value: T, attributes: &[KeyValue<'_>]); } /// An instrument that records increasing or decreasing values. @@ -37,7 +37,7 @@ impl UpDownCounter { } /// Records an increment or decrement to the counter. - pub fn add(&self, value: T, attributes: &[KeyValue]) { + pub fn add(&self, value: T, attributes: &[KeyValue<'_>]) { self.0.add(value, attributes) } } @@ -93,7 +93,7 @@ impl ObservableUpDownCounter { /// It is only valid to call this within a callback. If called outside of the /// registered callback it should have no effect on the instrument, and an /// error will be reported via the error handler. - pub fn observe(&self, value: T, attributes: &[KeyValue]) { + pub fn observe(&self, value: T, attributes: &[KeyValue<'_>]) { self.0.observe(value, attributes) } @@ -104,7 +104,7 @@ impl ObservableUpDownCounter { } impl AsyncInstrument for ObservableUpDownCounter { - fn observe(&self, measurement: T, attributes: &[KeyValue]) { + fn observe(&self, measurement: T, attributes: &[KeyValue<'_>]) { self.0.observe(measurement, attributes) } diff --git a/opentelemetry/src/metrics/meter.rs b/opentelemetry/src/metrics/meter.rs index 3422240b48..8edcb7e8dc 100644 --- a/opentelemetry/src/metrics/meter.rs +++ b/opentelemetry/src/metrics/meter.rs @@ -59,7 +59,7 @@ pub trait MeterProvider { name: impl Into>, version: Option>>, schema_url: Option>>, - attributes: Option>, + attributes: Option>>, ) -> Meter; } diff --git a/opentelemetry/src/metrics/mod.rs b/opentelemetry/src/metrics/mod.rs index f929c16d27..48fe2431b7 100644 --- a/opentelemetry/src/metrics/mod.rs +++ b/opentelemetry/src/metrics/mod.rs @@ -72,11 +72,11 @@ impl Hash for F64Hashable { } } -impl Hash for KeyValue { +impl Hash for KeyValue<'_> { fn hash(&self, state: &mut H) { self.key.hash(state); match &self.value { - Value::F64(f) => F64Hashable(*f).hash(state), + Value::F64(f) => F64Hashable(**f).hash(state), Value::Array(a) => match a { Array::Bool(b) => b.hash(state), Array::I64(i) => i.hash(state), @@ -90,20 +90,20 @@ impl Hash for KeyValue { } } -impl PartialOrd for KeyValue { +impl PartialOrd for KeyValue<'_> { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } /// Ordering is based on the key only. -impl Ord for KeyValue { +impl Ord for KeyValue<'_> { fn cmp(&self, other: &Self) -> Ordering { self.key.cmp(&other.key) } } -impl Eq for KeyValue {} +impl Eq for KeyValue<'_> {} /// SDK implemented trait for creating instruments pub trait InstrumentProvider { diff --git a/opentelemetry/src/metrics/noop.rs b/opentelemetry/src/metrics/noop.rs index 716e4ca3c8..ee2d768f81 100644 --- a/opentelemetry/src/metrics/noop.rs +++ b/opentelemetry/src/metrics/noop.rs @@ -31,7 +31,7 @@ impl MeterProvider for NoopMeterProvider { _name: impl Into>, _version: Option>>, _schema_url: Option>>, - _attributes: Option>, + _attributes: Option>>, ) -> Meter { Meter::new(Arc::new(NoopMeterCore::new())) } @@ -66,25 +66,25 @@ impl NoopSyncInstrument { } impl SyncCounter for NoopSyncInstrument { - fn add(&self, _value: T, _attributes: &[KeyValue]) { + fn add(&self, _value: T, _attributes: &[KeyValue<'_>]) { // Ignored } } impl SyncUpDownCounter for NoopSyncInstrument { - fn add(&self, _value: T, _attributes: &[KeyValue]) { + fn add(&self, _value: T, _attributes: &[KeyValue<'_>]) { // Ignored } } impl SyncHistogram for NoopSyncInstrument { - fn record(&self, _value: T, _attributes: &[KeyValue]) { + fn record(&self, _value: T, _attributes: &[KeyValue<'_>]) { // Ignored } } impl SyncGauge for NoopSyncInstrument { - fn record(&self, _value: T, _attributes: &[KeyValue]) { + fn record(&self, _value: T, _attributes: &[KeyValue<'_>]) { // Ignored } } @@ -103,7 +103,7 @@ impl NoopAsyncInstrument { } impl AsyncInstrument for NoopAsyncInstrument { - fn observe(&self, _value: T, _attributes: &[KeyValue]) { + fn observe(&self, _value: T, _attributes: &[KeyValue<'_>]) { // Ignored } diff --git a/opentelemetry/src/testing/trace.rs b/opentelemetry/src/testing/trace.rs index 040d22d314..0df3f7278b 100644 --- a/opentelemetry/src/testing/trace.rs +++ b/opentelemetry/src/testing/trace.rs @@ -12,7 +12,7 @@ impl Span for TestSpan { &mut self, _name: T, _timestamp: std::time::SystemTime, - _attributes: Vec, + _attributes: Vec>, ) where T: Into>, { @@ -23,7 +23,7 @@ impl Span for TestSpan { fn is_recording(&self) -> bool { false } - fn set_attribute(&mut self, _attribute: KeyValue) {} + fn set_attribute(&mut self, _attribute: KeyValue<'static>) {} fn set_status(&mut self, _status: Status) {} fn update_name(&mut self, _new_name: T) where @@ -31,7 +31,7 @@ impl Span for TestSpan { { } - fn add_link(&mut self, _span_context: SpanContext, _attributes: Vec) {} + fn add_link(&mut self, _span_context: SpanContext, _attributes: Vec>) {} fn end_with_timestamp(&mut self, _timestamp: std::time::SystemTime) {} } diff --git a/opentelemetry/src/trace/context.rs b/opentelemetry/src/trace/context.rs index 681c7b2e0c..0519db89e4 100644 --- a/opentelemetry/src/trace/context.rs +++ b/opentelemetry/src/trace/context.rs @@ -70,7 +70,7 @@ impl SpanRef<'_> { /// /// [standard attributes]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/semantic_conventions/README.md /// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions - pub fn add_event(&self, name: T, attributes: Vec) + pub fn add_event(&self, name: T, attributes: Vec>) where T: Into>, { @@ -99,7 +99,7 @@ impl SpanRef<'_> { &self, name: T, timestamp: std::time::SystemTime, - attributes: Vec, + attributes: Vec>, ) where T: Into>, { @@ -142,7 +142,7 @@ impl SpanRef<'_> { /// /// [standard attributes]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/semantic_conventions/README.md /// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions - pub fn set_attribute(&self, attribute: crate::KeyValue) { + pub fn set_attribute(&self, attribute: crate::KeyValue<'static>) { self.with_inner_mut(move |inner| inner.set_attribute(attribute)) } @@ -157,7 +157,7 @@ impl SpanRef<'_> { /// /// [standard attributes]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/semantic_conventions/README.md /// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions - pub fn set_attributes(&self, attributes: impl IntoIterator) { + pub fn set_attributes(&self, attributes: impl IntoIterator>) { self.with_inner_mut(move |inner| inner.set_attributes(attributes)) } diff --git a/opentelemetry/src/trace/mod.rs b/opentelemetry/src/trace/mod.rs index 5e3edc3519..24869587a3 100644 --- a/opentelemetry/src/trace/mod.rs +++ b/opentelemetry/src/trace/mod.rs @@ -248,7 +248,7 @@ pub struct Event { pub timestamp: time::SystemTime, /// Attributes that describe this event. - pub attributes: Vec, + pub attributes: Vec>, /// The number of attributes that were above the configured limit, and thus /// dropped. @@ -260,7 +260,7 @@ impl Event { pub fn new>>( name: T, timestamp: time::SystemTime, - attributes: Vec, + attributes: Vec>, dropped_attributes_count: u32, ) -> Self { Event { @@ -292,7 +292,7 @@ pub struct Link { pub span_context: SpanContext, /// Attributes that describe this link. - pub attributes: Vec, + pub attributes: Vec>, /// The number of attributes that were above the configured limit, and thus /// dropped. @@ -303,7 +303,7 @@ impl Link { /// Create new `Link` pub fn new( span_context: SpanContext, - attributes: Vec, + attributes: Vec>, dropped_attributes_count: u32, ) -> Self { Link { diff --git a/opentelemetry/src/trace/noop.rs b/opentelemetry/src/trace/noop.rs index d46a657a2e..59c09fbb92 100644 --- a/opentelemetry/src/trace/noop.rs +++ b/opentelemetry/src/trace/noop.rs @@ -47,7 +47,7 @@ impl NoopSpan { impl trace::Span for NoopSpan { /// Ignores all events - fn add_event(&mut self, _name: T, _attributes: Vec) + fn add_event(&mut self, _name: T, _attributes: Vec>) where T: Into>, { @@ -59,7 +59,7 @@ impl trace::Span for NoopSpan { &mut self, _name: T, _timestamp: SystemTime, - _attributes: Vec, + _attributes: Vec>, ) where T: Into>, { @@ -77,7 +77,7 @@ impl trace::Span for NoopSpan { } /// Ignores all attributes - fn set_attribute(&mut self, _attribute: KeyValue) { + fn set_attribute(&mut self, _attribute: KeyValue<'static>) { // Ignored } @@ -94,7 +94,7 @@ impl trace::Span for NoopSpan { // Ignored } - fn add_link(&mut self, _span_context: trace::SpanContext, _attributes: Vec) { + fn add_link(&mut self, _span_context: trace::SpanContext, _attributes: Vec>) { // Ignored } diff --git a/opentelemetry/src/trace/span.rs b/opentelemetry/src/trace/span.rs index 8b54fffb53..84c0053abb 100644 --- a/opentelemetry/src/trace/span.rs +++ b/opentelemetry/src/trace/span.rs @@ -56,7 +56,7 @@ pub trait Span { /// /// [standard attributes]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/semantic_conventions/README.md /// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions - fn add_event(&mut self, name: T, attributes: Vec) + fn add_event(&mut self, name: T, attributes: Vec>) where T: Into>, { @@ -88,7 +88,7 @@ pub trait Span { &mut self, name: T, timestamp: SystemTime, - attributes: Vec, + attributes: Vec>, ) where T: Into>; @@ -119,7 +119,7 @@ pub trait Span { /// /// [standard attributes]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/semantic_conventions/README.md /// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions - fn set_attribute(&mut self, attribute: KeyValue); + fn set_attribute(&mut self, attribute: KeyValue<'static>); /// Set multiple attributes of this span. /// @@ -133,7 +133,7 @@ pub trait Span { /// /// [standard attributes]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/semantic_conventions/README.md /// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions - fn set_attributes(&mut self, attributes: impl IntoIterator) { + fn set_attributes(&mut self, attributes: impl IntoIterator>) { if self.is_recording() { for attr in attributes.into_iter() { self.set_attribute(attr); @@ -168,7 +168,7 @@ pub trait Span { /// can include any contextual information relevant to the link between the spans. /// /// [`Link`]: crate::trace::Link - fn add_link(&mut self, span_context: SpanContext, attributes: Vec); + fn add_link(&mut self, span_context: SpanContext, attributes: Vec>); /// Signals that the operation described by this span has now ended. fn end(&mut self) { diff --git a/opentelemetry/src/trace/tracer.rs b/opentelemetry/src/trace/tracer.rs index cc8554c216..36d682bb71 100644 --- a/opentelemetry/src/trace/tracer.rs +++ b/opentelemetry/src/trace/tracer.rs @@ -262,7 +262,7 @@ pub struct SpanBuilder { /// More attributes can be added afterwards. /// Providing duplicate keys will result in multiple attributes /// with the same key, as there is no de-duplication performed. - pub attributes: Option>, + pub attributes: Option>>, /// Span events pub events: Option>, @@ -332,7 +332,7 @@ impl SpanBuilder { /// with the same key, as there is no de-duplication performed. pub fn with_attributes(self, attributes: I) -> Self where - I: IntoIterator, + I: IntoIterator>, { SpanBuilder { attributes: Some(attributes.into_iter().collect()), @@ -388,7 +388,7 @@ pub struct SamplingResult { pub decision: SamplingDecision, /// Extra attributes to be added to the span by the sampler - pub attributes: Vec, + pub attributes: Vec>, /// Trace state from parent context, may be modified by samplers. pub trace_state: TraceState, diff --git a/opentelemetry/src/trace/tracer_provider.rs b/opentelemetry/src/trace/tracer_provider.rs index c17c3fdd58..7ba7ee6c28 100644 --- a/opentelemetry/src/trace/tracer_provider.rs +++ b/opentelemetry/src/trace/tracer_provider.rs @@ -71,7 +71,7 @@ pub trait TracerProvider { name: impl Into>, version: Option>>, schema_url: Option>>, - attributes: Option>, + attributes: Option>>, ) -> Self::Tracer { let mut builder = self.tracer_builder(name); if let Some(v) = version { @@ -160,7 +160,7 @@ impl<'a, T: TracerProvider + ?Sized> TracerBuilder<'a, T> { pub fn with_attributes(mut self, attributes: I) -> Self where - I: IntoIterator, + I: IntoIterator>, { self.library_builder = self.library_builder.with_attributes(attributes); self