3

I'm setting up a Standard, Java, AppEngine Service and I need to trace custom location in the code. I have followed the tutorial provided by Google: https://cloud.google.com/trace/docs/setup/java to setup the tracing, however the issue, is that that approach creates a new trace rather than appending new spans to the trace started by Google.

To append new spans to an existing trace I have tried extracting the traceId and rootSpanId from X-Cloud-Trace-Context header, and using spanBuilderWithRemoteParent method to create child spans.

However, the problem is that, SpanId.fromLowerBase16 expects a 16 byte ID, where as the rootSpanId is 19 bytes long. Thus my code throws a java.lang.IllegalArgumentException: Invalid size: expected 16, got 19 exception.

Code example:

List<String> traceHeaders = headers.get("X-Cloud-Trace-Context");
String traceID = "";
String rootSpanId = "";
if (traceHeaders.size() > 0) {
    traceID = traceHeaders.get(0).split("/")[0];
    rootSpanId = traceHeaders.get(0).split("/")[1].split(";")[0];
}

Span sp1 =
    tracer
        .spanBuilderWithRemoteParent(
            "fetch-data",
            SpanContext.create(
                TraceId.fromLowerBase16(traceID),
                SpanId.fromLowerBase16(rootSpanId), <- exception thrown here
                tracer.getCurrentSpan().getContext().getTraceOptions()))
        .setSampler(Samplers.alwaysSample())
        .startSpan();

Thus, is there a way to append customs spans to an existing Stackdriver trace using Opencensus library, or are we limited to using Stackdriver Trace API?

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
Vito Tumas
  • 31
  • 2

0 Answers0