Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a builder pattern for loggers and tracers #1567

Merged
merged 18 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions opentelemetry-appender-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ where
{
pub fn new(provider: &P) -> Self {
OpenTelemetryLogBridge {
logger: provider.versioned_logger(
"opentelemetry-log-appender",
Some(Cow::Borrowed(env!("CARGO_PKG_VERSION"))),
None,
None,
),
logger: provider
.logger_builder("opentelemetry-log-appender")
.with_version(Cow::Borrowed(env!("CARGO_PKG_VERSION")))
.build(),
_phantom: Default::default(),
}
}
Expand Down
10 changes: 4 additions & 6 deletions opentelemetry-appender-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,10 @@ where
{
pub fn new(provider: &P) -> Self {
OpenTelemetryTracingBridge {
logger: provider.versioned_logger(
INSTRUMENTATION_LIBRARY_NAME,
Some(Cow::Borrowed(env!("CARGO_PKG_VERSION"))),
None,
None,
),
logger: provider
.logger_builder(INSTRUMENTATION_LIBRARY_NAME)
.with_version(Cow::Borrowed(env!("CARGO_PKG_VERSION")))
.build(),
_phantom: Default::default(),
}
}
Expand Down
10 changes: 5 additions & 5 deletions opentelemetry-jaeger/src/exporter/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ fn build_config_and_process(
pub(crate) fn install_tracer_provider_and_get_tracer(
tracer_provider: TracerProvider,
) -> Result<Tracer, TraceError> {
let tracer = opentelemetry::trace::TracerProvider::versioned_tracer(
let tracer = opentelemetry::trace::TracerProvider::tracer_builder(
&tracer_provider,
"opentelemetry-jaeger",
Some(env!("CARGO_PKG_VERSION")),
Some(semcov::SCHEMA_URL),
None,
);
)
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url(semcov::SCHEMA_URL)
.build();
let _ = global::set_tracer_provider(tracer_provider);
Ok(tracer)
}
Expand Down
20 changes: 8 additions & 12 deletions opentelemetry-otlp/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,10 @@ fn build_simple_with_exporter(
provider_builder = provider_builder.with_config(config);
}
let provider = provider_builder.build();
let logger = provider.versioned_logger(
Cow::Borrowed("opentelemetry-otlp"),
Some(Cow::Borrowed(env!("CARGO_PKG_VERSION"))),
None,
None,
);
let logger = provider
.logger_builder(Cow::Borrowed("opentelemetry-otlp"))
.with_version(Cow::Borrowed(env!("CARGO_PKG_VERSION")))
.build();
let _ = global::set_logger_provider(provider);
logger
}
Expand All @@ -209,12 +207,10 @@ fn build_batch_with_exporter<R: RuntimeChannel>(
provider_builder = provider_builder.with_config(config);
}
let provider = provider_builder.build();
let logger = provider.versioned_logger(
Cow::Borrowed("opentelemetry-otlp"),
Some(Cow::Borrowed(env!("CARGO_PKG_VERSION"))),
None,
None,
);
let logger = provider
.logger_builder(Cow::Borrowed("opentelemetry-otlp"))
.with_version(Cow::Borrowed(env!("CARGO_PKG_VERSION")))
.build();
let _ = global::set_logger_provider(provider);
logger
}
22 changes: 10 additions & 12 deletions opentelemetry-otlp/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,11 @@ fn build_simple_with_exporter(
provider_builder = provider_builder.with_config(config);
}
let provider = provider_builder.build();
let tracer = provider.versioned_tracer(
"opentelemetry-otlp",
Some(env!("CARGO_PKG_VERSION")),
Some(SCHEMA_URL),
None,
);
let tracer = provider
.tracer_builder("opentelemetry-otlp")
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url(SCHEMA_URL)
.build();
let _ = global::set_tracer_provider(provider);
tracer
}
Expand All @@ -166,12 +165,11 @@ fn build_batch_with_exporter<R: RuntimeChannel>(
provider_builder = provider_builder.with_config(config);
}
let provider = provider_builder.build();
let tracer = provider.versioned_tracer(
"opentelemetry-otlp",
Some(env!("CARGO_PKG_VERSION")),
Some(SCHEMA_URL),
None,
);
let tracer = provider
.tracer_builder("opentelemetry-otlp")
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url(SCHEMA_URL)
.build();
let _ = global::set_tracer_provider(provider);
tracer
}
Expand Down
19 changes: 13 additions & 6 deletions opentelemetry-sdk/src/logs/log_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,19 @@ impl opentelemetry::logs::LoggerProvider for LoggerProvider {
name
};

