Comments (2)
The problem you're facing is related to inter-process trace propagation (see also OTel Context Propagation): when the second instance of the program starts, it has no idea the caller process was instrumented so it cannot set the parent ID as you'd expect.
You need to use a TraceContextPropagator
that will let you extract information about the current span in the first process (trace ID, span ID), to forward it to the second process and properly set the parent span ID.
Here's a modified version of your repro (please note I've added serde
and serde_json
). I've also stripped the CondVar-related code, because it's not relevant to the problem and appears to not be working (the second instance seems to block on wait
and never exits, so it also holds back the related span).
main.rs
use std::{collections::HashMap, env, process::Command};
use opentelemetry::{
global::{self, shutdown_tracer_provider},
trace::TraceError,
KeyValue,
};
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::{propagation::TraceContextPropagator, trace as sdktrace, Resource};
use tracing::{instrument, Span};
use tracing_opentelemetry::OpenTelemetrySpanExt;
use tracing_subscriber::{layer::SubscriberExt, Registry};
#[tokio::main]
async fn main() {
let tracer = init_tracer().expect("Failed to initialize tracer.");
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
global::set_text_map_propagator(TraceContextPropagator::new());
let subscriber = Registry::default().with(telemetry);
tracing::subscriber::with_default(subscriber, || {
shim_main();
});
shutdown_tracer_provider();
println!("Shutdown");
}
#[instrument]
fn shim_main() {
let args: Vec<_> = env::args().collect();
match args.get(1) {
Some(arg) if arg == "1" => {
a1();
spawn();
}
Some(arg) if arg == "2" => {
if let Some(trace_context) = env::args().nth(2) {
let extractor: HashMap<String, String> =
serde_json::from_str(&trace_context).unwrap();
let context =
global::get_text_map_propagator(|propagator| propagator.extract(&extractor));
Span::current().set_parent(context);
}
a2();
}
_ => println!("Usage: {} <1|2 trace_context>", args[0]),
}
}
#[instrument]
pub fn a1() {
println!("a1");
}
#[instrument]
pub fn a2() {
println!("a2");
}
#[instrument]
fn spawn() {
let cmd = env::current_exe().unwrap();
let cwd = env::current_dir().unwrap();
let mut command = Command::new(cmd);
let mut injector: HashMap<String, String> = HashMap::default();
global::get_text_map_propagator(|propagator| {
// We must explicitely retrieve the context from `tracing`
propagator.inject_context(&Span::current().context(), &mut injector);
});
let trace_context = serde_json::to_string(&injector).unwrap();
command.current_dir(cwd).arg("2").arg(trace_context);
command.spawn().unwrap();
}
fn init_tracer() -> Result<opentelemetry_sdk::trace::Tracer, TraceError> {
opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("http://localhost:4317"),
)
.with_trace_config(
sdktrace::config().with_resource(Resource::new(vec![KeyValue::new(
"service.name",
"instance3",
)])),
)
.install_simple()
}
Finally, here's the collected trace in Jaeger:
from opentelemetry-rust.
Related Issues (20)
- Flaky test HOT 1
- [Bug]: missing attributes in prometheus exporter HOT 4
- Memory optimization for metric datapoints HOT 8
- OpenTelemetry Tracing API vs Tokio-Tracing API for Distributed Tracing HOT 3
- Overview of Major blockers for GA/Stable Release
- [Bug]: Span kind is always set to "internal" in v0.22.0 HOT 4
- URL encoded values for `OTEL_EXPORTER_OTLP_HEADERS` are not supported
- Updates opentelemetry registry with Rust ecosystem crates HOT 1
- urlencoding dependency removal HOT 2
- [Bug]: converting from jaeger to otlp does not compile HOT 2
- OTLP Exporter builder issues HOT 9
- [Bug]: OpenTelemetryTracingBridge is waiting a LoggerProvider HOT 2
- Recover Metrics Sum HashMap from Mutex poisoning during lock.
- Metrics - Delta aggregation should not export unless new measurements are reported in current cycle
- Generate semantic conventions for metrics.
- Metrics SDK multi-threaded testing
- [Bug]: the Meter::i64_histogram() api removed HOT 2
- [Bug]: Context::current().with_remote_span_context() not working HOT 3
- [Bug]: Error sending data to New Relic OTEL service HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from opentelemetry-rust.