diff --git a/opentelemetry-appender-log/src/lib.rs b/opentelemetry-appender-log/src/lib.rs index f20b5f65ff..786e53ab9f 100644 --- a/opentelemetry-appender-log/src/lib.rs +++ b/opentelemetry-appender-log/src/lib.rs @@ -97,11 +97,11 @@ use log::{Level, Metadata, Record}; use opentelemetry::{ logs::{AnyValue, LogRecord, Logger, LoggerProvider, Severity}, - Key, + InstrumentationLibrary, Key, }; #[cfg(feature = "experimental_metadata_attributes")] use opentelemetry_semantic_conventions::attribute::{CODE_FILEPATH, CODE_LINENO, CODE_NAMESPACE}; -use std::borrow::Cow; +use std::sync::Arc; pub struct OpenTelemetryLogBridge where @@ -170,11 +170,10 @@ where L: Logger + Send + Sync, { pub fn new(provider: &P) -> Self { + let library = + Arc::new(InstrumentationLibrary::builder("opentelemetry-log-appender").build()); OpenTelemetryLogBridge { - logger: provider - .logger_builder("opentelemetry-log-appender") - .with_version(Cow::Borrowed(env!("CARGO_PKG_VERSION"))) - .build(), + logger: provider.library_logger(library), _phantom: Default::default(), } } diff --git a/opentelemetry-appender-tracing/src/layer.rs b/opentelemetry-appender-tracing/src/layer.rs index fbbf8e4d97..46ed73d5eb 100644 --- a/opentelemetry-appender-tracing/src/layer.rs +++ b/opentelemetry-appender-tracing/src/layer.rs @@ -1,8 +1,8 @@ use opentelemetry::{ logs::{AnyValue, LogRecord, Logger, LoggerProvider, Severity}, - Key, + InstrumentationLibrary, Key, }; -use std::borrow::Cow; +use std::{borrow::Cow, sync::Arc}; use tracing_core::Level; #[cfg(feature = "experimental_metadata_attributes")] use tracing_core::Metadata; @@ -136,11 +136,14 @@ where L: Logger + Send + Sync, { pub fn new(provider: &P) -> Self { - OpenTelemetryTracingBridge { - logger: provider - .logger_builder(INSTRUMENTATION_LIBRARY_NAME) + let library = Arc::new( + InstrumentationLibrary::builder(INSTRUMENTATION_LIBRARY_NAME) .with_version(Cow::Borrowed(env!("CARGO_PKG_VERSION"))) .build(), + ); + + OpenTelemetryTracingBridge { + logger: provider.library_logger(library), _phantom: Default::default(), } } diff --git a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs index 006d8e4e2e..e6b2ee5364 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 _}, - KeyValue, + InstrumentationLibrary, KeyValue, }; use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge; use opentelemetry_otlp::Protocol; @@ -13,7 +13,7 @@ use opentelemetry_sdk::{ logs::{self as sdklogs}, Resource, }; -use std::error::Error; +use std::{error::Error, sync::Arc}; use tracing::info; use tracing_subscriber::prelude::*; use tracing_subscriber::EnvFilter; @@ -121,16 +121,14 @@ async fn main() -> Result<(), Box> { .init(); let common_scope_attributes = vec![KeyValue::new("scope-key", "scope-value")]; - let tracer = global::tracer_provider() - .tracer_builder("basic") - .with_attributes(common_scope_attributes.clone()) - .build(); - let meter = global::meter_with_version( - "basic", - Some("v1.0"), - Some("schema_url"), - Some(common_scope_attributes.clone()), + let library = Arc::new( + InstrumentationLibrary::builder("basic") + .with_version("1.0") + .with_attributes(common_scope_attributes.clone()) + .build(), ); + let tracer = global::tracer_provider().library_tracer(library.clone()); + let meter = global::library_meter(library); let counter = meter .u64_counter("test_counter") diff --git a/opentelemetry-otlp/examples/basic-otlp/src/main.rs b/opentelemetry-otlp/examples/basic-otlp/src/main.rs index f931e592e2..a05d87ae4f 100644 --- a/opentelemetry-otlp/examples/basic-otlp/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp/src/main.rs @@ -1,17 +1,15 @@ use once_cell::sync::Lazy; -use opentelemetry::global; use opentelemetry::logs::LogError; use opentelemetry::metrics::MetricsError; -use opentelemetry::trace::{TraceError, TracerProvider}; -use opentelemetry::{ - trace::{TraceContextExt, Tracer}, - KeyValue, -}; +use opentelemetry::trace::{TraceContextExt, TraceError, Tracer, TracerProvider}; +use opentelemetry::KeyValue; +use opentelemetry::{global, InstrumentationLibrary}; use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge; use opentelemetry_otlp::{ExportConfig, WithExportConfig}; use opentelemetry_sdk::trace::Config; use opentelemetry_sdk::{runtime, trace as sdktrace, Resource}; use std::error::Error; +use std::sync::Arc; use tracing::info; use tracing_subscriber::prelude::*; use tracing_subscriber::EnvFilter; @@ -112,16 +110,14 @@ async fn main() -> Result<(), Box> { .init(); let common_scope_attributes = vec![KeyValue::new("scope-key", "scope-value")]; - let tracer = global::tracer_provider() - .tracer_builder("basic") - .with_attributes(common_scope_attributes.clone()) - .build(); - let meter = global::meter_with_version( - "basic", - Some("v1.0"), - Some("schema_url"), - Some(common_scope_attributes.clone()), + let library = Arc::new( + InstrumentationLibrary::builder("basic") + .with_version("1.0") + .with_attributes(common_scope_attributes.clone()) + .build(), ); + let tracer = global::tracer_provider().library_tracer(library.clone()); + let meter = global::library_meter(library); let counter = meter .u64_counter("test_counter") diff --git a/opentelemetry-sdk/src/logs/log_emitter.rs b/opentelemetry-sdk/src/logs/log_emitter.rs index 71b02dc158..2f3150fefa 100644 --- a/opentelemetry-sdk/src/logs/log_emitter.rs +++ b/opentelemetry-sdk/src/logs/log_emitter.rs @@ -10,11 +10,11 @@ use opentelemetry::{ #[cfg(feature = "logs_level_enabled")] use opentelemetry::logs::Severity; -use std::{ - borrow::Cow, - sync::{atomic::Ordering, Arc}, +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, }; -use std::{sync::atomic::AtomicBool, time::SystemTime}; +use std::time::SystemTime; use once_cell::sync::Lazy; @@ -45,42 +45,9 @@ pub struct LoggerProvider { inner: Arc, } -/// Default logger name if empty string is provided. -const DEFAULT_COMPONENT_NAME: &str = "rust.opentelemetry.io/sdk/logger"; - impl opentelemetry::logs::LoggerProvider for LoggerProvider { type Logger = Logger; - /// Create a new versioned `Logger` instance. - fn versioned_logger( - &self, - name: impl Into>, - version: Option>, - schema_url: Option>, - attributes: Option>, - ) -> Logger { - let name = name.into(); - let component_name = if name.is_empty() { - Cow::Borrowed(DEFAULT_COMPONENT_NAME) - } else { - name - }; - - let mut builder = self.logger_builder(component_name); - - if let Some(v) = version { - builder = builder.with_version(v); - } - if let Some(s) = schema_url { - builder = builder.with_schema_url(s); - } - if let Some(a) = attributes { - builder = builder.with_attributes(a); - } - - builder.build() - } - fn library_logger(&self, library: Arc) -> Self::Logger { // If the provider is shutdown, new logger will refer a no-op logger provider. if self.inner.is_shutdown.load(Ordering::Relaxed) { diff --git a/opentelemetry-sdk/src/logs/mod.rs b/opentelemetry-sdk/src/logs/mod.rs index 59b1b9b338..b71cbcd190 100644 --- a/opentelemetry-sdk/src/logs/mod.rs +++ b/opentelemetry-sdk/src/logs/mod.rs @@ -30,6 +30,7 @@ mod tests { use opentelemetry::{logs::AnyValue, Key, KeyValue}; use std::borrow::Borrow; use std::collections::HashMap; + use std::sync::Arc; #[test] fn logging_sdk_test() { @@ -104,36 +105,16 @@ mod tests { #[test] fn logger_attributes() { let provider = LoggerProvider::builder().build(); - let logger = provider - .logger_builder("test_logger") - .with_schema_url("https://opentelemetry.io/schema/1.0.0") - .with_attributes(vec![(KeyValue::new("test_k", "test_v"))]) - .build(); - let instrumentation_library = logger.instrumentation_library(); - let attributes = &instrumentation_library.attributes; - assert_eq!(instrumentation_library.name, "test_logger"); - assert_eq!( - instrumentation_library.schema_url, - Some("https://opentelemetry.io/schema/1.0.0".into()) - ); - assert_eq!(attributes.len(), 1); - assert_eq!(attributes[0].key, "test_k".into()); - assert_eq!(attributes[0].value, "test_v".into()); - } - - #[test] - #[allow(deprecated)] - fn versioned_logger_options() { - let provider = LoggerProvider::builder().build(); - let logger = provider.versioned_logger( - "test_logger", - Some("v1.2.3".into()), - Some("https://opentelemetry.io/schema/1.0.0".into()), - Some(vec![(KeyValue::new("test_k", "test_v"))]), + let library = Arc::new( + InstrumentationLibrary::builder("test_logger") + .with_schema_url("https://opentelemetry.io/schema/1.0.0") + .with_attributes(vec![(KeyValue::new("test_k", "test_v"))]) + .build(), ); + let logger = provider.library_logger(library); let instrumentation_library = logger.instrumentation_library(); let attributes = &instrumentation_library.attributes; - assert_eq!(instrumentation_library.version, Some("v1.2.3".into())); + assert_eq!(instrumentation_library.name, "test_logger"); assert_eq!( instrumentation_library.schema_url, Some("https://opentelemetry.io/schema/1.0.0".into()) diff --git a/opentelemetry-sdk/src/metrics/meter_provider.rs b/opentelemetry-sdk/src/metrics/meter_provider.rs index 1468ed0f30..5998cce675 100644 --- a/opentelemetry-sdk/src/metrics/meter_provider.rs +++ b/opentelemetry-sdk/src/metrics/meter_provider.rs @@ -10,7 +10,7 @@ use std::{ use opentelemetry::{ global, metrics::{Meter, MeterProvider, MetricsError, Result}, - KeyValue, + InstrumentationLibrary, }; use crate::{instrumentation::Scope, Resource}; @@ -145,34 +145,14 @@ impl Drop for SdkMeterProviderInner { } } impl MeterProvider for SdkMeterProvider { - fn versioned_meter( - &self, - name: &'static str, - version: Option<&'static str>, - schema_url: Option<&'static str>, - attributes: Option>, - ) -> Meter { + fn library_meter(&self, library: Arc) -> Meter { if self.inner.is_shutdown.load(Ordering::Relaxed) { return Meter::new(Arc::new(NoopMeter::new())); } - let mut builder = Scope::builder(name); - - if let Some(v) = version { - builder = builder.with_version(v); - } - if let Some(s) = schema_url { - builder = builder.with_schema_url(s); - } - if let Some(a) = attributes { - builder = builder.with_attributes(a); - } - - let scope = builder.build(); - if let Ok(mut meters) = self.inner.meters.lock() { let meter = meters - .entry(scope) + .entry(InstrumentationLibrary::clone(&library)) .or_insert_with_key(|scope| { Arc::new(SdkMeter::new(scope.clone(), self.inner.pipes.clone())) }) @@ -260,10 +240,11 @@ mod tests { }; use crate::testing::metrics::metric_reader::TestMetricReader; use crate::Resource; - use opentelemetry::global; use opentelemetry::metrics::MeterProvider; + use opentelemetry::{global, InstrumentationLibrary}; use opentelemetry::{Key, KeyValue, Value}; use std::env; + use std::sync::Arc; #[test] fn test_meter_provider_resource() { @@ -454,21 +435,41 @@ mod tests { let _meter1 = provider.meter("test"); let _meter2 = provider.meter("test"); assert_eq!(provider.inner.meters.lock().unwrap().len(), 1); - let _meter3 = - provider.versioned_meter("test", Some("1.0.0"), Some("http://example.com"), None); - let _meter4 = - provider.versioned_meter("test", Some("1.0.0"), Some("http://example.com"), None); - let _meter5 = - provider.versioned_meter("test", Some("1.0.0"), Some("http://example.com"), None); + + let library = Arc::new( + InstrumentationLibrary::builder("test") + .with_version("1.0.0") + .with_schema_url("http://example.com") + .build(), + ); + + let _meter3 = provider.library_meter(library.clone()); + let _meter4 = provider.library_meter(library.clone()); + let _meter5 = provider.library_meter(library); assert_eq!(provider.inner.meters.lock().unwrap().len(), 2); - // the below are different meters, as meter names are case sensitive - let _meter6 = - provider.versioned_meter("ABC", Some("1.0.0"), Some("http://example.com"), None); - let _meter7 = - provider.versioned_meter("Abc", Some("1.0.0"), Some("http://example.com"), None); - let _meter8 = - provider.versioned_meter("abc", Some("1.0.0"), Some("http://example.com"), None); + // these are different meters because meter names are case sensitive + let library = Arc::new( + InstrumentationLibrary::builder("ABC") + .with_version("1.0.0") + .with_schema_url("http://example.com") + .build(), + ); + let _meter6 = provider.library_meter(library); + let library = Arc::new( + InstrumentationLibrary::builder("Abc") + .with_version("1.0.0") + .with_schema_url("http://example.com") + .build(), + ); + let _meter7 = provider.library_meter(library); + let library = Arc::new( + InstrumentationLibrary::builder("abc") + .with_version("1.0.0") + .with_schema_url("http://example.com") + .build(), + ); + let _meter8 = provider.library_meter(library); assert_eq!(provider.inner.meters.lock().unwrap().len(), 5); } } diff --git a/opentelemetry-sdk/src/metrics/mod.rs b/opentelemetry-sdk/src/metrics/mod.rs index 681b0af8a8..8ac06bb4c1 100644 --- a/opentelemetry-sdk/src/metrics/mod.rs +++ b/opentelemetry-sdk/src/metrics/mod.rs @@ -135,6 +135,7 @@ mod tests { use crate::testing::metrics::InMemoryMetricsExporterBuilder; use crate::{runtime, testing::metrics::InMemoryMetricsExporter}; use opentelemetry::metrics::{Counter, Meter, UpDownCounter}; + use opentelemetry::InstrumentationLibrary; use opentelemetry::{metrics::MeterProvider as _, KeyValue}; use rand::{rngs, Rng, SeedableRng}; use std::borrow::Cow; @@ -638,18 +639,24 @@ mod tests { // Act // Meters are identical except for scope attributes, but scope attributes are not an identifying property. // Hence there should be a single metric stream output for this test. - let meter1 = meter_provider.versioned_meter( - "test.meter", - Some("v0.1.0"), - Some("schema_url"), - Some(vec![KeyValue::new("key", "value1")]), + let library = Arc::new( + InstrumentationLibrary::builder("test.meter") + .with_version("v0.1.0") + .with_schema_url("http://example.com") + .with_attributes(vec![KeyValue::new("key", "value1")]) + .build(), ); - let meter2 = meter_provider.versioned_meter( - "test.meter", - Some("v0.1.0"), - Some("schema_url"), - Some(vec![KeyValue::new("key", "value2")]), + let meter1 = meter_provider.library_meter(library); + + let library = Arc::new( + InstrumentationLibrary::builder("test.meter") + .with_version("v0.1.0") + .with_schema_url("http://example.com") + .with_attributes(vec![KeyValue::new("key", "value2")]) + .build(), ); + let meter2 = meter_provider.library_meter(library); + let counter1 = meter1 .u64_counter("my_counter") .with_unit("my_unit") @@ -685,7 +692,7 @@ mod tests { let scope = &resource_metrics[0].scope_metrics[0].scope; assert_eq!(scope.name, "test.meter"); assert_eq!(scope.version, Some(Cow::Borrowed("v0.1.0"))); - assert_eq!(scope.schema_url, Some(Cow::Borrowed("schema_url"))); + assert_eq!(scope.schema_url, Some(Cow::Borrowed("http://example.com"))); // This is validating current behavior, but it is not guaranteed to be the case in the future, // as this is a user error and SDK reserves right to change this behavior. diff --git a/opentelemetry-sdk/src/trace/mod.rs b/opentelemetry-sdk/src/trace/mod.rs index ccaae00638..edf31cb68f 100644 --- a/opentelemetry-sdk/src/trace/mod.rs +++ b/opentelemetry-sdk/src/trace/mod.rs @@ -40,15 +40,17 @@ mod runtime_tests; #[cfg(all(test, feature = "testing"))] mod tests { + use std::sync::Arc; + use super::*; use crate::{ testing::trace::InMemorySpanExporterBuilder, trace::span_limit::{DEFAULT_MAX_EVENT_PER_SPAN, DEFAULT_MAX_LINKS_PER_SPAN}, }; - use opentelemetry::testing::trace::TestSpan; use opentelemetry::trace::{ SamplingDecision, SamplingResult, SpanKind, Status, TraceContextExt, TraceState, }; + use opentelemetry::{testing::trace::TestSpan, InstrumentationLibrary}; use opentelemetry::{ trace::{ Event, Link, Span, SpanBuilder, SpanContext, SpanId, TraceFlags, TraceId, Tracer, @@ -326,35 +328,14 @@ mod tests { #[test] fn tracer_attributes() { let provider = TracerProvider::builder().build(); - let tracer = provider - .tracer_builder("test_tracer") - .with_attributes(vec![KeyValue::new("test_k", "test_v")]) - .build(); - let instrumentation_library = tracer.instrumentation_library(); - let attributes = &instrumentation_library.attributes; - assert_eq!(attributes.len(), 1); - assert_eq!(attributes[0].key, "test_k".into()); - assert_eq!(attributes[0].value, "test_v".into()); - } - - #[test] - #[allow(deprecated)] - fn versioned_tracer_options() { - let provider = TracerProvider::builder().build(); - let tracer = provider.versioned_tracer( - "test_tracer", - Some(String::from("v1.2.3")), - Some(String::from("https://opentelemetry.io/schema/1.0.0")), - Some(vec![(KeyValue::new("test_k", "test_v"))]), + let library = Arc::new( + InstrumentationLibrary::builder("basic") + .with_attributes(vec![KeyValue::new("test_k", "test_v")]) + .build(), ); + let tracer = provider.library_tracer(library); let instrumentation_library = tracer.instrumentation_library(); let attributes = &instrumentation_library.attributes; - assert_eq!(instrumentation_library.name, "test_tracer"); - assert_eq!(instrumentation_library.version, Some("v1.2.3".into())); - assert_eq!( - instrumentation_library.schema_url, - Some("https://opentelemetry.io/schema/1.0.0".into()) - ); assert_eq!(attributes.len(), 1); assert_eq!(attributes[0].key, "test_k".into()); assert_eq!(attributes[0].value, "test_v".into()); diff --git a/opentelemetry-sdk/src/trace/provider.rs b/opentelemetry-sdk/src/trace/provider.rs index 9550ce11d2..da201f1eb2 100644 --- a/opentelemetry-sdk/src/trace/provider.rs +++ b/opentelemetry-sdk/src/trace/provider.rs @@ -21,8 +21,6 @@ use std::borrow::Cow; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; -/// Default tracer name if empty string is provided. -const DEFAULT_COMPONENT_NAME: &str = "rust.opentelemetry.io/sdk/tracer"; static PROVIDER_RESOURCE: OnceCell = OnceCell::new(); // a no nop tracer provider used as placeholder when the provider is shutdown @@ -183,37 +181,6 @@ impl opentelemetry::trace::TracerProvider for TracerProvider { /// This implementation of `TracerProvider` produces `Tracer` instances. type Tracer = Tracer; - /// Create a new versioned `Tracer` instance. - fn versioned_tracer( - &self, - name: impl Into>, - version: Option>>, - schema_url: Option>>, - attributes: Option>, - ) -> Self::Tracer { - // Use default value if name is invalid empty string - let name = name.into(); - let component_name = if name.is_empty() { - Cow::Borrowed(DEFAULT_COMPONENT_NAME) - } else { - name - }; - - let mut builder = self.tracer_builder(component_name); - - if let Some(v) = version { - builder = builder.with_version(v); - } - if let Some(s) = schema_url { - builder = builder.with_schema_url(s); - } - if let Some(a) = attributes { - builder = builder.with_attributes(a); - } - - builder.build() - } - fn library_tracer(&self, library: Arc) -> Self::Tracer { if self.is_shutdown.load(Ordering::Relaxed) { return Tracer::new(library, NOOP_TRACER_PROVIDER.clone()); diff --git a/opentelemetry-stdout/examples/basic.rs b/opentelemetry-stdout/examples/basic.rs index 4289c74ec4..4ad6c62ed6 100644 --- a/opentelemetry-stdout/examples/basic.rs +++ b/opentelemetry-stdout/examples/basic.rs @@ -64,16 +64,20 @@ fn init_logs() -> opentelemetry_sdk::logs::LoggerProvider { #[cfg(feature = "trace")] fn emit_span() { - use opentelemetry::trace::{ - SpanContext, SpanId, TraceFlags, TraceId, TraceState, TracerProvider, + use std::sync::Arc; + + use opentelemetry::{ + trace::{SpanContext, SpanId, TraceFlags, TraceId, TraceState, TracerProvider}, + InstrumentationLibrary, }; - let tracer = global::tracer_provider() - .tracer_builder("stdout-example") - .with_version("v1") - .with_schema_url("schema_url") - .with_attributes([KeyValue::new("scope_key", "scope_value")]) - .build(); + let library = Arc::new( + InstrumentationLibrary::builder("stdout-example") + .with_version("v1") + .with_attributes([KeyValue::new("scope_key", "scope_value")]) + .build(), + ); + let tracer = global::tracer_provider().library_tracer(library); let mut span = tracer.start("example-span"); span.set_attribute(KeyValue::new("attribute_key1", "attribute_value1")); span.set_attribute(KeyValue::new("attribute_key2", "attribute_value2")); diff --git a/opentelemetry-zipkin/src/exporter/mod.rs b/opentelemetry-zipkin/src/exporter/mod.rs index 88edcfb025..4c8d1b004f 100644 --- a/opentelemetry-zipkin/src/exporter/mod.rs +++ b/opentelemetry-zipkin/src/exporter/mod.rs @@ -6,7 +6,7 @@ use async_trait::async_trait; use futures_core::future::BoxFuture; use http::Uri; use model::endpoint::Endpoint; -use opentelemetry::{global, trace::TraceError, KeyValue}; +use opentelemetry::{global, trace::TraceError, InstrumentationLibrary, KeyValue}; use opentelemetry_http::HttpClient; use opentelemetry_sdk::{ export::{trace, ExportError}, @@ -144,11 +144,13 @@ impl ZipkinPipelineBuilder { let mut provider_builder = TracerProvider::builder().with_simple_exporter(exporter); provider_builder = provider_builder.with_config(config); let provider = provider_builder.build(); - let tracer = - opentelemetry::trace::TracerProvider::tracer_builder(&provider, "opentelemetry-zipkin") + let library = Arc::new( + InstrumentationLibrary::builder("opentelemetry-zipkin") .with_version(env!("CARGO_PKG_VERSION")) .with_schema_url(semcov::SCHEMA_URL) - .build(); + .build(), + ); + let tracer = opentelemetry::trace::TracerProvider::library_tracer(&provider, library); let _ = global::set_tracer_provider(provider); Ok(tracer) } @@ -161,11 +163,13 @@ impl ZipkinPipelineBuilder { let mut provider_builder = TracerProvider::builder().with_batch_exporter(exporter, runtime); provider_builder = provider_builder.with_config(config); let provider = provider_builder.build(); - let tracer = - opentelemetry::trace::TracerProvider::tracer_builder(&provider, "opentelemetry-zipkin") + let library = Arc::new( + InstrumentationLibrary::builder("opentelemetry-zipkin") .with_version(env!("CARGO_PKG_VERSION")) .with_schema_url(semcov::SCHEMA_URL) - .build(); + .build(), + ); + let tracer = opentelemetry::trace::TracerProvider::library_tracer(&provider, library); let _ = global::set_tracer_provider(provider); Ok(tracer) } diff --git a/opentelemetry/src/global/metrics.rs b/opentelemetry/src/global/metrics.rs index e23a746966..1592329f8b 100644 --- a/opentelemetry/src/global/metrics.rs +++ b/opentelemetry/src/global/metrics.rs @@ -1,5 +1,5 @@ use crate::metrics::{self, Meter, MeterProvider}; -use crate::KeyValue; +use crate::InstrumentationLibrary; use once_cell::sync::Lazy; use std::sync::{Arc, RwLock}; @@ -44,26 +44,26 @@ pub fn meter(name: &'static str) -> Meter { /// - version specifies the version of the instrumentation scope if the scope has a version /// - schema url specifies the Schema URL that should be recorded in the emitted telemetry. /// -/// This is a convenient way of `global::meter_provider().versioned_meter(...)` +/// This is a convenient way of `global::meter_provider().library_meter(...)` /// /// # Example /// /// ``` -/// use opentelemetry::global::meter_with_version; +/// use std::sync::Arc; +/// use opentelemetry::global::library_meter; +/// use opentelemetry::InstrumentationLibrary; /// use opentelemetry::KeyValue; /// -/// let meter = meter_with_version( -/// "io.opentelemetry", -/// Some("0.17"), -/// Some("https://opentelemetry.io/schemas/1.2.0"), -/// Some(vec![KeyValue::new("key", "value")]), +/// let library = Arc::new( +/// InstrumentationLibrary::builder("io.opentelemetry") +/// .with_version("0.17") +/// .with_schema_url("https://opentelemetry.io/schema/1.2.0") +/// .with_attributes(vec![(KeyValue::new("key", "value"))]) +/// .build(), /// ); +/// +/// let meter = library_meter(library); /// ``` -pub fn meter_with_version( - name: &'static str, - version: Option<&'static str>, - schema_url: Option<&'static str>, - attributes: Option>, -) -> Meter { - meter_provider().versioned_meter(name, version, schema_url, attributes) +pub fn library_meter(library: Arc) -> Meter { + meter_provider().library_meter(library) } diff --git a/opentelemetry/src/global/mod.rs b/opentelemetry/src/global/mod.rs index 5a6d335431..b73e40a9cc 100644 --- a/opentelemetry/src/global/mod.rs +++ b/opentelemetry/src/global/mod.rs @@ -50,16 +50,22 @@ //! ``` //! # #[cfg(feature="trace")] //! # { +//! use std::sync::Arc; //! use opentelemetry::trace::{Tracer, TracerProvider}; //! use opentelemetry::global; +//! use opentelemetry::InstrumentationLibrary; //! //! pub fn my_traced_library_function() { //! // End users of your library will configure their global tracer provider //! // so you can use the global tracer without any setup -//! let tracer = global::tracer_provider().tracer_builder("my-library-name"). -//! with_version(env!("CARGO_PKG_VERSION")). -//! with_schema_url("https://opentelemetry.io/schemas/1.17.0"). -//! build(); +//! +//! let library = Arc::new(InstrumentationLibrary::builder("my_library-name") +//! .with_version(env!("CARGO_PKG_VERSION")) +//! .with_schema_url("https://opentelemetry.io/schemas/1.17.0") +//! .build() +//! ); +//! +//! let tracer = global::tracer_provider().library_tracer(library); //! //! tracer.in_span("doing_library_work", |cx| { //! // Traced library logic here... diff --git a/opentelemetry/src/logs/logger.rs b/opentelemetry/src/logs/logger.rs index fd4e18e043..d71415adff 100644 --- a/opentelemetry/src/logs/logger.rs +++ b/opentelemetry/src/logs/logger.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, sync::Arc}; -use crate::{logs::LogRecord, InstrumentationLibrary, InstrumentationLibraryBuilder, KeyValue}; +use crate::{logs::LogRecord, InstrumentationLibrary}; #[cfg(feature = "logs_level_enabled")] use super::Severity; @@ -30,73 +30,14 @@ pub trait LoggerProvider { /// The [`Logger`] type that this provider will return. type Logger: Logger; - /// Deprecated, use [`LoggerProvider::logger_builder()`] - /// - /// Returns a new versioned logger with a given name. - /// - /// The `name` should be the application name or the name of the library - /// providing instrumentation. If the name is empty, then an - /// implementation-defined default name may be used instead. - /// Create a new versioned `Logger` instance. - #[deprecated(since = "0.23.0", note = "Please use logger_builder() instead")] - fn versioned_logger( - &self, - name: impl Into>, - version: Option>, - schema_url: Option>, - attributes: Option>, - ) -> Self::Logger { - let mut builder = self.logger_builder(name); - if let Some(v) = version { - builder = builder.with_version(v); - } - if let Some(s) = schema_url { - builder = builder.with_schema_url(s); - } - if let Some(a) = attributes { - builder = builder.with_attributes(a); - } - builder.build() - } - - /// Returns a new builder for creating a [`Logger`] instance - /// - /// The `name` should be the application name or the name of the library - /// providing instrumentation. If the name is empty, then an - /// implementation-defined default name may be used instead. + /// Returns a new logger with the given instrumentation library. /// /// # Examples /// /// ``` + /// use std::sync::Arc; /// use opentelemetry::InstrumentationLibrary; - /// use crate::opentelemetry::logs::LoggerProvider; - /// use opentelemetry_sdk::logs::LoggerProvider as SdkLoggerProvider; - /// - /// let provider = SdkLoggerProvider::builder().build(); - /// - /// // logger used in applications/binaries - /// let logger = provider.logger_builder("my_app").build(); - /// - /// // logger used in libraries/crates that optionally includes version and schema url - /// let logger = provider.logger_builder("my_library") - /// .with_version(env!("CARGO_PKG_VERSION")) - /// .with_schema_url("https://opentelemetry.io/schema/1.0.0") - /// .build(); - /// ``` - fn logger_builder(&self, name: impl Into>) -> LoggerBuilder<'_, Self> { - LoggerBuilder { - provider: self, - library_builder: InstrumentationLibrary::builder(name), - } - } - - /// Returns a new versioned logger with the given instrumentation library. - /// - /// # Examples - /// - /// ``` - /// use opentelemetry::InstrumentationLibrary; - /// use crate::opentelemetry::logs::LoggerProvider; + /// use opentelemetry::logs::LoggerProvider; /// use opentelemetry_sdk::logs::LoggerProvider as SdkLoggerProvider; /// /// let provider = SdkLoggerProvider::builder().build(); @@ -105,7 +46,7 @@ pub trait LoggerProvider { /// let logger = provider.logger("my_app"); /// /// // logger used in libraries/crates that optionally includes version and schema url - /// let library = std::sync::Arc::new( + /// let library = Arc::new( /// InstrumentationLibrary::builder(env!("CARGO_PKG_NAME")) /// .with_version(env!("CARGO_PKG_VERSION")) /// .with_schema_url("https://opentelemetry.io/schema/1.0.0") @@ -121,37 +62,7 @@ pub trait LoggerProvider { /// providing instrumentation. If the name is empty, then an /// implementation-defined default name may be used instead. fn logger(&self, name: impl Into>) -> Self::Logger { - self.logger_builder(name).build() - } -} - -#[derive(Debug)] -pub struct LoggerBuilder<'a, T: LoggerProvider + ?Sized> { - provider: &'a T, - library_builder: InstrumentationLibraryBuilder, -} - -impl<'a, T: LoggerProvider + ?Sized> LoggerBuilder<'a, T> { - pub fn with_version(mut self, version: impl Into>) -> Self { - self.library_builder = self.library_builder.with_version(version); - self - } - - pub fn with_schema_url(mut self, schema_url: impl Into>) -> Self { - self.library_builder = self.library_builder.with_schema_url(schema_url); - self - } - - pub fn with_attributes(mut self, attributes: I) -> Self - where - I: IntoIterator, - { - self.library_builder = self.library_builder.with_attributes(attributes); - self - } - - pub fn build(self) -> T::Logger { - self.provider - .library_logger(Arc::new(self.library_builder.build())) + let library = Arc::new(InstrumentationLibrary::builder(name).build()); + self.library_logger(library) } } diff --git a/opentelemetry/src/logs/noop.rs b/opentelemetry/src/logs/noop.rs index 8c31328e5d..91c2e7bf97 100644 --- a/opentelemetry/src/logs/noop.rs +++ b/opentelemetry/src/logs/noop.rs @@ -2,7 +2,7 @@ use std::{borrow::Cow, sync::Arc, time::SystemTime}; use crate::{ logs::{AnyValue, LogRecord, Logger, LoggerProvider, Severity}, - InstrumentationLibrary, Key, KeyValue, + InstrumentationLibrary, Key, }; /// A no-op implementation of a [`LoggerProvider`]. @@ -22,16 +22,6 @@ impl LoggerProvider for NoopLoggerProvider { fn library_logger(&self, _library: Arc) -> Self::Logger { NoopLogger(()) } - - fn versioned_logger( - &self, - _name: impl Into>, - _version: Option>, - _schema_url: Option>, - _attributes: Option>, - ) -> Self::Logger { - NoopLogger(()) - } } #[derive(Debug, Clone, Default)] diff --git a/opentelemetry/src/metrics/meter.rs b/opentelemetry/src/metrics/meter.rs index 7b3f851dd5..c3a2976fb6 100644 --- a/opentelemetry/src/metrics/meter.rs +++ b/opentelemetry/src/metrics/meter.rs @@ -6,13 +6,39 @@ use crate::metrics::{ AsyncInstrumentBuilder, Gauge, InstrumentBuilder, InstrumentProvider, ObservableCounter, ObservableGauge, ObservableUpDownCounter, UpDownCounter, }; -use crate::KeyValue; +use crate::InstrumentationLibrary; use super::{Counter, Histogram, HistogramBuilder}; /// Provides access to named [Meter] instances, for instrumenting an application /// or crate. pub trait MeterProvider { + /// Returns a new [Meter] with the given instrumentation library. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// use opentelemetry::InstrumentationLibrary; + /// use opentelemetry::metrics::MeterProvider; + /// use opentelemetry_sdk::metrics::SdkMeterProvider; + /// + /// let provider = SdkMeterProvider::default(); + /// + /// // logger used in applications/binaries + /// let meter = provider.meter("my_app"); + /// + /// // logger used in libraries/crates that optionally includes version and schema url + /// let library = std::sync::Arc::new( + /// InstrumentationLibrary::builder(env!("CARGO_PKG_NAME")) + /// .with_version(env!("CARGO_PKG_VERSION")) + /// .with_schema_url("https://opentelemetry.io/schema/1.0.0") + /// .build(), + /// ); + /// let logger = provider.library_meter(library); + /// ``` + fn library_meter(&self, library: Arc) -> Meter; + /// Returns a new [Meter] with the provided name and default configuration. /// /// A [Meter] should be scoped at most to a single application or crate. The @@ -32,32 +58,11 @@ pub trait MeterProvider { /// /// // meter used in applications /// let meter = provider.meter("my_app"); - /// - /// // meter used in libraries/crates that optionally includes version and schema url - /// let meter = provider.versioned_meter( - /// "my_library", - /// Some(env!("CARGO_PKG_VERSION")), - /// Some("https://opentelemetry.io/schema/1.0.0"), - /// Some(vec![KeyValue::new("key", "value")]), - /// ); /// ``` fn meter(&self, name: &'static str) -> Meter { - self.versioned_meter(name, None, None, None) + let library = Arc::new(InstrumentationLibrary::builder(name).build()); + self.library_meter(library) } - - /// Returns a new versioned meter with a given name. - /// - /// The instrumentation name must be the name of the library providing instrumentation. This - /// name may be the same as the instrumented code only if that code provides built-in - /// instrumentation. If the instrumentation name is empty, then a implementation defined - /// default name will be used instead. - fn versioned_meter( - &self, - name: &'static str, - version: Option<&'static str>, - schema_url: Option<&'static str>, - attributes: Option>, - ) -> Meter; } /// Provides the ability to create instruments for recording measurements or diff --git a/opentelemetry/src/metrics/noop.rs b/opentelemetry/src/metrics/noop.rs index dbe0726515..c293d0f08c 100644 --- a/opentelemetry/src/metrics/noop.rs +++ b/opentelemetry/src/metrics/noop.rs @@ -25,13 +25,7 @@ impl NoopMeterProvider { } impl MeterProvider for NoopMeterProvider { - fn versioned_meter( - &self, - _name: &'static str, - _version: Option<&'static str>, - _schema_url: Option<&'static str>, - _attributes: Option>, - ) -> Meter { + fn library_meter(&self, _library: Arc) -> Meter { Meter::new(Arc::new(NoopMeter::new())) } } diff --git a/opentelemetry/src/trace/mod.rs b/opentelemetry/src/trace/mod.rs index 5e3edc3519..f43ef713b5 100644 --- a/opentelemetry/src/trace/mod.rs +++ b/opentelemetry/src/trace/mod.rs @@ -44,6 +44,8 @@ //! //! ``` //! use opentelemetry::{global, trace::{Span, Tracer, TracerProvider}}; +//! use opentelemetry::InstrumentationLibrary; +//! use std::sync::Arc; //! //! fn my_library_function() { //! // Use the global tracer provider to get access to the user-specified @@ -51,10 +53,12 @@ //! let tracer_provider = global::tracer_provider(); //! //! // Get a tracer for this library -//! let tracer = tracer_provider.tracer_builder("my_name"). -//! with_version(env!("CARGO_PKG_VERSION")). -//! with_schema_url("https://opentelemetry.io/schemas/1.17.0"). -//! build(); +//! let library = Arc::new(InstrumentationLibrary::builder("my_name") +//! .with_version(env!("CARGO_PKG_VERSION")) +//! .with_schema_url("https://opentelemetry.io/schemas/1.17.0") +//! .build() +//! ); +//! let tracer = tracer_provider.library_tracer(library); //! //! // Create spans //! let mut span = tracer.start("doing_work"); diff --git a/opentelemetry/src/trace/tracer_provider.rs b/opentelemetry/src/trace/tracer_provider.rs index c17c3fdd58..e01616827a 100644 --- a/opentelemetry/src/trace/tracer_provider.rs +++ b/opentelemetry/src/trace/tracer_provider.rs @@ -1,4 +1,4 @@ -use crate::{trace::Tracer, InstrumentationLibrary, InstrumentationLibraryBuilder, KeyValue}; +use crate::{trace::Tracer, InstrumentationLibrary}; use std::{borrow::Cow, sync::Arc}; /// Types that can create instances of [`Tracer`]. @@ -27,93 +27,10 @@ pub trait TracerProvider { /// /// // tracer used in applications/binaries /// let tracer = provider.tracer("my_app"); - /// - /// // tracer used in libraries/crates that optionally includes version and schema url - /// let tracer = provider.tracer_builder("my_library"). - /// with_version(env!("CARGO_PKG_VERSION")). - /// with_schema_url("https://opentelemetry.io/schema/1.0.0"). - /// with_attributes([KeyValue::new("key", "value")]). - /// build(); /// ``` fn tracer(&self, name: impl Into>) -> Self::Tracer { - self.tracer_builder(name).build() - } - - /// Deprecated, use [`TracerProvider::tracer_builder()`] - /// - /// Returns a new versioned tracer with a given name. - /// - /// The `name` should be the application name or the name of the library - /// providing instrumentation. If the name is empty, then an - /// implementation-defined default name may be used instead. - /// - /// # Examples - /// - /// ``` - /// use opentelemetry::{global, trace::TracerProvider}; - /// - /// let provider = global::tracer_provider(); - /// - /// // tracer used in applications/binaries - /// let tracer = provider.tracer("my_app"); - /// - /// // tracer used in libraries/crates that optionally includes version and schema url - /// let tracer = provider.versioned_tracer( - /// "my_library", - /// Some(env!("CARGO_PKG_VERSION")), - /// Some("https://opentelemetry.io/schema/1.0.0"), - /// None, - /// ); - /// ``` - #[deprecated(since = "0.23.0", note = "Please use tracer_builder() instead")] - fn versioned_tracer( - &self, - name: impl Into>, - version: Option>>, - schema_url: Option>>, - attributes: Option>, - ) -> Self::Tracer { - let mut builder = self.tracer_builder(name); - if let Some(v) = version { - builder = builder.with_version(v); - } - if let Some(s) = schema_url { - builder = builder.with_version(s); - } - if let Some(a) = attributes { - builder = builder.with_attributes(a); - } - - builder.build() - } - - /// Returns a new builder for creating a [`Tracer`] instance - /// - /// The `name` should be the application name or the name of the library - /// providing instrumentation. If the name is empty, then an - /// implementation-defined default name may be used instead. - /// - /// # Examples - /// - /// ``` - /// use opentelemetry::{global, trace::TracerProvider}; - /// - /// let provider = global::tracer_provider(); - /// - /// // tracer used in applications/binaries - /// let tracer = provider.tracer_builder("my_app").build(); - /// - /// // tracer used in libraries/crates that optionally includes version and schema url - /// let tracer = provider.tracer_builder("my_library") - /// .with_version(env!("CARGO_PKG_VERSION")) - /// .with_schema_url("https://opentelemetry.io/schema/1.0.0") - /// .build(); - /// ``` - fn tracer_builder(&self, name: impl Into>) -> TracerBuilder<'_, Self> { - TracerBuilder { - provider: self, - library_builder: InstrumentationLibrary::builder(name), - } + let library = InstrumentationLibrary::builder(name).build(); + self.library_tracer(Arc::new(library)) } /// Returns a new versioned tracer with the given instrumentation library. @@ -140,34 +57,3 @@ pub trait TracerProvider { /// ``` fn library_tracer(&self, library: Arc) -> Self::Tracer; } - -#[derive(Debug)] -pub struct TracerBuilder<'a, T: TracerProvider + ?Sized> { - provider: &'a T, - library_builder: InstrumentationLibraryBuilder, -} - -impl<'a, T: TracerProvider + ?Sized> TracerBuilder<'a, T> { - pub fn with_version(mut self, version: impl Into>) -> Self { - self.library_builder = self.library_builder.with_version(version); - self - } - - pub fn with_schema_url(mut self, schema_url: impl Into>) -> Self { - self.library_builder = self.library_builder.with_schema_url(schema_url); - self - } - - pub fn with_attributes(mut self, attributes: I) -> Self - where - I: IntoIterator, - { - self.library_builder = self.library_builder.with_attributes(attributes); - self - } - - pub fn build(self) -> T::Tracer { - self.provider - .library_tracer(Arc::new(self.library_builder.build())) - } -}