-
Notifications
You must be signed in to change notification settings - Fork 886
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start a CONSUMER span for Kafka poll(); and refactor spring-kafka... (#…
…4041) * Start a separate CONSUMER receive span for each non-empty KafkaConsumer#poll() call * One batch receive + one batch process span in spring-kafka * Add CONSUMER receive spans to kafka-streams too * codenarc * code review comments
- Loading branch information
Mateusz Rzeszutek
authored
Sep 8, 2021
1 parent
28e5cb5
commit e30b082
Showing
23 changed files
with
669 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
.../opentelemetry/javaagent/instrumentation/kafkaclients/ConsumerRecordsInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.kafkaclients; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import io.opentelemetry.javaagent.instrumentation.api.ContextStore; | ||
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
|
||
public class ConsumerRecordsInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.apache.kafka.clients.consumer.ConsumerRecords"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(isPublic()) | ||
.and(named("records")) | ||
.and(takesArgument(0, String.class)) | ||
.and(returns(Iterable.class)), | ||
ConsumerRecordsInstrumentation.class.getName() + "$IterableAdvice"); | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(isPublic()) | ||
.and(named("records")) | ||
.and(takesArgument(0, named("org.apache.kafka.common.TopicPartition"))) | ||
.and(returns(List.class)), | ||
ConsumerRecordsInstrumentation.class.getName() + "$ListAdvice"); | ||
transformer.applyAdviceToMethod( | ||
isMethod() | ||
.and(isPublic()) | ||
.and(named("iterator")) | ||
.and(takesArguments(0)) | ||
.and(returns(Iterator.class)), | ||
ConsumerRecordsInstrumentation.class.getName() + "$IteratorAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class IterableAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void wrap( | ||
@Advice.Return(readOnly = false) Iterable<ConsumerRecord<?, ?>> iterable) { | ||
if (iterable != null) { | ||
ContextStore<ConsumerRecords, SpanContext> consumerRecordsSpan = | ||
InstrumentationContext.get(ConsumerRecords.class, SpanContext.class); | ||
iterable = new TracingIterable(iterable); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ListAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void wrap(@Advice.Return(readOnly = false) List<ConsumerRecord<?, ?>> iterable) { | ||
if (iterable != null) { | ||
iterable = new TracingList(iterable); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class IteratorAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void wrap( | ||
@Advice.Return(readOnly = false) Iterator<ConsumerRecord<?, ?>> iterator) { | ||
if (iterator != null) { | ||
iterator = new TracingIterator(iterator); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.