Skip to content

Jedis 4 instrumentation #4555

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 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions instrumentation/jedis/jedis-1.4/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ dependencies {
compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")

testInstrumentation(project(":instrumentation:jedis:jedis-3.0:javaagent"))
testInstrumentation(project(":instrumentation:jedis:jedis-4.0:javaagent"))

// Jedis 3.0 has API changes that prevent instrumentation from applying
latestDepTestLibrary("redis.clients:jedis:2.+")
}
Expand Down
14 changes: 4 additions & 10 deletions instrumentation/jedis/jedis-3.0/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ plugins {
}

muzzle {
fail {
group.set("redis.clients")
module.set("jedis")
versions.set("[,3.0.0)")
// TODO: remove this once jedis people release the new version correctly
skip("jedis-3.6.2")
}

pass {
group.set("redis.clients")
module.set("jedis")
versions.set("[3.0.0,)")
versions.set("[3.0.0,4)")
skip("jedis-3.6.2")
assertInverse.set(true)
}
}

Expand All @@ -28,7 +22,7 @@ dependencies {
// the tests in the event it does. The tests will end up with double spans
testInstrumentation(project(":instrumentation:jedis:jedis-1.4:javaagent"))

testLibrary("redis.clients:jedis:3.+")
latestDepTestLibrary("redis.clients:jedis:3.+")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

package io.opentelemetry.javaagent.instrumentation.jedis.v3_0;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static java.util.Collections.singletonList;
import static net.bytebuddy.matcher.ElementMatchers.not;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import java.util.List;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class JedisInstrumentationModule extends InstrumentationModule {
Expand All @@ -19,6 +22,11 @@ public JedisInstrumentationModule() {
super("jedis", "jedis-3.0");
}

@Override
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
return not(hasClassesNamed("redis.clients.jedis.CommandArguments"));
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new JedisConnectionInstrumentation());
Expand Down
29 changes: 29 additions & 0 deletions instrumentation/jedis/jedis-4.0/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
id("otel.javaagent-instrumentation")
}

muzzle {
pass {
group.set("redis.clients")
module.set("jedis")
versions.set("[4.0.0-beta1,)")
skip("jedis-3.6.2")
assertInverse.set(true)
}
}

dependencies {
library("redis.clients:jedis:4.0.0-beta1")

compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")

testInstrumentation(project(":instrumentation:jedis:jedis-1.4:javaagent"))
testInstrumentation(project(":instrumentation:jedis:jedis-3.0:javaagent"))
}

tasks {
test {
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].getService())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jedis.v4_0;

import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.instrumentation.jedis.v4_0.JedisSingletons.instrumenter;
import static java.util.Arrays.asList;
import static net.bytebuddy.matcher.ElementMatchers.is;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

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 java.net.Socket;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import redis.clients.jedis.CommandArguments;
import redis.clients.jedis.commands.ProtocolCommand;

public class JedisConnectionInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("redis.clients.jedis.Connection");
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isMethod()
.and(named("sendCommand"))
.and(takesArguments(1))
.and(takesArgument(0, named("redis.clients.jedis.CommandArguments"))),
this.getClass().getName() + "$SendCommand2Advice");

transformer.applyAdviceToMethod(
isMethod()
.and(named("sendCommand"))
.and(takesArguments(2))
.and(takesArgument(0, named("redis.clients.jedis.commands.ProtocolCommand")))
.and(takesArgument(1, is(byte[][].class))),
this.getClass().getName() + "$SendCommandAdvice");
// FIXME: This instrumentation only incorporates sending the command, not processing the result.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laurit Please create an issue about this gap

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iNikem this is a copy paste from previous version

}

@SuppressWarnings("unused")
public static class SendCommandAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) ProtocolCommand command,
@Advice.Argument(1) byte[][] args,
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
request = JedisRequest.create(command, asList(args));
if (!instrumenter().shouldStart(parentContext, request)) {
return;
}

context = instrumenter().start(parentContext, request);
scope = context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.FieldValue("socket") Socket socket,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
}

request.setSocket(socket);

scope.close();
instrumenter().end(context, request, null, throwable);
}
}

@SuppressWarnings("unused")
public static class SendCommand2Advice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) CommandArguments command,
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
request = JedisRequest.create(command);
if (!instrumenter().shouldStart(parentContext, request)) {
return;
}

context = instrumenter().start(parentContext, request);
scope = context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.FieldValue("socket") Socket socket,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
}

request.setSocket(socket);

scope.close();
instrumenter().end(context, request, null, throwable);
}
}
}
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.jedis.v4_0;

import io.opentelemetry.instrumentation.api.instrumenter.db.DbAttributesExtractor;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import javax.annotation.Nullable;

final class JedisDbAttributesExtractor extends DbAttributesExtractor<JedisRequest, Void> {
@Override
protected String system(JedisRequest request) {
return SemanticAttributes.DbSystemValues.REDIS;
}

@Override
@Nullable
protected String user(JedisRequest request) {
return null;
}

@Override
protected String name(JedisRequest request) {
return null;
}

@Override
protected String connectionString(JedisRequest request) {
return null;
}

@Override
protected String statement(JedisRequest request) {
return request.getStatement();
}

@Override
protected String operation(JedisRequest request) {
return request.getOperation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jedis.v4_0;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static java.util.Collections.singletonList;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import java.util.List;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class JedisInstrumentationModule extends InstrumentationModule {

public JedisInstrumentationModule() {
super("jedis", "jedis-4.0");
}

@Override
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
return hasClassesNamed("redis.clients.jedis.CommandArguments");
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new JedisConnectionInstrumentation());
}
}
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.jedis.v4_0;

import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesExtractor;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import javax.annotation.Nullable;

final class JedisNetAttributesExtractor
extends InetSocketAddressNetClientAttributesExtractor<JedisRequest, Void> {

@Override
@Nullable
public InetSocketAddress getAddress(JedisRequest jedisRequest, @Nullable Void unused) {
SocketAddress socketAddress = jedisRequest.getRemoteSocketAddress();
if (socketAddress != null && socketAddress instanceof InetSocketAddress) {
return (InetSocketAddress) socketAddress;
}
return null;
}

@Override
public String transport(JedisRequest jedisRequest, @Nullable Void unused) {
return SemanticAttributes.NetTransportValues.IP_TCP;
}
}
Loading