-
Notifications
You must be signed in to change notification settings - Fork 969
Add NATS instrumentation #13999
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
Open
AlixBa
wants to merge
15
commits into
open-telemetry:main
Choose a base branch
from
AlixBa:nats-instrumentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add NATS instrumentation #13999
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
66be356
Bootstrap NATS instrumentation with Connection.publish(Message) tracing
AlixBa 9757a46
don't propagate into headers when not possible
AlixBa 830bf68
cover all publish methods with tests
AlixBa c4f11a6
Instrument NATS Subscription.nextMessage
AlixBa 56e3656
Instrument NATS library 'request' method
AlixBa 2bb53a2
Instrument NATS agent 'request' method
AlixBa c2ca73a
Instrument NATS library 'dispatcher/subscribe' methods
AlixBa fa341b2
Refactor tests/names & minor fixes
AlixBa b02272e
Instrument NATS agent 'dispatcher' methods
AlixBa 2f78f45
./gradlew spotlessApply
otelbot[bot] 487dffb
review & fixes
AlixBa 5c4d6da
fix muzzle
AlixBa 7d1b355
add captured-headers
AlixBa 360a21f
fix dispatcher in library instrumentation
AlixBa 4e0741d
Merge branch 'main' into nats-instrumentation
AlixBa 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ out/ | |
###################### | ||
.vscode | ||
**/bin/ | ||
.metals | ||
|
||
# Others # | ||
########## | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Auth-instrumentation for NATS version 2.21 | ||
|
||
Provides OpenTelemetry auto-instrumentation for [NATS 2.21](https://github.com/nats-io/nats.java). | ||
|
||
### Trace propagation | ||
|
||
It's recommended to provide `Message` with a writable `Header` structure | ||
to allow propagation between publishers and subscribers. Without headers, | ||
the tracing context will not be propagated in the headers. | ||
|
||
```java | ||
import io.nats.client.impl.Headers; | ||
import io.nats.client.impl.NatsMessage; | ||
|
||
// don't | ||
Message msg = NatsMessage.builder().subject("sub").build(); | ||
|
||
// do | ||
Message msg = NatsMessage.builder().subject("sub").headers(new Headers()).build(); | ||
``` |
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,39 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("io.nats") | ||
module.set("jnats") | ||
versions.set("[2.17.2,)") | ||
assertInverse.set(true) | ||
} | ||
} | ||
|
||
dependencies { | ||
library("io.nats:jnats:2.21.0") | ||
|
||
implementation(project(":instrumentation:nats:nats-2.21:library")) | ||
testImplementation(project(":instrumentation:nats:nats-2.21:testing")) | ||
} | ||
|
||
tasks { | ||
val testMessagingReceive by registering(Test::class) { | ||
filter { | ||
includeTestsMatching("NatsInstrumentationMessagingReceiveTest") | ||
} | ||
jvmArgs("-Dotel.instrumentation.messaging.experimental.receive-telemetry.enabled=true") | ||
} | ||
|
||
test { | ||
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service) | ||
filter { | ||
excludeTestsMatching("NatsInstrumentationMessagingReceiveTest") | ||
} | ||
} | ||
|
||
check { | ||
dependsOn(testMessagingReceive) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../java/io/opentelemetry/javaagent/instrumentation/nats/v2_21/CompletableFutureWrapper.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,31 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.nats.v2_21; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public final class CompletableFutureWrapper { | ||
|
||
private CompletableFutureWrapper() {} | ||
|
||
public static <T> CompletableFuture<T> wrap(CompletableFuture<T> future, Context context) { | ||
CompletableFuture<T> result = new CompletableFuture<>(); | ||
future.whenComplete( | ||
(T value, Throwable throwable) -> { | ||
try (Scope ignored = context.makeCurrent()) { | ||
if (throwable != null) { | ||
result.completeExceptionally(throwable); | ||
} else { | ||
result.complete(value); | ||
} | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
} |
263 changes: 263 additions & 0 deletions
263
.../opentelemetry/javaagent/instrumentation/nats/v2_21/ConnectionPublishInstrumentation.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,263 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.nats.v2_21; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; | ||
import static io.opentelemetry.javaagent.instrumentation.nats.v2_21.NatsSingletons.PRODUCER_INSTRUMENTER; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import io.nats.client.Connection; | ||
import io.nats.client.Message; | ||
import io.nats.client.impl.Headers; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.nats.v2_21.internal.NatsRequest; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class ConnectionPublishInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return implementsInterface(named("io.nats.client.Connection")); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isPublic() | ||
.and(named("publish")) | ||
.and(takesArguments(2)) | ||
.and(takesArgument(0, String.class)) | ||
.and(takesArgument(1, byte[].class)), | ||
ConnectionPublishInstrumentation.class.getName() + "$PublishBodyAdvice"); | ||
transformer.applyAdviceToMethod( | ||
isPublic() | ||
.and(named("publish")) | ||
.and(takesArguments(3)) | ||
.and(takesArgument(0, String.class)) | ||
.and(takesArgument(1, named("io.nats.client.impl.Headers"))) | ||
.and(takesArgument(2, byte[].class)), | ||
ConnectionPublishInstrumentation.class.getName() + "$PublishHeadersBodyAdvice"); | ||
transformer.applyAdviceToMethod( | ||
isPublic() | ||
.and(named("publish")) | ||
.and(takesArguments(3)) | ||
.and(takesArgument(0, String.class)) | ||
.and(takesArgument(1, String.class)) | ||
.and(takesArgument(2, byte[].class)), | ||
ConnectionPublishInstrumentation.class.getName() + "$PublishReplyToBodyAdvice"); | ||
transformer.applyAdviceToMethod( | ||
isPublic() | ||
.and(named("publish")) | ||
.and(takesArguments(4)) | ||
.and(takesArgument(0, String.class)) | ||
.and(takesArgument(1, String.class)) | ||
.and(takesArgument(2, named("io.nats.client.impl.Headers"))) | ||
.and(takesArgument(3, byte[].class)), | ||
ConnectionPublishInstrumentation.class.getName() + "$PublishReplyToHeadersBodyAdvice"); | ||
transformer.applyAdviceToMethod( | ||
isPublic() | ||
.and(named("publish")) | ||
.and(takesArguments(1)) | ||
.and(takesArgument(0, named("io.nats.client.Message"))), | ||
ConnectionPublishInstrumentation.class.getName() + "$PublishMessageAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class PublishBodyAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.This Connection connection, | ||
@Advice.Argument(0) String subject, | ||
@Advice.Argument(1) byte[] body, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
Context parentContext = Context.current(); | ||
natsRequest = NatsRequest.create(connection, null, subject, null, body); | ||
|
||
if (!PRODUCER_INSTRUMENTER.shouldStart(parentContext, natsRequest)) { | ||
return; | ||
} | ||
|
||
otelContext = PRODUCER_INSTRUMENTER.start(parentContext, natsRequest); | ||
otelScope = otelContext.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
if (otelScope == null) { | ||
return; | ||
} | ||
|
||
otelScope.close(); | ||
PRODUCER_INSTRUMENTER.end(otelContext, natsRequest, null, throwable); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class PublishHeadersBodyAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.This Connection connection, | ||
@Advice.Argument(0) String subject, | ||
@Advice.Argument(1) Headers headers, | ||
@Advice.Argument(2) byte[] body, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
Context parentContext = Context.current(); | ||
natsRequest = NatsRequest.create(connection, null, subject, headers, body); | ||
|
||
if (!PRODUCER_INSTRUMENTER.shouldStart(parentContext, natsRequest)) { | ||
return; | ||
} | ||
|
||
otelContext = PRODUCER_INSTRUMENTER.start(parentContext, natsRequest); | ||
otelScope = otelContext.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
if (otelScope == null) { | ||
return; | ||
} | ||
|
||
otelScope.close(); | ||
PRODUCER_INSTRUMENTER.end(otelContext, natsRequest, null, throwable); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class PublishReplyToBodyAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.This Connection connection, | ||
@Advice.Argument(0) String subject, | ||
@Advice.Argument(1) String replyTo, | ||
@Advice.Argument(2) byte[] body, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
Context parentContext = Context.current(); | ||
natsRequest = NatsRequest.create(connection, replyTo, subject, null, body); | ||
|
||
if (!PRODUCER_INSTRUMENTER.shouldStart(parentContext, natsRequest)) { | ||
return; | ||
} | ||
|
||
otelContext = PRODUCER_INSTRUMENTER.start(parentContext, natsRequest); | ||
otelScope = otelContext.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
if (otelScope == null) { | ||
return; | ||
} | ||
|
||
otelScope.close(); | ||
PRODUCER_INSTRUMENTER.end(otelContext, natsRequest, null, throwable); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class PublishReplyToHeadersBodyAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.This Connection connection, | ||
@Advice.Argument(0) String subject, | ||
@Advice.Argument(1) String replyTo, | ||
@Advice.Argument(2) Headers headers, | ||
@Advice.Argument(3) byte[] body, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
Context parentContext = Context.current(); | ||
natsRequest = NatsRequest.create(connection, replyTo, subject, headers, body); | ||
|
||
if (!PRODUCER_INSTRUMENTER.shouldStart(parentContext, natsRequest)) { | ||
return; | ||
} | ||
|
||
otelContext = PRODUCER_INSTRUMENTER.start(parentContext, natsRequest); | ||
otelScope = otelContext.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
if (otelScope == null) { | ||
return; | ||
} | ||
|
||
otelScope.close(); | ||
PRODUCER_INSTRUMENTER.end(otelContext, natsRequest, null, throwable); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class PublishMessageAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.This Connection connection, | ||
@Advice.Argument(0) Message message, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
Context parentContext = Context.current(); | ||
natsRequest = NatsRequest.create(connection, message); | ||
|
||
if (!PRODUCER_INSTRUMENTER.shouldStart(parentContext, natsRequest)) { | ||
return; | ||
} | ||
|
||
otelContext = PRODUCER_INSTRUMENTER.start(parentContext, natsRequest); | ||
otelScope = otelContext.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
if (otelScope == null) { | ||
return; | ||
} | ||
|
||
otelScope.close(); | ||
PRODUCER_INSTRUMENTER.end(otelContext, natsRequest, null, throwable); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since it passes for 2.17.2+, should I change 2.21 to 2.17.2 everywhere? (library included)