I am injecting the spanId and traceId flowing via HTTP header to to build a remote context and creating new Span, however the new span does not get the parent trace and span but created its own trace id. As a result it does not get attached to the desired parent span.
OpenTelemetry openTelemetry = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
Tracer tracer = openTelemetry.getTracer(INSTRUMENTATION_NAME);
String[] ids = (headers.get("x-cloud-trace-context").split(";"))[0].split("/");
SpanContext remoteContext = SpanContext.createFromRemoteParent(
ids[0],
ids[1],
TraceFlags.getSampled(),
TraceState.getDefault());
Span span = tracer
.spanBuilder("Make Call Out")
.setParent(Context.current().with(Span.wrap(remoteContext)))
.startSpan();
Span childSpan = tracer.spanBuilder("HTTP Request")
.setParent(Context.current().with(span))
.startSpan();
//Some Work
childSpan.end();
span.end();
When I see trace explorer the default trace (from Cloud Run endpoint invocation ) and custom trace (from Java code) has two different traces. Ideally they should be hooked in same hierarchy in single trace.
I realised that the span id that is extracted from the request header was a decimal number however while creating the context it expects in a hexadecimal format.
SPAN_ID is a 64-bit decimal representation of the unsigned span ID.
Reference - https://cloud.google.com/trace/docs/trace-context#http-requests