基于OpenTelemetry Java SDK从唯一ID构建Trace Context的问题
基于数据包唯一ID构建OpenTelemetry Trace上下文的问题
我有一个包含5个步骤的ETL pipeline,每个步骤可在不同线程、不同应用中执行。由于无法访问内部实现,仅能接触到处理步骤,导致传递OpenTelemetry Trace Context十分困难。我希望通过唯一标识单条数据全流程运行的ID来构建Trace Context。
示例场景
| 数据包 | 步骤 | 线程 | 应用 |
|---|---|---|---|
| id_1234 | 1 | thread_01 | app_01 |
| id_1234 | 2 | thread_02 | app_01 |
| id_1234 | 3 | thread_10 | app_02 |
| id_1234 | 4 | thread_01 | app_01 |
| id_1234 | 5 | thread_05 | app_02 |
| id_5555 | 1 | thread_05 | app_02 |
| id_5555 | 2 | thread_01 | app_02 |
| id_5555 | 3 | thread_05 | app_01 |
| id_5555 | 4 | thread_06 | app_02 |
| id_5555 | 5 | thread_15 | app_02 |
尝试的代码
public class Step1 { public void execute(DataPackage obj){ var otelContext = SpanContext.create( TraceId.fromBytes(obj.getUniqueId().getBytes()), SpanId.fromBytes(processorName.getBytes()), TraceFlags.getDefault(), TraceState.getDefault() ); var wrap = Span.wrap(otelContext); var with = Context.root().with(wrap); var span = tracer.spanBuilder("Step1").setParent(with).startSpan(); CompletableFuture.runAsync(() -> { /* 业务代码 */ }).whenComplete((c1, exception) -> { if (exception != null) { span.recordException(exception); } else { span.end(); } }); } }
遇到的问题
我用上述代码构建上下文时,出现了Span的启停不在同一Trace Context的问题。之后我手动修复了字节数组长度问题,但链路数据完全不显示在Jaeger中。我猜测是因为代码默认上下文已存在,而我无法判断何时需要创建上下文(ETL步骤执行顺序不固定),请问是否支持“upsert”上下文的方式?
内容的提问来源于stack exchange,提问作者Loading
相关产品推荐
相关产品推荐

