使用kafka流的opentracing-如何?

cgyqldqp  于 2021-06-06  发布在  Kafka
关注(0)|答案(1)|浏览(343)

我正在尝试将jaeger跟踪集成到k流中。我计划在我最重要的几条管线上添加追踪功能,我想知道怎样才能将追踪ID从一条管线传递到另一条管线?
到目前为止,我得到的是—在流处理管道的开始,我启动一个服务器范围,并将traceid保存到状态存储中。稍后,在转换管道中,我访问statestore并从transform()方法捕获跟踪。这是处理流处理中跟踪的好方法吗?

input
  .mapValues(toSomethingThatMyAppUnderstands)      
  .mapValues(this::startStreamTrace)
  .filter((k, v) -> v.isPresent())            
  .mapValues(Optional::get)                   
  .mapValues(doSomethingHereWith)       
  .flatMapValues(doSomethingElse)       
  .filter((k, v) -> isInterestingEvent(v))    
  .transform(() -> new TransformerWithTracing<SomeObjectA, SomeObjectB>(IN_MEM_STORE_NAME, someFunction), IN_MEM_STORE_NAME)
  .flatMapValues(c -> c)
  .to(outTopic, Produced.with(Serdes.String(), new EventSerde()));

public class TransformerWithTracing<V, VR> implements Transformer<String, V, KeyValue<String, VR>> {

  final Function valueAction;
  final String storeId;
  private KeyValueStore<String, String> traceIdStore;

  public TransformerWithTracing(String storeId, Function valueAction) {
    this.storeId = storeId;
    this.valueAction = valueAction;
  }

  @Override
  public void init(ProcessorContext context) {
   // KeyValueStore store = ((KeyValueStore<String, String>) context.getStateStore(storeId));
    InMemoryKeyValueStore inMemoryKeyValueStore = (InMemoryKeyValueStore) store;
    this.traceIdStore = store;
  }

  @Override
  public KeyValue<String, VR> transform(String key, V value) {
    System.out.println(traceIdStore.get(key));

    // BuildTraceHeader 
    try(Scope scope = serviceTracer.startServerSpan(traceHeader, "Converting to Enterprise Event")) {
      return KeyValue.pair(key, (VR) valueAction.apply(value));
    }
  }

  @Override
  public KeyValue<String, VR> punctuate(long timestamp) {
    return null;
  }

  @Override
  public void close() {
//    if (streamId != null)   traceIdStore.delete(streamId);
  }

}
e4yzc0pl

e4yzc0pl1#

在@jeqo的zipkin/brave repo中也有类似的想法。
https://github.com/jeqo/brave/tree/kafka-streams-processor/instrumentation/kafka-streams
opentracing contrib repo中似乎也有一些可用的东西,但似乎只在跟踪生产者/消费者级别。
https://github.com/opentracing-contrib/java-kafka-client/tree/master/opentracing-kafka-streams
伦尼

相关问题