forked from open-telemetry/opentelemetry-rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} |