Add db.url for jedis-1.4 and jedis-3.0 (#316)

* Add db.url for jedis-1.4

* Add db.url for jedis-3.0
This commit is contained in:
Trask Stalnaker 2020-04-14 12:00:09 -07:00 committed by GitHub
parent b127673610
commit d94bf95bcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 18 deletions

View File

@ -18,9 +18,9 @@ package io.opentelemetry.auto.instrumentation.jedis.v1_4;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.DatabaseClientDecorator;
import io.opentelemetry.trace.Tracer;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.Connection;
public class JedisClientDecorator extends DatabaseClientDecorator<Protocol.Command> {
public class JedisClientDecorator extends DatabaseClientDecorator<Connection> {
public static final JedisClientDecorator DECORATE = new JedisClientDecorator();
public static final Tracer TRACER =
@ -42,12 +42,17 @@ public class JedisClientDecorator extends DatabaseClientDecorator<Protocol.Comma
}
@Override
protected String dbUser(final Protocol.Command session) {
protected String dbUser(final Connection connection) {
return null;
}
@Override
protected String dbInstance(final Protocol.Command session) {
protected String dbInstance(final Connection connection) {
return null;
}
@Override
protected String dbUrl(final Connection connection) {
return connection.getHost() + ":" + connection.getPort();
}
}

View File

@ -22,12 +22,12 @@ import static io.opentelemetry.trace.Span.Kind.CLIENT;
import static io.opentelemetry.trace.TracingContextUtils.currentContextWith;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import io.opentelemetry.auto.bootstrap.CallDepthThreadLocalMap;
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
import io.opentelemetry.auto.tooling.Instrumenter;
import io.opentelemetry.trace.Span;
@ -36,6 +36,7 @@ import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import redis.clients.jedis.Connection;
import redis.clients.jedis.Protocol.Command;
@AutoService(Instrumenter.class)
@ -53,7 +54,7 @@ public final class JedisInstrumentation extends Instrumenter.Default {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("redis.clients.jedis.Protocol");
return named("redis.clients.jedis.Connection");
}
@Override
@ -67,9 +68,8 @@ public final class JedisInstrumentation extends Instrumenter.Default {
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
isMethod()
.and(isPublic())
.and(named("sendCommand"))
.and(takesArgument(1, named("redis.clients.jedis.Protocol$Command"))),
.and(takesArgument(0, named("redis.clients.jedis.Protocol$Command"))),
JedisInstrumentation.class.getName() + "$JedisAdvice");
// FIXME: This instrumentation only incorporates sending the command, not processing the result.
}
@ -77,9 +77,16 @@ public final class JedisInstrumentation extends Instrumenter.Default {
public static class JedisAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static SpanWithScope onEnter(@Advice.Argument(1) final Command command) {
public static SpanWithScope onEnter(
@Advice.This final Connection connection, @Advice.Argument(0) final Command command) {
final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(Connection.class);
if (callDepth > 0) {
return null;
}
final Span span = TRACER.spanBuilder(command.name()).setSpanKind(CLIENT).startSpan();
DECORATE.afterStart(span);
DECORATE.onConnection(span, connection);
DECORATE.onStatement(span, command.name());
return new SpanWithScope(span, currentContextWith(span));
}
@ -87,6 +94,11 @@ public final class JedisInstrumentation extends Instrumenter.Default {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final SpanWithScope spanWithScope, @Advice.Thrown final Throwable throwable) {
if (spanWithScope == null) {
return;
}
CallDepthThreadLocalMap.reset(Connection.class);
final Span span = spanWithScope.getSpan();
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);

View File

@ -64,6 +64,7 @@ class JedisClientTest extends AgentTestRunner {
spanKind CLIENT
tags {
"$Tags.DB_TYPE" "redis"
"$Tags.DB_URL" "localhost:$port"
"$Tags.DB_STATEMENT" "SET"
}
}
@ -86,6 +87,7 @@ class JedisClientTest extends AgentTestRunner {
spanKind CLIENT
tags {
"$Tags.DB_TYPE" "redis"
"$Tags.DB_URL" "localhost:$port"
"$Tags.DB_STATEMENT" "SET"
}
}
@ -96,6 +98,7 @@ class JedisClientTest extends AgentTestRunner {
spanKind CLIENT
tags {
"$Tags.DB_TYPE" "redis"
"$Tags.DB_URL" "localhost:$port"
"$Tags.DB_STATEMENT" "GET"
}
}
@ -118,6 +121,7 @@ class JedisClientTest extends AgentTestRunner {
spanKind CLIENT
tags {
"$Tags.DB_TYPE" "redis"
"$Tags.DB_URL" "localhost:$port"
"$Tags.DB_STATEMENT" "SET"
}
}
@ -128,6 +132,7 @@ class JedisClientTest extends AgentTestRunner {
spanKind CLIENT
tags {
"$Tags.DB_TYPE" "redis"
"$Tags.DB_URL" "localhost:$port"
"$Tags.DB_STATEMENT" "RANDOMKEY"
}
}

View File

@ -18,9 +18,9 @@ package io.opentelemetry.auto.instrumentation.jedis.v3_0;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.DatabaseClientDecorator;
import io.opentelemetry.trace.Tracer;
import redis.clients.jedis.commands.ProtocolCommand;
import redis.clients.jedis.Connection;
public class JedisClientDecorator extends DatabaseClientDecorator<ProtocolCommand> {
public class JedisClientDecorator extends DatabaseClientDecorator<Connection> {
public static final JedisClientDecorator DECORATE = new JedisClientDecorator();
public static final Tracer TRACER =
@ -42,12 +42,17 @@ public class JedisClientDecorator extends DatabaseClientDecorator<ProtocolComman
}
@Override
protected String dbUser(final ProtocolCommand session) {
protected String dbUser(final Connection connection) {
return null;
}
@Override
protected String dbInstance(final ProtocolCommand session) {
protected String dbInstance(final Connection connection) {
return null;
}
@Override
protected String dbUrl(final Connection connection) {
return connection.getHost() + ":" + connection.getPort();
}
}

View File

@ -21,11 +21,11 @@ import static io.opentelemetry.trace.Span.Kind.CLIENT;
import static io.opentelemetry.trace.TracingContextUtils.currentContextWith;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.google.auto.service.AutoService;
import io.opentelemetry.auto.bootstrap.CallDepthThreadLocalMap;
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
import io.opentelemetry.auto.tooling.Instrumenter;
import io.opentelemetry.trace.Span;
@ -35,6 +35,7 @@ import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import redis.clients.jedis.Connection;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.commands.ProtocolCommand;
@ -47,7 +48,7 @@ public final class JedisInstrumentation extends Instrumenter.Default {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("redis.clients.jedis.Protocol");
return named("redis.clients.jedis.Connection");
}
@Override
@ -61,9 +62,8 @@ public final class JedisInstrumentation extends Instrumenter.Default {
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
isMethod()
.and(isPublic())
.and(named("sendCommand"))
.and(takesArgument(1, named("redis.clients.jedis.commands.ProtocolCommand"))),
.and(takesArgument(0, named("redis.clients.jedis.commands.ProtocolCommand"))),
JedisInstrumentation.class.getName() + "$JedisAdvice");
// FIXME: This instrumentation only incorporates sending the command, not processing the result.
}
@ -71,7 +71,14 @@ public final class JedisInstrumentation extends Instrumenter.Default {
public static class JedisAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static SpanWithScope onEnter(@Advice.Argument(1) final ProtocolCommand command) {
public static SpanWithScope onEnter(
@Advice.This final Connection connection,
@Advice.Argument(0) final ProtocolCommand command) {
final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(Connection.class);
if (callDepth > 0) {
return null;
}
final String query;
if (command instanceof Protocol.Command) {
query = ((Protocol.Command) command).name();
@ -82,6 +89,7 @@ public final class JedisInstrumentation extends Instrumenter.Default {
}
final Span span = TRACER.spanBuilder(query).setSpanKind(CLIENT).startSpan();
DECORATE.afterStart(span);
DECORATE.onConnection(span, connection);
DECORATE.onStatement(span, query);
return new SpanWithScope(span, currentContextWith(span));
}
@ -89,6 +97,11 @@ public final class JedisInstrumentation extends Instrumenter.Default {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Enter final SpanWithScope spanWithScope, @Advice.Thrown final Throwable throwable) {
if (spanWithScope == null) {
return;
}
CallDepthThreadLocalMap.reset(Connection.class);
final Span span = spanWithScope.getSpan();
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);

View File

@ -64,6 +64,7 @@ class Jedis30ClientTest extends AgentTestRunner {
spanKind CLIENT
tags {
"$Tags.DB_TYPE" "redis"
"$Tags.DB_URL" "localhost:$port"
"$Tags.DB_STATEMENT" "SET"
}
}
@ -86,6 +87,7 @@ class Jedis30ClientTest extends AgentTestRunner {
spanKind CLIENT
tags {
"$Tags.DB_TYPE" "redis"
"$Tags.DB_URL" "localhost:$port"
"$Tags.DB_STATEMENT" "SET"
}
}
@ -96,6 +98,7 @@ class Jedis30ClientTest extends AgentTestRunner {
spanKind CLIENT
tags {
"$Tags.DB_TYPE" "redis"
"$Tags.DB_URL" "localhost:$port"
"$Tags.DB_STATEMENT" "GET"
}
}
@ -118,6 +121,7 @@ class Jedis30ClientTest extends AgentTestRunner {
spanKind CLIENT
tags {
"$Tags.DB_TYPE" "redis"
"$Tags.DB_URL" "localhost:$port"
"$Tags.DB_STATEMENT" "SET"
}
}
@ -128,6 +132,7 @@ class Jedis30ClientTest extends AgentTestRunner {
spanKind CLIENT
tags {
"$Tags.DB_TYPE" "redis"
"$Tags.DB_URL" "localhost:$port"
"$Tags.DB_STATEMENT" "RANDOMKEY"
}
}