-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Merged
Merged
Add NATS instrumentation #13999
Changes from 40 commits
Commits
Show all changes
43 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 ef671c7
Merge branch 'main' into nats-instrumentation
AlixBa 617961d
Merge branch 'main' into nats-instrumentation
AlixBa dd00a7f
generate metadata
AlixBa e43d741
Merge branch 'main' into nats-instrumentation
AlixBa 1c88b1c
remove muzzle check for version without dependency
AlixBa f289e05
rename nats-2.21 to nats-2.17
AlixBa 3aae560
review fixes
AlixBa 6c092ad
disabled_by_default: false
AlixBa fefeebd
use @BeforeAll over static init
AlixBa 01a48a8
<publish/request>(args) to <publish/request>(Message)
AlixBa bdad7d3
remove receive spans
AlixBa 041a7a6
fix to minimal version
AlixBa bad1806
Merge branch 'main' into nats-instrumentation
AlixBa 85a0d63
./gradlew spotlessApply
otelbot[bot] 0a89c79
remove unwanted code
AlixBa df431c4
add tests on default inbox monitoring
AlixBa 6a3baf1
remove readOnly=false on headers
AlixBa 84ee013
forward all calls to (subject, replyTo, headers, data) instead of (me…
AlixBa a2d15b4
review fixes
AlixBa 9b0d45f
Merge branch 'main' into nats-instrumentation
AlixBa fec879b
reviews
AlixBa d6ec30e
Merge branch 'main' into nats-instrumentation
AlixBa 34e2f1d
add test on Request methods
AlixBa e7fb172
Merge branch 'main' into nats-instrumentation
laurit 043c857
review
laurit e599182
switch back to messaging.client_id, it is renamed to messaging.client…
laurit bb41af8
Merge branch 'main' into nats-instrumentation
jaydeluca 2d652da
Update instrumentation/nats/nats-2.17/library/README.md
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
Some comments aren't visible on the classic Files Changed page.
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
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
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,49 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("io.nats") | ||
module.set("jnats") | ||
versions.set("[2.17.2,)") | ||
|
||
// Could not find io.nats:nats-parent:1.0-SNAPSHOT | ||
skip("0.5.0", "0.5.1") | ||
|
||
assertInverse.set(true) | ||
} | ||
} | ||
|
||
dependencies { | ||
library("io.nats:jnats:2.17.2") | ||
|
||
implementation(project(":instrumentation:nats:nats-2.17:library")) | ||
testImplementation(project(":instrumentation:nats:nats-2.17:testing")) | ||
} | ||
|
||
tasks { | ||
withType<Test>().configureEach { | ||
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service) | ||
AlixBa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false") | ||
} | ||
|
||
val testExperimental by registering(Test::class) { | ||
testClassesDirs = sourceSets.test.get().output.classesDirs | ||
classpath = sourceSets.test.get().runtimeClasspath | ||
filter { | ||
includeTestsMatching("NatsExperimentalTest") | ||
} | ||
jvmArgs("-Dotel.instrumentation.messaging.experimental.capture-headers=captured-header") | ||
} | ||
|
||
test { | ||
filter { | ||
excludeTestsMatching("NatsExperimentalTest") | ||
} | ||
} | ||
|
||
check { | ||
dependsOn(testExperimental) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../java/io/opentelemetry/javaagent/instrumentation/nats/v2_17/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_17; | ||
|
||
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; | ||
} | ||
} |
174 changes: 174 additions & 0 deletions
174
.../opentelemetry/javaagent/instrumentation/nats/v2_17/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,174 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.nats.v2_17; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; | ||
import static io.opentelemetry.javaagent.instrumentation.nats.v2_17.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_17.internal.NatsMessageWritableHeaders; | ||
import io.opentelemetry.instrumentation.nats.v2_17.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(skipOn = Advice.OnNonDefaultValue.class) | ||
public static boolean onEnter( | ||
@Advice.This Connection connection, | ||
@Advice.Argument(0) String subject, | ||
@Advice.Argument(1) byte[] body) { | ||
// call the instrumented publish method | ||
connection.publish(subject, null, null, body); | ||
AlixBa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true; | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class PublishHeadersBodyAdvice { | ||
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class) | ||
public static boolean onEnter( | ||
@Advice.This Connection connection, | ||
@Advice.Argument(0) String subject, | ||
@Advice.Argument(1) Headers headers, | ||
@Advice.Argument(2) byte[] body) { | ||
// call the instrumented publish method | ||
connection.publish(subject, null, headers, body); | ||
return true; | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class PublishReplyToBodyAdvice { | ||
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class) | ||
public static boolean onEnter( | ||
@Advice.This Connection connection, | ||
@Advice.Argument(0) String subject, | ||
@Advice.Argument(1) String replyTo, | ||
@Advice.Argument(2) byte[] body) { | ||
// call the instrumented publish method | ||
connection.publish(subject, replyTo, null, body); | ||
return true; | ||
} | ||
} | ||
|
||
@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(value = 2, readOnly = false) Headers headers, | ||
@Advice.Argument(3) byte[] body, | ||
@Advice.Local("otelContext") Context otelContext, | ||
@Advice.Local("otelScope") Scope otelScope, | ||
@Advice.Local("natsRequest") NatsRequest natsRequest) { | ||
headers = NatsMessageWritableHeaders.create(headers); | ||
|
||
Context parentContext = Context.current(); | ||
natsRequest = NatsRequest.create(connection, subject, replyTo, 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(skipOn = Advice.OnNonDefaultValue.class) | ||
public static boolean onEnter( | ||
@Advice.This Connection connection, @Advice.Argument(0) Message message) { | ||
if (message == null) { | ||
return false; | ||
} | ||
|
||
// call the instrumented publish method | ||
connection.publish( | ||
AlixBa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
message.getSubject(), message.getReplyTo(), message.getHeaders(), message.getData()); | ||
return true; | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.