-
Notifications
You must be signed in to change notification settings - Fork 871
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improve][pulsar] Enhance SendCallback to address PIP-363 (#11648)
Co-authored-by: Lauri Tulmin <[email protected]> Co-authored-by: Lauri Tulmin <[email protected]>
- Loading branch information
1 parent
2b42bc2
commit 7d004b5
Showing
5 changed files
with
118 additions
and
54 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
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
23 changes: 23 additions & 0 deletions
23
...rc/main/java/io/opentelemetry/javaagent/instrumentation/pulsar/v2_8/SendCallbackData.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,23 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.pulsar.v2_8; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.javaagent.instrumentation.pulsar.v2_8.telemetry.PulsarRequest; | ||
|
||
public final class SendCallbackData { | ||
public final Context context; | ||
public final PulsarRequest request; | ||
|
||
private SendCallbackData(Context context, PulsarRequest request) { | ||
this.context = context; | ||
this.request = request; | ||
} | ||
|
||
public static SendCallbackData create(Context context, PulsarRequest request) { | ||
return new SendCallbackData(context, request); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...a/io/opentelemetry/javaagent/instrumentation/pulsar/v2_8/SendCallbackInstrumentation.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,75 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.pulsar.v2_8; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasSuperType; | ||
import static io.opentelemetry.javaagent.instrumentation.pulsar.v2_8.telemetry.PulsarSingletons.producerInstrumenter; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
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<ClassLoader> classLoaderOptimization() { | ||
return hasClassesNamed("org.apache.pulsar.client.impl.SendCallback"); | ||
} | ||
|
||
@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")), | ||
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. | ||
SendCallbackData callBackData = VirtualFieldStore.extract(callback); | ||
if (callBackData != null) { | ||
// If the extraction was successful, store the Context and PulsarRequest in local variables. | ||
otelContext = callBackData.context; | ||
request = callBackData.request; | ||
otelScope = otelContext.makeCurrent(); | ||
} | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Argument(0) Throwable t, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("otelRequest") PulsarRequest request) { | ||
if (otelScope != null) { | ||
// Close the Scope and end the span. | ||
otelScope.close(); | ||
producerInstrumenter().end(otelContext, request, null, t); | ||
} | ||
} | ||
} | ||
} |
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