Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
package io.opentelemetry.javaagent.instrumentation.lettuce.v4_0;

import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.instrumentation.lettuce.v4_0.LettuceSingletons.COMMAND_CONNECTION_INFO;
import static io.opentelemetry.javaagent.instrumentation.lettuce.v4_0.LettuceSingletons.CONNECTION_URI;
import static io.opentelemetry.javaagent.instrumentation.lettuce.v4_0.LettuceSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.lambdaworks.redis.AbstractRedisAsyncCommands;
import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.api.StatefulConnection;
import com.lambdaworks.redis.protocol.AsyncCommand;
import com.lambdaworks.redis.protocol.RedisCommand;
import io.opentelemetry.context.Context;
Expand Down Expand Up @@ -60,7 +65,16 @@ public void end(
}

@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope onEnter(@Advice.Argument(0) RedisCommand<?, ?, ?> command) {
public static AdviceScope onEnter(
@Advice.This AbstractRedisAsyncCommands<?, ?> asyncCommands,
@Advice.Argument(0) RedisCommand<?, ?, ?> command) {

StatefulConnection<?, ?> connection = asyncCommands.getConnection();
RedisURI redisUri = connection != null ? CONNECTION_URI.get(connection) : null;

if (redisUri != null) {
COMMAND_CONNECTION_INFO.set(command, redisUri);
}

Context parentContext = currentContext();
if (!instrumenter().shouldStart(parentContext, command)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
package io.opentelemetry.javaagent.instrumentation.lettuce.v4_0;

import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.instrumentation.lettuce.v4_0.LettuceSingletons.CONNECTION_URI;
import static io.opentelemetry.javaagent.instrumentation.lettuce.v4_0.LettuceSingletons.connectInstrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.api.StatefulConnection;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
Expand Down Expand Up @@ -66,8 +68,14 @@ public static AdviceScope onEnter(@Advice.Argument(1) RedisURI redisUri) {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Argument(1) RedisURI redisUri,
@Advice.Return @Nullable StatefulConnection<?, ?> connection,
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {

if (connection != null) {
CONNECTION_URI.set(connection, redisUri);
}

if (adviceScope != null) {
adviceScope.end(throwable, redisUri);
}
Expand Down
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.lettuce.v4_0;

import static io.opentelemetry.javaagent.instrumentation.lettuce.v4_0.LettuceSingletons.COMMAND_CONNECTION_INFO;

import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.protocol.RedisCommand;
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesGetter;
import javax.annotation.Nullable;

public final class LettuceNetworkAttributesGetter
implements ServerAttributesGetter<RedisCommand<?, ?, ?>> {

@Override
@Nullable
public String getServerAddress(RedisCommand<?, ?, ?> redisCommand) {
RedisURI redisUri = COMMAND_CONNECTION_INFO.get(redisCommand);
return redisUri != null ? redisUri.getHost() : null;
}

@Override
@Nullable
public Integer getServerPort(RedisCommand<?, ?, ?> redisCommand) {
RedisURI redisUri = COMMAND_CONNECTION_INFO.get(redisCommand);
return redisUri != null ? redisUri.getPort() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.javaagent.instrumentation.lettuce.v4_0;

import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.api.StatefulConnection;
import com.lambdaworks.redis.protocol.AsyncCommand;
import com.lambdaworks.redis.protocol.RedisCommand;
import io.opentelemetry.api.GlobalOpenTelemetry;
Expand Down Expand Up @@ -33,29 +34,35 @@ public final class LettuceSingletons {

public static final VirtualField<AsyncCommand<?, ?, ?>, Context> CONTEXT =
VirtualField.find(AsyncCommand.class, Context.class);
public static final VirtualField<StatefulConnection<?, ?>, RedisURI> CONNECTION_URI =
VirtualField.find(StatefulConnection.class, RedisURI.class);
public static final VirtualField<RedisCommand<?, ?, ?>, RedisURI> COMMAND_CONNECTION_INFO =
VirtualField.find(RedisCommand.class, RedisURI.class);

static {
LettuceDbAttributesGetter dbAttributesGetter = new LettuceDbAttributesGetter();
LettuceNetworkAttributesGetter netAttributesGetter = new LettuceNetworkAttributesGetter();

INSTRUMENTER =
Instrumenter.<RedisCommand<?, ?, ?>, Void>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
DbClientSpanNameExtractor.create(dbAttributesGetter))
.addAttributesExtractor(DbClientAttributesExtractor.create(dbAttributesGetter))
.addAttributesExtractor(ServerAttributesExtractor.create(netAttributesGetter))
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());

LettuceConnectNetworkAttributesGetter netAttributesGetter =
LettuceConnectNetworkAttributesGetter connectNetAttributesGetter =
new LettuceConnectNetworkAttributesGetter();

CONNECT_INSTRUMENTER =
Instrumenter.<RedisURI, Void>builder(
GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, redisUri -> "CONNECT")
.addAttributesExtractor(ServerAttributesExtractor.create(netAttributesGetter))
.addAttributesExtractor(ServerAttributesExtractor.create(connectNetAttributesGetter))
.addAttributesExtractor(
PeerServiceAttributesExtractor.create(
netAttributesGetter, AgentCommonConfig.get().getPeerServiceResolver()))
connectNetAttributesGetter, AgentCommonConfig.get().getPeerServiceResolver()))
.addAttributesExtractor(new LettuceConnectAttributesExtractor())
.setEnabled(
AgentInstrumentationConfig.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ void testSetCommandUsingFutureGetWithTimeout()
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "SET"))));
equalTo(maybeStable(DB_OPERATION), "SET"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port))));
}

