Skip to content

#[instrument(err)] causes broken traces in Datadog #242

@software-artificer

Description

@software-artificer

I've been sent here after having a conversation in CNCF Slack in OTel Rust group. The issue tokio-rs/tracing#2648 was reported before, but it doesn't seem like that's a correct place for it.

Bug Report

We are using the tracing framework for our Rust applications for distributed tracing and sending traces and spans to Datadog using OTLP+gRPC.

When using #[tracing::instrument(err)] macro to capture and record Result::Err() returned by the annotated function, traces end up being broken in Datadog, reporting: Missing error message and stack trace.

I am willing to work through this issue and contribute a fix, provided some guidance as to where do I start from.

Please let me know if you need any additional information here.

Version

tracing v0.1.43
tracing-attributes v0.1.31 (proc-macro)
tracing-core v0.1.35
tracing-opentelemetry v0.32.0
tracing-subscriber v0.3.22

Platform

Google's Distroless container images based on Debian 12, x86_64.

Description

Here's the minimal reproducible example:

use opentelemetry::trace::TracerProvider as _;
use opentelemetry_otlp::WithExportConfig as _;
use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _};

#[tracing::instrument("Failing span", err)]
fn failing_function() -> Result<(), std::io::Error> {
    Err(std::io::Error::other("Big error, much problem"))
}

#[tokio::main]
async fn main() {
    let span_exporter = opentelemetry_otlp::SpanExporter::builder()
        .with_tonic()
        .with_protocol(opentelemetry_otlp::Protocol::Grpc)
        // Update to point to your collector
        .with_endpoint("grpc://127.0.0.1:4317")
        .with_timeout(std::time::Duration::from_secs(5))
        .build()
        .expect("Failed to build a span exporter");

    let tracer_provider = opentelemetry_sdk::trace::SdkTracerProvider::builder()
        .with_batch_exporter(span_exporter)
        .build();

    let traces_layer =
        tracing_opentelemetry::OpenTelemetryLayer::new(tracer_provider.tracer("tracing-repro"));

    tracing_subscriber::registry()
        .with(traces_layer)
        .try_init()
        .expect("Failed to initialize tracing");

    failing_function().expect_err("Expected failing function to fail");

    tracer_provider
        .shutdown_with_timeout(std::time::Duration::from_secs(5))
        .expect("Failed to shutdown the tracer provider");
}

and the Cargo.toml to go with it:

[package]
name = "tracing-instrument-otel-err"
version = "0.1.0"
edition = "2024"

[dependencies]
opentelemetry = { version = "0.31.0", default-features = false, features = ["trace"] }
opentelemetry-otlp = { version = "0.31.0", default-features = false, features = ["grpc-tonic", "trace"] }
opentelemetry_sdk = { version = "0.31.0", default-features = false, features = ["trace"] }
tokio = { version = "1.48.0", default-features = false, features = [
	"macros",
	"rt-multi-thread",
] }
tracing = { version = "0.1.43", default-features = false, features = ["attributes", "std"] }
tracing-opentelemetry = { version = "0.32.0", default-features = false }
tracing-subscriber = { version = "0.3.22", default-features = false, features = ["std", "registry"] }

When using this code and sending traces to Datadog, the following problem can be observed:

Image

and the error tab, respectively:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions