-
Notifications
You must be signed in to change notification settings - Fork 4
Rd 12757 java tracer support kafka instrumentation #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nadav3396
merged 4 commits into
master
from
RD-12757-java-tracer-support-kafka-instrumentation
May 26, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
71 changes: 71 additions & 0 deletions
71
src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaConsumerInstrumentation.java
This file contains hidden or 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,71 @@ | ||
| package io.lumigo.core.instrumentation.impl; | ||
|
|
||
| import static net.bytebuddy.matcher.ElementMatchers.*; | ||
|
|
||
| import io.lumigo.core.SpansContainer; | ||
| import io.lumigo.core.instrumentation.LumigoInstrumentationApi; | ||
| import io.lumigo.core.instrumentation.agent.Loader; | ||
| import io.lumigo.core.utils.LRUCache; | ||
| import net.bytebuddy.agent.builder.AgentBuilder; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
| import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
| import org.apache.kafka.clients.consumer.internals.ConsumerMetadata; | ||
| import org.pmw.tinylog.Logger; | ||
|
|
||
| public class ApacheKafkaConsumerInstrumentation implements LumigoInstrumentationApi { | ||
|
|
||
| public static final String INSTRUMENTATION_PACKAGE_PREFIX = "org.apache.kafka.clients.consumer"; | ||
|
|
||
| @Override | ||
| public ElementMatcher<TypeDescription> getTypeMatcher() { | ||
| return named("org.apache.kafka.clients.consumer.KafkaConsumer"); | ||
| } | ||
|
|
||
| @Override | ||
| public AgentBuilder.Transformer.ForAdvice getTransformer() { | ||
| return new AgentBuilder.Transformer.ForAdvice() | ||
| .include(Loader.class.getClassLoader()) | ||
| .advice( | ||
| isMethod() | ||
| .and(isPublic()) | ||
| .and(named("poll")) | ||
| .and(takesArguments(1)) | ||
| .and( | ||
| returns( | ||
| named( | ||
| "org.apache.kafka.clients.consumer.ConsumerRecords"))), | ||
| ApacheKafkaConsumerAdvice.class.getName()); | ||
| } | ||
|
|
||
| public static class ApacheKafkaConsumerAdvice { | ||
| public static final SpansContainer spansContainer = SpansContainer.getInstance(); | ||
| public static final LRUCache<String, Long> startTimeMap = new LRUCache<>(1000); | ||
|
|
||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void methodEnter(@Advice.FieldValue("clientId") String clientId) { | ||
| try { | ||
| startTimeMap.put(clientId, System.currentTimeMillis()); | ||
| } catch (Exception e) { | ||
| Logger.error(e); | ||
| } | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void methodExit( | ||
| @Advice.This KafkaConsumer<?, ?> consumer, | ||
| @Advice.FieldValue("metadata") ConsumerMetadata metadata, | ||
| @Advice.FieldValue("clientId") String clientId, | ||
| @Advice.Return ConsumerRecords<?, ?> consumerRecords) { | ||
| try { | ||
| Logger.info("Handling kafka request {}", consumerRecords.hashCode()); | ||
| spansContainer.addKafkaConsumeSpan( | ||
| startTimeMap.get(clientId), consumer, metadata, consumerRecords); | ||
| } catch (Throwable error) { | ||
| Logger.error(error, "Failed to add kafka span"); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.