How to add a custom traceId in opentelemetry-javaagent

Viewed 40

How to add a custom traceId in opentelemetry-javaagent

hi, I have a question to ask. Currently I use opentelemetry-javaagent-1.4.1 to automatically grab tracing. Now I want to add a custom traceId in each span under the attributes field. For example, a tracing consisting of three spans looks like this:

[
{
    "traceId":"traceIdA",
    "spanId":"spanIdA",
    "attributes":{
        "customTraceId":"customTraceIdA"
    }
},
{
    "traceId":"traceIdA",
    "spanId":"spanIdB",
    "attributes":{
        "customTraceId":"customTraceIdA"
    }
},
{
    "traceId":"traceIdA",
    "spanId":"spanIdC",
    "attributes":{
        "customTraceId":"customTraceIdA"
    }
}
]

How can I do the above and are there any best practices?

I have tried two ways. At first, I modified the startSpan method of the io.opentelemetry.instrumentation.api.tracer.BaseTracer class and added baggage, but there are very few tracings that will lose the custom traceId. Later, I modified the startSpan method of the io.opentelemetry.sdk.trace class and added baggage to the root span, but the custom traceId set in the baggage could not be obtained in the child span

1 Answers
Related