Skip to content

[Bug]: TracerProvider can be dropped unknowingly #1625

Open
@SZenglein

Description

@SZenglein

What happened?

Consider this example code:
(I am using tracing_opentelemetry, but it should be reproducible without it)

let telemetry =  {
    // Create a new OpenTelemetry trace pipeline that prints to stdout
    let provider = TracerProvider::builder()
        .with_simple_exporter(
            opentelemetry_otlp::new_exporter()
                .tonic()
                .with_endpoint("http:localhost:4317")
                .with_timeout(Duration::from_secs(3))
                .build_span_exporter()?,
        )
        .with_config(opentelemetry_sdk::trace::Config::default().with_resource(
            Resource::new(vec![KeyValue::new(
                opentelemetry_semantic_conventions::resource::SERVICE_NAME,
                "service-api",
            )]),
        ))
        .build();
    
    let tracer = provider.tracer("service-api");
    
    // Create and return a tracing layer with the configured tracer
    tracing_opentelemetry::layer().with_tracer(tracer)
};

Since the provider is not used afterwards, it is very easy to accidentally drop it when executing inside a function or conditional block.
There is no compiler warning or any documentation note regarding this behaviour or that it must be kept alive manually. This results in very hard to debug problem, because code that compiles fine without a warning can stop working just by moving a value into a scope.

I suspect this is because TracerProvider creates the Tracer by downgrading the Arc to a Weak.

IMHO this should at the very least be documented on the TracerProvider struct, maybe a warning can be added to the Drop Implementation, too.

API Version

0.15

SDK Version

0.22.1

What Exporters are you seeing the problem on?

No response

Relevant log output

No response

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requesttriage:acceptedHas been triaged and accepted.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions