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

Retain empty tracer name as is instead of using default name #2486

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ metadata, a feature introduced in version 0.1.40. [#2418](https://github.com/ope
- Continue enabling one of the async runtime feature flags: `rt-tokio`,
`rt-tokio-current-thread`, or `rt-async-std`.

- Bug fix: Empty Tracer names are retained as-is instead of replacing with
"rust.opentelemetry.io/sdk/tracer"
[#2486](https://github.com/open-telemetry/opentelemetry-rust/pull/2486)

## 0.27.1

Released 2024-Nov-27
Expand Down
44 changes: 43 additions & 1 deletion opentelemetry-sdk/src/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod tests {

use super::*;
use crate::{
testing::trace::InMemorySpanExporterBuilder,
testing::trace::{InMemorySpanExporter, InMemorySpanExporterBuilder},
trace::span_limit::{DEFAULT_MAX_EVENT_PER_SPAN, DEFAULT_MAX_LINKS_PER_SPAN},
};
use opentelemetry::trace::{
Expand Down Expand Up @@ -342,4 +342,46 @@ mod tests {
.attributes()
.eq(&[KeyValue::new("test_k", "test_v")]));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn empty_tracer_name_retained() {
async fn tracer_name_retained_helper(
tracer: super::Tracer,
provider: TracerProvider,
exporter: InMemorySpanExporter,
) {
// Act
tracer.start("my_span").end();

// Force flush to ensure spans are exported
provider.force_flush().into_iter().for_each(|result| {
result.expect("failed to flush spans");
});

// Assert
let finished_spans = exporter
.get_finished_spans()
.expect("spans are expected to be exported.");
assert_eq!(finished_spans.len(), 1, "There should be a single span");

let tracer_name = finished_spans[0].instrumentation_scope.name();
assert_eq!(tracer_name, "", "The tracer name should be an empty string");

exporter.reset();
}

let exporter = InMemorySpanExporter::default();
let span_processor = SimpleSpanProcessor::new(Box::new(exporter.clone()));
let tracer_provider = TracerProvider::builder()
.with_span_processor(span_processor)
.build();

// Test Tracer creation in 2 ways, both with empty string as tracer name
let tracer1 = tracer_provider.tracer("");
tracer_name_retained_helper(tracer1, tracer_provider.clone(), exporter.clone()).await;

let tracer_scope = InstrumentationScope::builder("").build();
let tracer2 = tracer_provider.tracer_with_scope(tracer_scope);
tracer_name_retained_helper(tracer2, tracer_provider, exporter).await;
}
}
14 changes: 4 additions & 10 deletions opentelemetry-sdk/src/trace/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ use crate::trace::{
use crate::Resource;
use crate::{export::trace::SpanExporter, trace::SpanProcessor};
use opentelemetry::trace::TraceError;
use opentelemetry::InstrumentationScope;
use opentelemetry::{otel_debug, trace::TraceResult};
use opentelemetry::{otel_info, InstrumentationScope};
use std::borrow::Cow;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};
Expand Down Expand Up @@ -252,20 +252,11 @@ impl TracerProvider {
}
}

/// Default tracer name if empty string is provided.
const DEFAULT_COMPONENT_NAME: &str = "rust.opentelemetry.io/sdk/tracer";

impl opentelemetry::trace::TracerProvider for TracerProvider {
/// This implementation of `TracerProvider` produces `Tracer` instances.
type Tracer = Tracer;

fn tracer(&self, name: impl Into<Cow<'static, str>>) -> Self::Tracer {
let mut name = name.into();

if name.is_empty() {
name = Cow::Borrowed(DEFAULT_COMPONENT_NAME)
};

let scope = InstrumentationScope::builder(name).build();
self.tracer_with_scope(scope)
}
Expand All @@ -274,6 +265,9 @@ impl opentelemetry::trace::TracerProvider for TracerProvider {
if self.inner.is_shutdown.load(Ordering::Relaxed) {
return Tracer::new(scope, noop_tracer_provider().clone());
}
if scope.name().is_empty() {
otel_info!(name: "TracerNameEmpty", message = "Tracer name is empty; consider providing a meaningful name. Tracer will function normally and the provided name will be used as-is.");
};
Tracer::new(scope, self.clone())
}
}
Expand Down
Loading