@Around("anyConsumerFactory()") public Object wrapConsumerFactory(ProceedingJoinPoint pjp) throws Throwable { Consumer consumer = (Consumer) pjp.proceed(); return this.kafkaTracing.consumer(consumer); }
@Setup(Level.Trial) public void init() { Tracing tracing = Tracing.newBuilder().spanReporter(Reporter.NOOP).build(); producer = new FakeProducer(); tracingProducer = KafkaTracing.create(tracing).producer(producer); tracingB3SingleProducer = KafkaTracing.newBuilder(tracing).writeB3SingleFormat(true).build().producer(producer); }
@Override public Object invoke(MethodInvocation invocation) throws Throwable { if (!"onMessage".equals(invocation.getMethod().getName())) { return invocation.proceed(); } Object[] arguments = invocation.getArguments(); Optional<Object> record = Arrays.stream(arguments) .filter(o -> o instanceof ConsumerRecord).findFirst(); if (!record.isPresent()) { return invocation.proceed(); } if (log.isDebugEnabled()) { log.debug("Wrapping onMessage call"); } Span span = this.kafkaTracing.nextSpan((ConsumerRecord<?, ?>) record.get()) .name("on-message").start(); try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span)) { return invocation.proceed(); } catch (RuntimeException | Error e) { String message = e.getMessage(); if (message == null) message = e.getClass().getSimpleName(); span.tag("error", message); throw e; } finally { span.finish(); } }
/** * Use this to create a span for processing the given record. Note: the result has no name and is * not started. * * <p>This creates a child from identifiers extracted from the record headers, or a new span if * one couldn't be extracted. */ public Span nextSpan(ConsumerRecord<?, ?> record) { TraceContextOrSamplingFlags extracted = extractAndClearHeaders(record.headers()); Span result = tracing.tracer().nextSpan(extracted); if (extracted.context() == null && !result.isNoop()) { addTags(record, result); } return result; }
final Tracing tracing = Tracing.newBuilder().localServiceName("hello-consumer") .sampler(Sampler.ALWAYS_SAMPLE).spanReporter(reporter).build(); final KafkaTracing kafkaTracing = KafkaTracing.newBuilder(tracing) .remoteServiceName("kafka").build(); consumerConfigs); final Consumer<String, String> tracingConsumer = kafkaTracing .consumer(kafkaConsumer); .poll(Duration.ofMillis(Long.MAX_VALUE)); for (ConsumerRecord<String, String> record : records) { brave.Span span = kafkaTracing.nextSpan(record).name("print-hello") .start(); span.annotate("starting printing");
@Around("anyProducerFactory()") public Object wrapProducerFactory(ProceedingJoinPoint pjp) throws Throwable { Producer producer = (Producer) pjp.proceed(); return this.kafkaTracing.producer(producer); }
@Bean @ConditionalOnMissingBean KafkaTracing kafkaTracing(Tracing tracing, SleuthMessagingProperties properties) { return KafkaTracing.newBuilder(tracing) .remoteServiceName( properties.getMessaging().getKafka().getRemoteServiceName()) .build(); }
/** * Creates a {@link KafkaStreams} instance with a tracing-enabled {@link KafkaClientSupplier}. All * Topology Sources and Sinks (including internal Topics) will create Spans on records processed * (i.e. send or consumed). * * Use this instead of {@link KafkaStreams} constructor. * * <p>Simple example: * <pre>{@code * // KafkaStreams with tracing-enabled KafkaClientSupplier * KafkaStreams kafkaStreams = kafkaStreamsTracing.kafkaStreams(topology, streamsConfig); * }</pre> * * @see TracingKafkaClientSupplier */ public KafkaStreams kafkaStreams(Topology topology, Properties streamsConfig) { final KafkaTracing kafkaTracing = KafkaTracing.create(tracing); final KafkaClientSupplier kafkaClientSupplier = new TracingKafkaClientSupplier(kafkaTracing); return new KafkaStreams(topology, streamsConfig, kafkaClientSupplier); }
ConsumerRecord<K, V> record = recordsInPartition.get(i); TraceContextOrSamplingFlags extracted = kafkaTracing.extractAndClearHeaders(record.headers());
TraceContextOrSamplingFlags extractAndClearHeaders(Headers headers) { TraceContextOrSamplingFlags extracted = extractor.extract(headers); // clear propagation headers if we were able to extract a span if (!extracted.equals(TraceContextOrSamplingFlags.EMPTY)) { clearHeaders(headers); } return extracted; }
public KafkaTracing build() { return new KafkaTracing(this); } }
@Override public Producer<byte[], byte[]> getProducer(Map<String, Object> config) { config.put("key.serializer", ByteArraySerializer.class); config.put("value.serializer", ByteArraySerializer.class); Producer<byte[], byte[]> producer = new KafkaProducer<>(config); return kafkaTracing.producer(producer); }
@Bean @ConditionalOnMissingBean KafkaTracing kafkaTracing(Tracing tracing, SleuthMessagingProperties properties) { return KafkaTracing.newBuilder(tracing) .remoteServiceName( properties.getMessaging().getKafka().getRemoteServiceName()) .build(); }
/** * Use this to create a span for processing the given record. Note: the result has no name and is * not started. * * <p>This creates a child from identifiers extracted from the record headers, or a new span if * one couldn't be extracted. */ public Span nextSpan(ConsumerRecord<?, ?> record) { TraceContextOrSamplingFlags extracted = extractAndClearHeaders(record.headers()); Span result = tracing.tracer().nextSpan(extracted); if (extracted.context() == null && !result.isNoop()) { addTags(record, result); } return result; }
span = tracer.nextSpan(kafkaTracing.extractAndClearHeaders(record.headers())); } else {
TraceContextOrSamplingFlags extractAndClearHeaders(Headers headers) { TraceContextOrSamplingFlags extracted = extractor.extract(headers); // clear propagation headers if we were able to extract a span if (!extracted.equals(TraceContextOrSamplingFlags.EMPTY)) { clearHeaders(headers); } return extracted; }
public KafkaTracing build() { return new KafkaTracing(this); } }
@Around("anyProducerFactory()") public Object wrapProducerFactory(ProceedingJoinPoint pjp) throws Throwable { Producer producer = (Producer) pjp.proceed(); return this.kafkaTracing.producer(producer); }
@Override public Consumer<byte[], byte[]> getConsumer(Map<String, Object> config) { config.put("key.deserializer", ByteArrayDeserializer.class); config.put("value.deserializer", ByteArrayDeserializer.class); Consumer<byte[], byte[]> consumer = new KafkaConsumer<>(config); return kafkaTracing.consumer(consumer); }
@Override public Object invoke(MethodInvocation invocation) throws Throwable { if (!"onMessage".equals(invocation.getMethod().getName())) { return invocation.proceed(); } Object[] arguments = invocation.getArguments(); Optional<Object> record = Arrays.stream(arguments) .filter(o -> o instanceof ConsumerRecord).findFirst(); if (!record.isPresent()) { return invocation.proceed(); } if (log.isDebugEnabled()) { log.debug("Wrapping onMessage call"); } Span span = this.kafkaTracing.nextSpan((ConsumerRecord<?, ?>) record.get()) .name("on-message").start(); try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span)) { return invocation.proceed(); } catch (RuntimeException | Error e) { String message = e.getMessage(); if (message == null) message = e.getClass().getSimpleName(); span.tag("error", message); throw e; } finally { span.finish(); } }