Skip to content

fix jedis plugin for 2.7.2 #10982

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 9 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
13 changes: 13 additions & 0 deletions instrumentation/jedis/jedis-1.4/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,25 @@ dependencies {

implementation(project(":instrumentation:jedis:jedis-common:javaagent"))

testImplementation(project(":instrumentation:jedis:jedis-1.4:testing"))

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 version272 by registering(JvmTestSuite::class) {
dependencies {
implementation("redis.clients:jedis:2.7.2")
implementation(project(":instrumentation:jedis:jedis-1.4:testing"))
}
}
}
}

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"))),
this.getClass().getName() + "$SendCommandNoArgsAdvice");
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() + "$SendCommandWithArgsAdvice");
}

@SuppressWarnings("unused")
Expand All @@ -54,12 +68,15 @@ public static class SendCommandNoArgsAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This Connection connection,
@Advice.Argument(0) Protocol.Command command,
@Advice.Argument(0) Object command,
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
request = JedisRequest.create(connection, command);
// Must be a Protocol.Command
if (command instanceof Protocol.Command) {
request = JedisRequest.create(connection, (Protocol.Command) command);
}
if (!instrumenter().shouldStart(parentContext, request)) {
return;
}
Expand Down Expand Up @@ -89,13 +106,16 @@ public static class SendCommandWithArgsAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This Connection connection,
@Advice.Argument(0) Protocol.Command command,
@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();
request = JedisRequest.create(connection, command, asList(args));
// Must be a Protocol.Command
if (command instanceof Protocol.Command) {
request = JedisRequest.create(connection, (Protocol.Command) command, asList(args));
}
if (!instrumenter().shouldStart(parentContext, request)) {
return;
}
Expand Down
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;

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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jedis;

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

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;

public abstract class AbstractJedisTest {
@RegisterExtension
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

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

private static int port;

private static Jedis jedis;

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

@AfterAll
static void cleanup() {
REDIS_SERVER.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))));
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ include(":instrumentation:jdbc:javaagent")
include(":instrumentation:jdbc:library")
include(":instrumentation:jdbc:testing")
include(":instrumentation:jedis:jedis-1.4:javaagent")
include(":instrumentation:jedis:jedis-1.4:testing")
include(":instrumentation:jedis:jedis-3.0:javaagent")
include(":instrumentation:jedis:jedis-4.0:javaagent")
include(":instrumentation:jedis:jedis-common:javaagent")
Expand Down
Loading