-
Notifications
You must be signed in to change notification settings - Fork 1k
[improve][pulsar] Enhance SendCallback to address PIP-363 #11648
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
+118
−54
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bd39c59
Enhance SendCallback to address PIP-363
dao-jun 7870b7d
fix codestyle
dao-jun 7b402b8
Merge branch 'refs/heads/main' into dev/PIP-363
dao-jun c4ffe63
Address review comments
dao-jun c41fafd
Address review comments
dao-jun 0c065b8
Update instrumentation/pulsar/pulsar-2.8/javaagent/src/main/java/io/o…
laurit a095484
polish
laurit aa745a0
Merge branch 'main' into dev/PIP-363
laurit 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,18 +14,14 @@ | |
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
| import io.opentelemetry.javaagent.instrumentation.pulsar.v2_8.telemetry.PulsarRequest; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
| import org.apache.pulsar.client.api.Message; | ||
| import org.apache.pulsar.client.api.MessageId; | ||
| import org.apache.pulsar.client.api.PulsarClient; | ||
| import org.apache.pulsar.client.impl.MessageImpl; | ||
| import org.apache.pulsar.client.impl.ProducerImpl; | ||
| import org.apache.pulsar.client.impl.PulsarClientImpl; | ||
| import org.apache.pulsar.client.impl.SendCallback; | ||
|
|
@@ -73,7 +69,7 @@ public static class ProducerSendAsyncMethodAdvice { | |
| public static void before( | ||
| @Advice.This ProducerImpl<?> producer, | ||
| @Advice.Argument(value = 0) Message<?> message, | ||
| @Advice.Argument(value = 1, readOnly = false) SendCallback callback) { | ||
| @Advice.Argument(value = 1) SendCallback callback) { | ||
| Context parent = Context.current(); | ||
| PulsarRequest request = PulsarRequest.create(message, VirtualFieldStore.extract(producer)); | ||
|
|
||
|
|
@@ -82,54 +78,9 @@ public static void before( | |
| } | ||
|
|
||
| Context context = producerInstrumenter().start(parent, request); | ||
| callback = new SendCallbackWrapper(context, request, callback); | ||
| } | ||
| } | ||
|
|
||
| public static class SendCallbackWrapper implements SendCallback { | ||
|
|
||
| private final Context context; | ||
| private final PulsarRequest request; | ||
| private final SendCallback delegate; | ||
|
|
||
| public SendCallbackWrapper(Context context, PulsarRequest request, SendCallback callback) { | ||
| this.context = context; | ||
| this.request = request; | ||
| this.delegate = callback; | ||
| } | ||
|
|
||
| @Override | ||
| public void sendComplete(Exception e) { | ||
| if (context == null) { | ||
| this.delegate.sendComplete(e); | ||
| return; | ||
| } | ||
|
|
||
| try (Scope ignore = context.makeCurrent()) { | ||
| this.delegate.sendComplete(e); | ||
| } finally { | ||
| producerInstrumenter().end(context, request, null, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addCallback(MessageImpl<?> msg, SendCallback scb) { | ||
| this.delegate.addCallback(msg, scb); | ||
| } | ||
|
|
||
| @Override | ||
| public SendCallback getNextSendCallback() { | ||
| return this.delegate.getNextSendCallback(); | ||
| } | ||
|
|
||
| @Override | ||
| public MessageImpl<?> getNextMessage() { | ||
| return this.delegate.getNextMessage(); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<MessageId> getFuture() { | ||
| return this.delegate.getFuture(); | ||
| // Inject the context/request into the message. This will be extracted and used when the | ||
|
||
| // message is sent and the callback is invoked. see `SendCallbackInstrumentation`. | ||
| VirtualFieldStore.inject(callback, context, request); | ||
| } | ||
| } | ||
| } | ||
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
73 changes: 73 additions & 0 deletions
73
...a/io/opentelemetry/javaagent/instrumentation/pulsar/v2_8/SendCallbackInstrumentation.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,73 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.pulsar.v2_8; | ||
|
|
||
| import static io.opentelemetry.javaagent.instrumentation.pulsar.v2_8.telemetry.PulsarSingletons.producerInstrumenter; | ||
| import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
| import io.opentelemetry.javaagent.instrumentation.pulsar.v2_8.telemetry.PulsarRequest; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
| import org.apache.pulsar.client.impl.SendCallback; | ||
|
|
||
| public class SendCallbackInstrumentation implements TypeInstrumentation { | ||
|
|
||
| @Override | ||
| public ElementMatcher<TypeDescription> typeMatcher() { | ||
| return hasSuperType(named("org.apache.pulsar.client.impl.SendCallback")); | ||
| } | ||
|
|
||
| @Override | ||
| public void transform(TypeTransformer transformer) { | ||
| transformer.applyAdviceToMethod( | ||
| isMethod().and(named("sendComplete")).and(takesArgument(0, named("java.lang.Exception"))), | ||
| SendCallbackInstrumentation.class.getName() + "$SendCallbackSendCompleteAdvice"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public static class SendCallbackSendCompleteAdvice { | ||
|
|
||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onEnter( | ||
| @Advice.This SendCallback callback, | ||
| @Advice.Local("otelContext") Context otelContext, | ||
| @Advice.Local("otelScope") Scope otelScope, | ||
| @Advice.Local("otelRequest") PulsarRequest request) { | ||
| // Extract the Context and PulsarRequest from the SendCallback instance. | ||
| Object[] objects = VirtualFieldStore.extract(callback); | ||
laurit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (objects != null | ||
| && objects.length == 2 | ||
| && objects[0] instanceof Context | ||
| && objects[1] instanceof PulsarRequest) { | ||
| // If the extraction was successful, store the Context and PulsarRequest in local variables. | ||
| otelContext = (Context) objects[0]; | ||
| request = (PulsarRequest) objects[1]; | ||
| otelScope = otelContext.makeCurrent(); | ||
| } | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit( | ||
| @Advice.Argument(0) Exception e, | ||
| @Advice.Local("otelContext") Context otelContext, | ||
| @Advice.Local("otelScope") Scope otelScope, | ||
| @Advice.Local("otelRequest") PulsarRequest request) { | ||
| if (otelScope != null && otelContext != null && request != null) { | ||
| // Close the Scope and end the span. | ||
| otelScope.close(); | ||
| producerInstrumenter().end(otelContext, request, null, e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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.