Create span from a trace ID #4593
-
Hi, In my job, I'm trying to create a span from the given trace ID. But everything I read so far points me to use the propagators which need both trace ID and span ID. Otherwise, the span context will be invalid and the library generates a new trace ID. This is what I'm trying to do. In this code, I return a fixed value for trace ID but in reality, the getter extracts that value from the input message. private transient Tracer tracer;
private transient TextMapGetter<I> getter;
@StartBundle
public void setup(PipelineOptions pipelineOptions) {
JobOptions options = pipelineOptions.as(JobOptions.class);
this.tracer =
GlobalOpenTelemetry.getTracer(options.getTracingAppName(), options.getTracingAppVersion());
this.getter =
new TextMapGetter<>() {
@Override
public Iterable<String> keys(I carrier) {return Collections.emptyList();}
@Override
public String get(@Nullable I carrier, String key) {return "b15efcf1b2ac2079245e6044ce09b091";}
};
}
@ProcessElement
public void processElement(@Element I message, OutputReceiver<O> receiver) {
Context extractedContext =
GlobalOpenTelemetry.getPropagators()
.getTextMapPropagator()
.extract(Context.current(), message, getter);
try (Scope unused = extractedContext.makeCurrent()) {
Span span =
tracer
.spanBuilder("dataflow job 1")
.setSpanKind(SpanKind.CONSUMER)
.setAttribute("thread.id", Thread.currentThread().getId())
.setAttribute("thread.name", Thread.currentThread().getName())
.setAttribute("code.function", getOperationName())
.setAttribute("code.namespace", this.getClass().getName())
.startSpan();
try {
operation(message, receiver);
span.setStatus(StatusCode.OK);
} catch (Exception e) {
span.recordException(e);
span.setStatus(StatusCode.ERROR, e.getClass().getSimpleName());
throw e;
} finally {
span.end();
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @irajhedayati - we'd generally expect the source to create a |
Beta Was this translation helpful? Give feedback.
Hi @irajhedayati - we'd generally expect the source to create a
PRODUCER
span too, and you'd propagate both the trace ID and span ID as message attributes. Is there a reason you only generate a trace ID at the source?