-
Notifications
You must be signed in to change notification settings - Fork 945
Load couchbase otel instrumentation for 3.1 #2524
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
anuraaga
merged 2 commits into
open-telemetry:main
from
anuraaga:couchbase-3.1-javaagent
Mar 9, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
20 changes: 20 additions & 0 deletions
20
instrumentation/couchbase/couchbase-3.1/javaagent/couchbase-3.1-javaagent.gradle
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 @@ | ||
apply from: "$rootDir/gradle/instrumentation.gradle" | ||
|
||
muzzle { | ||
pass { | ||
group = "com.couchbase.client" | ||
module = "java-client" | ||
versions = "[3.1,)" | ||
assertInverse = true | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation group: "com.couchbase.client", name: "tracing-opentelemetry", version: "0.3.3" | ||
|
||
library group: "com.couchbase.client", name: "core-io", version: "2.1.0" | ||
|
||
testLibrary group: "com.couchbase.client", name: "java-client", version: "3.1.0" | ||
|
||
testImplementation group: "org.testcontainers", name: "couchbase", version: versions.testcontainers | ||
} |
44 changes: 44 additions & 0 deletions
44
...lemetry/javaagent/instrumentation/couchbase/v3_1/CouchbaseEnvironmentInstrumentation.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,44 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.couchbase.v3_1; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import com.couchbase.client.core.env.CoreEnvironment; | ||
import com.couchbase.client.tracing.opentelemetry.OpenTelemetryRequestTracer; | ||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.javaagent.tooling.TypeInstrumentation; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class CouchbaseEnvironmentInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<? super TypeDescription> typeMatcher() { | ||
return named("com.couchbase.client.core.env.CoreEnvironment$Builder"); | ||
} | ||
|
||
@Override | ||
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { | ||
return Collections.singletonMap( | ||
isConstructor(), | ||
CouchbaseEnvironmentInstrumentation.class.getName() + "$ConstructorAdvice"); | ||
} | ||
|
||
public static class ConstructorAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void onExit(@Advice.This CoreEnvironment.Builder<?> builder) { | ||
builder.requestTracer( | ||
OpenTelemetryRequestTracer.wrap( | ||
GlobalOpenTelemetry.getTracer("io.opentelemetry.javaagent.couchbase-3.0"))); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...pentelemetry/javaagent/instrumentation/couchbase/v3_1/CouchbaseInstrumentationModule.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,43 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.couchbase.v3_1; | ||
|
||
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.tooling.InstrumentationModule; | ||
import io.opentelemetry.javaagent.tooling.TypeInstrumentation; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class CouchbaseInstrumentationModule extends InstrumentationModule { | ||
public CouchbaseInstrumentationModule() { | ||
super("couchbase", "couchbase-3.1"); | ||
} | ||
|
||
@Override | ||
protected String[] additionalHelperClassNames() { | ||
return new String[] { | ||
"com.couchbase.client.tracing.opentelemetry.OpenTelemetryRequestSpan", | ||
"com.couchbase.client.tracing.opentelemetry.OpenTelemetryRequestTracer" | ||
}; | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
// New class introduced in 3.1, the minimum version we support. | ||
// NB: Couchbase does not provide any API guarantees on their core IO artifact so reconsider | ||
// instrumenting it instead of each individual JVM artifact if this becomes unmaintainable. | ||
return hasClassesNamed("com.couchbase.client.core.cnc.TracingIdentifiers"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return Collections.singletonList(new CouchbaseEnvironmentInstrumentation()); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...umentation/couchbase/couchbase-3.1/javaagent/src/test/groovy/CouchbaseClient31Test.groovy
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 | ||
*/ | ||
|
||
import com.couchbase.client.core.error.DocumentNotFoundException | ||
import com.couchbase.client.java.Cluster | ||
import com.couchbase.client.java.Collection | ||
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification | ||
import java.time.Duration | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.testcontainers.containers.output.Slf4jLogConsumer | ||
import org.testcontainers.couchbase.BucketDefinition | ||
import org.testcontainers.couchbase.CouchbaseContainer | ||
import org.testcontainers.couchbase.CouchbaseService | ||
import spock.lang.Shared | ||
|
||
// Couchbase instrumentation is owned upstream so we don't assert on the contents of the spans, only | ||
// that the instrumentation is properly registered by the agent, meaning some spans were generated. | ||
class CouchbaseClient31Test extends AgentInstrumentationSpecification { | ||
private static final Logger log = LoggerFactory.getLogger("couchbase-container") | ||
|
||
@Shared | ||
CouchbaseContainer couchbase | ||
@Shared | ||
Cluster cluster | ||
@Shared | ||
Collection collection | ||
|
||
def setupSpec() { | ||
couchbase = new CouchbaseContainer() | ||
.withExposedPorts(8091) | ||
.withEnabledServices(CouchbaseService.KV) | ||
.withBucket(new BucketDefinition("test")) | ||
.withLogConsumer(new Slf4jLogConsumer(log)) | ||
.withStartupTimeout(Duration.ofSeconds(120)) | ||
couchbase.start() | ||
|
||
cluster = Cluster.connect(couchbase.connectionString, couchbase.username, couchbase.password) | ||
def bucket = cluster.bucket("test") | ||
collection = bucket.defaultCollection() | ||
bucket.waitUntilReady(Duration.ofSeconds(10)) | ||
} | ||
|
||
def cleanupSpec() { | ||
couchbase.stop() | ||
} | ||
|
||
def "emits spans"() { | ||
when: | ||
try { | ||
collection.get("id") | ||
} catch (DocumentNotFoundException e) { | ||
// Expected | ||
} | ||
|
||
then: | ||
assertTraces(1) { | ||
trace(0, 2) { | ||
span(0) { | ||
name(~/.*get/) | ||
} | ||
span(1) { | ||
name("dispatch_to_server") | ||
} | ||
} | ||
} | ||
|
||
cleanup: | ||
cluster.disconnect() | ||
} | ||
} |
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.
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.
This is interesting: if we're going to include external library instrumentations in the javaagent (and we're probably going to do that, since it seems to be a good way to make the agent a bit slimmer & simpler) then we need to improve muzzle a bit.
Right now muzzle not only does not collect helper classes from 3rd party packages, it also collects no references whatsoever. Which means that we actually don't have the muzzle check both in build time and runtime.
It should be possible to define the set of packages for each module, by default it'd be same as
InstrumentationClassPredicate
- I'm going to open an issue for that.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.
Turns out there already was one: #1395
I've just added my 3 cents there