Question about Trace propagation

2 views
Skip to first unread message

Valentin Leblanc

unread,
Apr 28, 2021, 4:34:57 AM4/28/21
to census-d...@googlegroups.com

Hello,

In Java, I am trying to propagate a SpanContext through an HTTP POST request (org.apache.http.client.methods.HttpPost). I passed the context arguments to the headers as follow :

    try (Scope scope = tracer.spanBuilder("firstSpan").startScopedSpan()) {

        SpanContext spanContext = tracer.getCurrentSpan().getContext();
        
        StringBuilder builder =
                new StringBuilder()
                    .append(spanContext.getTraceId().toLowerBase16())
                    .append(SPAN_ID_DELIMITER)
                    .append(UnsignedLongs.toString(spanIdToLong(spanContext.getSpanId())))
                    .append(TRACE_OPTION_DELIMITER)
                    .append(spanContext.getTraceOptions().isSampled() ? SAMPLED : NOT_SAMPLED);

        httpPost.addHeader(HEADER_NAME, builder.toString());

        HttpResponse response = reportSession.getHttpClient().execute(httpPost,reportSession.getHttpContext());

The request is handled by a Servlet (in a separate JAR) in which I retrieve the arguments and try to open a new Span :

    String headerStr = req.getHeader(HEADER_NAME);

    TraceId traceId = TraceId.fromLowerBase16(headerStr.subSequence(0, TRACE_ID_SIZE));

    int traceOptionsPos = headerStr.indexOf(TRACE_OPTION_DELIMITER, TRACE_ID_SIZE);
    CharSequence spanIdStr = headerStr.subSequence(SPAN_ID_START_POS, traceOptionsPos < 0 ? headerStr.length() : traceOptionsPos);

    SpanId spanId = longToSpanId(UnsignedLongs.parseUnsignedLong(spanIdStr.toString(), 10));
    TraceOptions traceOptions = OPTIONS_NOT_SAMPLED;

    if (traceOptionsPos > 0) {
        String traceOptionsStr = headerStr.substring(traceOptionsPos + TRACE_OPTION_DELIMITER_SIZE);
        if ((UnsignedInts.parseUnsignedInt(traceOptionsStr, 10) & CLOUD_TRACE_IS_SAMPLED) != 0) {
            traceOptions = OPTIONS_SAMPLED;
        }
    }

    SpanContext context = SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);

    try (Scope scope = tracer.spanBuilderWithRemoteParent("newSpan", context).setSpanKind(Kind.SERVER).startScopedSpan())  {
       // execute Servlet code
    }

But in the Google Cloud platform, the only Span I can see is the "firstSpan" and the "newSpan" is not existing in the monitor... What am I missing here ?

Thank you,


Best regards


Valentin


Reply all
Reply to author
Forward
0 new messages