self.library_logger(Arc::new(InstrumentationLibrary::new(
component_name,
version,
schema_url,
attributes,
)))
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<InstrumentationLibrary>) -> Self::Logger {
Expand Down
44 changes: 43 additions & 1 deletion opentelemetry-sdk/src/logs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod tests {
use super::*;
use crate::testing::logs::InMemoryLogsExporter;
use opentelemetry::logs::{LogRecord, Logger, LoggerProvider as _, Severity};
use opentelemetry::{logs::AnyValue, Key};
use opentelemetry::{logs::AnyValue, Key, KeyValue};

#[test]
fn logging_sdk_test() {
Expand Down Expand Up @@ -55,4 +55,46 @@ mod tests {
.expect("Attributes are expected");
assert_eq!(attributes.len(), 2);
}

#[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!(
izquierdo marked this conversation as resolved.
Show resolved Hide resolved
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 instrumentation_library = logger.instrumentation_library();
let attributes = &instrumentation_library.attributes;
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());
}
}
14 changes: 13 additions & 1 deletion opentelemetry-sdk/src/metrics/meter_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,19 @@ impl MeterProvider for SdkMeterProvider {
return Meter::new(Arc::new(NoopMeterCore::new()));
}

let scope = Scope::new(name, version, schema_url, attributes);
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.meters.lock() {
let meter = meters
Expand Down
37 changes: 37 additions & 0 deletions opentelemetry-sdk/src/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,41 @@ mod tests {
);
assert_eq!(span.span_context().trace_state().get("foo"), Some("bar"));
}

#[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 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());
}
}
19 changes: 13 additions & 6 deletions opentelemetry-sdk/src/trace/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,19 @@ impl opentelemetry::trace::TracerProvider for TracerProvider {
name
};

self.library_tracer(Arc::new(InstrumentationLibrary::new(
component_name,
version,
schema_url,
attributes,
)))
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<InstrumentationLibrary>) -> Self::Tracer {
Expand Down
24 changes: 10 additions & 14 deletions opentelemetry-zipkin/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,11 @@ 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::versioned_tracer(
&provider,
"opentelemetry-zipkin",
Some(env!("CARGO_PKG_VERSION")),
Some(semcov::SCHEMA_URL),
None,
);
let tracer =
opentelemetry::trace::TracerProvider::tracer_builder(&provider, "opentelemetry-zipkin")
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url(semcov::SCHEMA_URL)
.build();
let _ = global::set_tracer_provider(provider);
Ok(tracer)
}
Expand All @@ -170,13 +168,11 @@ 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::versioned_tracer(
&provider,
"opentelemetry-zipkin",
Some(env!("CARGO_PKG_VERSION")),
Some(semcov::SCHEMA_URL),
None,
);
let tracer =
opentelemetry::trace::TracerProvider::tracer_builder(&provider, "opentelemetry-zipkin")
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url(semcov::SCHEMA_URL)
.build();
let _ = global::set_tracer_provider(provider);
Ok(tracer)
}
Expand Down
50 changes: 50 additions & 0 deletions opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,56 @@

- Remove `urlencoding` crate dependency. [#1613](https://github.com/open-telemetry/opentelemetry-rust/pull/1613)

### Changed

- Deprecate `versioned_logger()` in favor of `logger_builder()` [1567](https://github.com/open-telemetry/opentelemetry-rust/pull/1567).

Before:

```
let logger = provider.versioned_logger(
"my-logger-name",
Some("X.Y.Z"),
izquierdo marked this conversation as resolved.
Show resolved Hide resolved
Some("https://opentelemetry.io/schema/1.0.0"),
Some(vec![KeyValue::new("key", "value")]),
);
```

After:

```
let logger = provider
.logger_builder("my-tracername")
izquierdo marked this conversation as resolved.
Show resolved Hide resolved
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url(semcov::SCHEMA_URL)
izquierdo marked this conversation as resolved.
Show resolved Hide resolved
.with_attributes(vec![KeyValue::new("key", "value")])
.build();
```

- Deprecate `versioned_tracer()` in favor of `tracer_builder()` [1567](https://github.com/open-telemetry/opentelemetry-rust/pull/1567).

Before:

```
let tracer = provider.versioned_tracer(
"my-tracer-name",
Some("X.Y.Z"),
Some("https://opentelemetry.io/schema/1.0.0"),
Some(vec![KeyValue::new("key", "value")]),
);
```

After:

```
let tracer = provider
.tracer_builder("my-tracername")
izquierdo marked this conversation as resolved.
Show resolved Hide resolved
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url(semcov::SCHEMA_URL)
.with_attributes(vec![KeyValue::new("key", "value")])
.build();
```

## v0.22.0

### Added
Expand Down
Loading
Loading