简体   繁体   English

如何使用 opentelemetry 在 Spring sleuth 中设置自定义 traceId

[英]How to set custom traceId in Spring sleuth with opentelemetry

I have a usecase where we are sending a message from process A to process B via gcp pub/sub.我有一个用例,我们通过 gcp pub/sub 从进程 A 向进程 B 发送消息。 Tracing is initiated in process A and want to use same traceId in process B. How can i override trace Id in process B when new span is started.跟踪是在进程 A 中启动的,并且希望在进程 B 中使用相同的 traceId。当启动新跨度时,如何覆盖进程 B 中的跟踪 ID。

Before using openTelemetry, with Brave i could override traceId using following code在使用 openTelemetry 之前,使用 Brave 我可以使用以下代码覆盖 traceId

private Span createNewSpan(long traceId) {
    TraceContext traceContext = TraceContext.newBuilder()
            .traceId(traceId)
            .spanId(RandomUtils.nextLong())
            .build();
    return tracer.nextSpan(TraceContextOrSamplingFlags.create(traceContext));
}

Is it possible to do same in spring sleuth with openTelemetry, https://spring-projects-experimental.github.io/spring-cloud-sleuth-otel/docs/current/reference/html/howto.html#how-to-set-up-sleuth-with-otel Is it possible to do same in spring sleuth with openTelemetry, https://spring-projects-experimental.github.io/spring-cloud-sleuth-otel/docs/current/reference/html/howto.html#how-to-设置侦探与 otel

Finally able to solve it.终于可以解决了。 Spring slueth interface doesn't support it out of box, Need to use OpenTelemetry native library for creating span with a traceId. Spring slueth 接口不支持开箱即用,需要使用 OpenTelemetry 本机库创建带有 traceId 的 span。

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.*;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;


private Span createNewSpan(String traceId) {
        SpanContext remoteContext = SpanContext.create(traceId,SpanId.fromLong(RandomUtils.nextLong()),
                TraceFlags.getSampled(),
                TraceState.getDefault());
        return openTelemetry.getTracer("").spanBuilder("TEST_SPAN")
                .setParent(Context.current().with(Span.wrap(remoteContext)))
                .startSpan();
}

    Span span = createNewSpan(traceId);
    try (Scope scope = span.makeCurrent()) {

    }finally {
        span.end();
    }

I had a similar problem and found an alternative solution with starting new scope:我遇到了类似的问题,并找到了启动新 scope 的替代解决方案:

import org.apache.commons.lang3.RandomUtils;
import org.springframework.cloud.sleuth.CurrentTraceContext;
import org.springframework.cloud.sleuth.TraceContext;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.internal.EncodingUtils;

void test(String traceId) {
    CurrentTraceContext currentContext = tracer.currentTraceContext();
    // TODO handle currentContext is undefined
    TraceContext newTraceContext = tracer.traceContextBuilder()
            .traceId(traceId)
            .parentId(currentContext.context().traceId())
            .spanId(EncodingUtils.fromLong(RandomUtils.nextLong()))
            .sampled(true)
            .build();
    try (CurrentTraceContext.Scope scope = currentContext.newScope(newTraceContext)) {
        // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM