Skip to content
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

fix jedis plugin for 2.7.2 #10982

Merged
merged 9 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
19 changes: 19 additions & 0 deletions instrumentation/jedis/jedis-1.4/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,32 @@ dependencies {
annotationProcessor("com.google.auto.value:auto-value")

implementation(project(":instrumentation:jedis:jedis-common:javaagent"))
testImplementation(project(":instrumentation:jedis:jedis-1.4:testing"))
123liuziming marked this conversation as resolved.
Show resolved Hide resolved

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

latestDepTestLibrary("redis.clients:jedis:2.+") // see jedis-3.0 module
}

testing {
suites {
val test_2_7_2 by registering(JvmTestSuite::class) {
dependencies {
implementation("redis.clients:jedis:2.7.2")
implementation(project(":instrumentation:jedis:jedis-1.4:testing"))
targets {
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
all {
testTask.configure {
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
}
}
}
}
}
}
}

tasks {
test {
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public void transform(TypeTransformer transformer) {
.and(takesArgument(0, named("redis.clients.jedis.Protocol$Command")))
.and(takesArgument(1, is(byte[][].class))),
this.getClass().getName() + "$SendCommandWithArgsAdvice");
// For version 2.7.2
transformer.applyAdviceToMethod(
isMethod()
.and(named("sendCommand"))
.and(takesArguments(1))
.and(takesArgument(0, named("redis.clients.jedis.ProtocolCommand"))),
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
this.getClass().getName() + "$SendCommandWithProtocolNoArgsAdvice");
transformer.applyAdviceToMethod(
isMethod()
.and(named("sendCommand"))
.and(takesArguments(2))
.and(takesArgument(0, named("redis.clients.jedis.ProtocolCommand")))
.and(takesArgument(1, is(byte[][].class))),
this.getClass().getName() + "$SendCommandWithProtocolCommandArgsAdvice");
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -118,4 +132,85 @@ public static void stopSpan(
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
}
}

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

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This Connection connection,
@Advice.Argument(0) Object command,
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
Protocol.Command cmd = null;
// It must be a Protocol.Command in redis 2.7.2
if (command instanceof Protocol.Command) {
123liuziming marked this conversation as resolved.
Show resolved Hide resolved
cmd = (Protocol.Command) command;
}
request = JedisRequest.create(connection, cmd);
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.Thrown Throwable throwable,
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
}

scope.close();
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
}
}

@SuppressWarnings("unused")
public static class SendCommandWithProtocolCommandArgsAdvice {
123liuziming marked this conversation as resolved.
Show resolved Hide resolved

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This Connection connection,
@Advice.Argument(0) Object command,
@Advice.Argument(1) byte[][] args,
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
Protocol.Command cmd = null;
// It must be a Protocol.Command in redis 2.7.2
if (command instanceof Protocol.Command) {
cmd = (Protocol.Command) command;
}
request = JedisRequest.create(connection, cmd, 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.Thrown Throwable throwable,
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
}

scope.close();
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,129 +5,6 @@

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

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.javaagent.instrumentation.jedis.AbstractJedisTest;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.semconv.SemanticAttributes;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.containers.GenericContainer;
import redis.clients.jedis.Jedis;

class JedisClientTest {
@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

static GenericContainer<?> redisServer =
new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379);

static int port;

static Jedis jedis;

@BeforeAll
static void setup() {
redisServer.start();
port = redisServer.getMappedPort(6379);
jedis = new Jedis("localhost", port);
}

@AfterAll
static void cleanup() {
redisServer.stop();
}

@BeforeEach
void reset() {
jedis.flushAll();
testing.clearData();
}

@Test
void setCommand() {
jedis.set("foo", "bar");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port))));
}

@Test
void getCommand() {
jedis.set("foo", "bar");
String value = jedis.get("foo");

assertThat(value).isEqualTo("bar");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET foo"),
equalTo(SemanticAttributes.DB_OPERATION, "GET"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port))));
}

@Test
void commandWithNoArguments() {
jedis.set("foo", "bar");
String value = jedis.randomKey();

assertThat(value).isEqualTo("foo");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("RANDOMKEY")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "RANDOMKEY"),
equalTo(SemanticAttributes.DB_OPERATION, "RANDOMKEY"),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port))));
}
}
class JedisClientTest extends AbstractJedisTest {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jedis.v1_4;
123liuziming marked this conversation as resolved.
Show resolved Hide resolved

import io.opentelemetry.javaagent.instrumentation.jedis.AbstractJedisTest;

class JedisClientTest extends AbstractJedisTest {}
9 changes: 9 additions & 0 deletions instrumentation/jedis/jedis-1.4/testing/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id("otel.java-conventions")
}

dependencies {
compileOnly("redis.clients:jedis:1.4.0")
api(project(":testing-common"))
implementation("org.testcontainers:testcontainers")
}
Loading
Loading