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.
String[] ids = (headers.get("x-cloud-trace-context").split(";"))[0].split("/");
logger.info(String.format("Trace Parent Context '%s' ", ids[0]));
logger.info(String.format("Span Parent Context '%s' ", ids[1]));
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")
.setAttribute("method", request.method())
.setAttribute("url", request.uri().toString())
.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.
Related Example