opencensus-java icon indicating copy to clipboard operation
opencensus-java copied to clipboard

How to set a custom Tracestate to a root span

Open HongZhaoHua opened this issue 5 years ago • 1 comments

I want to set a custom Tracestate to a root span.

But the api only support set a Tracestate as follow:

tracestate.set("key", "value"); SpanContext spanContext = SpanContext.create(traceId, spanId, traceOption, tracestate); Span span = tracer.spanBuilderWithRemoteParent(spanName, spanContext);

But in this way. if spanContext.isValid == true, i only can get a non root span. if spanContext.isValid == false, i will loss tracestate.

HongZhaoHua avatar Sep 11 '20 10:09 HongZhaoHua

here is the code:

    Tracer tracer = Tracing.getTracer();
    TraceId traceId = TraceId.generateRandomId(RandomUtility.getRandom());
    SpanId spanId = SpanId.generateRandomId(RandomUtility.getRandom());
    TraceOptions traceOption = TraceOptions.builder().build();
    Tracestate traceState = Tracestate.builder().set("userId", "xxxxx").build();
    SpanContext spanContext = SpanContext.create(traceId, spanId, traceOption, traceState);

    // TODO I hope get a root span, but in fact, i just get a non root span
    Span span = tracer.spanBuilderWithRemoteParent("root", spanContext).startSpan();

How do I build a root span with Tracestate?

HongZhaoHua avatar Sep 14 '20 01:09 HongZhaoHua