@Test
Expand Down Expand Up @@ -234,7 +236,9 @@ void testCommandChainedWithThenAccept() {
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "GET")),
equalTo(maybeStable(DB_OPERATION), "GET"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port)),
span ->
span.hasName("callback")
.hasKind(SpanKind.INTERNAL)
Expand Down Expand Up @@ -296,7 +300,9 @@ void getNonExistentKeyCommandWithHandleAsyncAndChainedWithThenApply() {
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "GET")),
equalTo(maybeStable(DB_OPERATION), "GET"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port)),
span ->
span.hasName("callback1")
.hasKind(SpanKind.INTERNAL)
Expand Down Expand Up @@ -337,7 +343,9 @@ void testCommandWithNoArgumentsUsingBiconsumer() {
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "RANDOMKEY")),
equalTo(maybeStable(DB_OPERATION), "RANDOMKEY"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port)),
span ->
span.hasName("callback")
.hasKind(SpanKind.INTERNAL)
Expand Down Expand Up @@ -381,15 +389,19 @@ void testHashSetAndThenNestApplyToHashGetall() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "HMSET"))),
equalTo(maybeStable(DB_OPERATION), "HMSET"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("HGETALL")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "HGETALL"))));
equalTo(maybeStable(DB_OPERATION), "HGETALL"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port))));
}

@Test
Expand Down Expand Up @@ -425,7 +437,9 @@ void testCommandCompletesExceptionally() {
new ArrayList<>(
Arrays.asList(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "DEL")));
equalTo(maybeStable(DB_OPERATION), "DEL"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port)));
if (SemconvStability.emitStableDatabaseSemconv()) {
assertions.add(equalTo(ERROR_TYPE, "java.lang.IllegalStateException"));
}
Expand Down Expand Up @@ -476,6 +490,8 @@ void testCommandBeforeItFinished() {
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "SADD"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port),
equalTo(booleanKey("lettuce.command.cancelled"), true)),
span ->
span.hasName("callback")
Expand Down Expand Up @@ -512,7 +528,9 @@ void testDebugSegfaultCommandWithNoArgumentShouldProduceSpan() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "DEBUG"))));
equalTo(maybeStable(DB_OPERATION), "DEBUG"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, serverPort))));
}

@Test
Expand Down Expand Up @@ -546,6 +564,8 @@ void testShutdownCommandShouldProduceSpan() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "SHUTDOWN"))));
equalTo(maybeStable(DB_OPERATION), "SHUTDOWN"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, shutdownServerPort))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ void testSetCommand() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "SET"))));
equalTo(maybeStable(DB_OPERATION), "SET"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port))));
}

@Test
Expand All @@ -184,7 +186,9 @@ void testGetCommand() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "GET"))));
equalTo(maybeStable(DB_OPERATION), "GET"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port))));
}

@Test
Expand All @@ -200,7 +204,9 @@ void testGetNonExistentKeyCommand() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "GET"))));
equalTo(maybeStable(DB_OPERATION), "GET"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port))));
}

@Test
Expand All @@ -216,7 +222,9 @@ void testCommandWithNoArguments() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "RANDOMKEY"))));
equalTo(maybeStable(DB_OPERATION), "RANDOMKEY"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port))));
}

@Test
Expand All @@ -232,7 +240,9 @@ void testListCommand() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "LPUSH"))));
equalTo(maybeStable(DB_OPERATION), "LPUSH"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port))));
}

@Test
Expand All @@ -248,7 +258,9 @@ void testHashSetCommand() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "HMSET"))));
equalTo(maybeStable(DB_OPERATION), "HMSET"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port))));
}

@Test
Expand All @@ -264,7 +276,9 @@ void testHashGetallCommand() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "HGETALL"))));
equalTo(maybeStable(DB_OPERATION), "HGETALL"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, port))));
}

@Test
Expand Down Expand Up @@ -296,7 +310,9 @@ void testDebugSegfaultCommandWithNoArgumentShouldProduceSpan() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "DEBUG"))));
equalTo(maybeStable(DB_OPERATION), "DEBUG"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, serverPort))));
}

@Test
Expand Down Expand Up @@ -330,6 +346,8 @@ void testShutdownCommandShouldProduceSpan() {
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(maybeStable(DB_SYSTEM), "redis"),
equalTo(maybeStable(DB_OPERATION), "SHUTDOWN"))));
equalTo(maybeStable(DB_OPERATION), "SHUTDOWN"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, shutdownServerPort))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

package io.opentelemetry.javaagent.instrumentation.lettuce.v5_0;

import static io.opentelemetry.javaagent.instrumentation.lettuce.v5_0.LettuceSingletons.CONNECTION_URI;
import static io.opentelemetry.javaagent.instrumentation.lettuce.v5_0.LettuceSingletons.connectInstrumenter;

import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulConnection;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
Expand Down Expand Up @@ -40,6 +42,10 @@ public EndConnectAsyncBiFunction(Context context, RedisURI redisUri) {

@Override
public R apply(T t, Throwable throwable) {
if (t instanceof StatefulConnection) {
CONNECTION_URI.set((StatefulConnection<?, ?>) t, redisUri);
}

if (throwable instanceof CancellationException) {
if (CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) {
Span.fromContext(context).setAttribute("lettuce.command.cancelled", true);
Expand Down
Loading
Loading