Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyCpp committed May 27, 2024
1 parent 91397fc commit ed3d5ec
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/broken_traces/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "broken_traces"
version = "0.1.0"
edition = "2021"

[dependencies]
opentelemetry = "0.22.0"
opentelemetry-stdout = { version = "0.3.0", features = ["trace"] }
opentelemetry_sdk = "0.22.1"
serde_json = "1.0.116"
tracing = "0.1.40"
tracing-opentelemetry = "0.23.0"
tracing-subscriber = "0.3.18"
53 changes: 53 additions & 0 deletions examples/broken_traces/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use opentelemetry::{
trace::{Span, Tracer, TracerProvider as _},
KeyValue,
};

use opentelemetry_sdk::{
resource,
trace::{config, Config, TracerProvider},
Resource,
};
use opentelemetry_stdout::SpanExporterBuilder;
use tracing::span;
use tracing_opentelemetry::OpenTelemetrySpanExt;
use tracing_subscriber::{layer::SubscriberExt, Registry};

fn main() {
println!("Hello, world!");
let stdout_exporter = SpanExporterBuilder::default()
.with_encoder(|writer, data| {
serde_json::to_writer_pretty(writer, &data).unwrap();
Ok(())
})
.build();
let provider = TracerProvider::builder()
.with_config(Config::default().with_resource(Resource::new(vec![])))
.with_simple_exporter(stdout_exporter)
.build();
let tracer = provider.tracer("tracing-opentelemetry");
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
let subscriber = Registry::default().with(telemetry);

tracing::subscriber::with_default(subscriber, || {
let tracer = provider.tracer("otel-tracing");

// this is the root span created using tokio tracing api
let root = span!(tracing::Level::TRACE, "tokio-tracing-span-parent");
let _enter = root.enter();

// this is a child span created using tokio tracing api
// this correctly parented to the root span
let child = span!(tracing::Level::TRACE, "tokio-tracing-span-child");
let _enter_child = child.enter();

// this is another child, created using otel tracing api
// but this is *NOT* parented to the above spans
// demonstrating broken traces when mixing and matching tracing and opentelemetry apis
// let mut span = tracer
// .start_with_context("otel-tracing-span", &child.context());
let mut span = tracer
.start("otel-tracing-span");
span.end();
});
}

0 comments on commit ed3d5ec

Please sign in to comment.