Skip to content

Commit

Permalink
Update jedis-1.4 to Instrumenter API (#3064)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored May 24, 2021
1 parent c28af1f commit b4cab9b
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ muzzle {
dependencies {
library "redis.clients:jedis:1.4.0"

compileOnly deps.autoValueAnnotations
annotationProcessor deps.autoValue

// Jedis 3.0 has API changes that prevent instrumentation from applying
latestDepTestLibrary "redis.clients:jedis:2.+"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

import io.opentelemetry.instrumentation.api.db.RedisCommandSanitizer;
import io.opentelemetry.instrumentation.api.instrumenter.db.DbAttributesExtractor;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import org.checkerframework.checker.nullness.qual.Nullable;

final class JedisDbAttributesExtractor extends DbAttributesExtractor<JedisRequest> {
@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 request.getConnectionString();
}

@Override
protected String statement(JedisRequest request) {
return RedisCommandSanitizer.sanitize(request.getCommand().name(), request.getArgs());
}

@Override
protected String operation(JedisRequest request) {
return request.getCommand().name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import static io.opentelemetry.javaagent.extension.matcher.ClassLoaderMatcher.hasClassesNamed;
import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.instrumentation.jedis.v1_4.JedisClientTracer.tracer;
import static io.opentelemetry.javaagent.instrumentation.jedis.v1_4.JedisInstrumenters.instrumenter;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static net.bytebuddy.matcher.ElementMatchers.is;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand All @@ -22,7 +23,6 @@
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.jedis.v1_4.JedisClientTracer.CommandWithArgs;
import java.util.List;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
Expand Down Expand Up @@ -80,32 +80,31 @@ public static class JedisNoArgsAdvice {
public static void onEnter(
@Advice.This Connection connection,
@Advice.Argument(0) Command command,
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
if (!tracer().shouldStartSpan(parentContext)) {
request = JedisRequest.create(connection, command);
if (!instrumenter().shouldStart(parentContext, request)) {
return;
}

context = tracer().startSpan(parentContext, connection, new CommandWithArgs(command));
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();

if (throwable != null) {
tracer().endExceptionally(context, throwable);
} else {
tracer().end(context);
}
scope.close();
instrumenter().end(context, request, null, throwable);
}
}

Expand All @@ -116,32 +115,31 @@ public static void onEnter(
@Advice.This Connection connection,
@Advice.Argument(0) Command command,
@Advice.Argument(1) byte[][] args,
@Advice.Local("otelJedisRequest") JedisRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
if (!tracer().shouldStartSpan(parentContext)) {
request = JedisRequest.create(connection, command, asList(args));
if (!instrumenter().shouldStart(parentContext, request)) {
return;
}

context = tracer().startSpan(parentContext, connection, new CommandWithArgs(command, args));
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();
if (throwable != null) {
tracer().endExceptionally(context, throwable);
} else {
tracer().end(context);
}
instrumenter().end(context, request, null, throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.db.DbAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.db.DbSpanNameExtractor;

public final class JedisInstrumenters {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.javaagent.jedis-1.4";

private static final Instrumenter<JedisRequest, Void> INSTRUMENTER;

static {
DbAttributesExtractor<JedisRequest> attributesExtractor = new JedisDbAttributesExtractor();
SpanNameExtractor<JedisRequest> spanName = DbSpanNameExtractor.create(attributesExtractor);

INSTRUMENTER =
Instrumenter.<JedisRequest, Void>newBuilder(
GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanName)
.addAttributesExtractor(attributesExtractor)
.addAttributesExtractor(new JedisNetAttributesExtractor())
.newInstrumenter(SpanKindExtractor.alwaysClient());
}

public static Instrumenter<JedisRequest, Void> instrumenter() {
return INSTRUMENTER;
}

private JedisInstrumenters() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

import io.opentelemetry.instrumentation.api.instrumenter.net.NetAttributesExtractor;
import org.checkerframework.checker.nullness.qual.Nullable;

final class JedisNetAttributesExtractor extends NetAttributesExtractor<JedisRequest, Void> {

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

@Override
protected String peerName(JedisRequest request, @Nullable Void response) {
return request.getConnection().getHost();
}

@Override
protected Integer peerPort(JedisRequest request, @Nullable Void response) {
return request.getConnection().getPort();
}

@Override
@Nullable
protected String peerIp(JedisRequest request, @Nullable Void response) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

import static java.util.Collections.emptyList;

import com.google.auto.value.AutoValue;
import java.util.List;
import redis.clients.jedis.Connection;
import redis.clients.jedis.Protocol;

@AutoValue
public abstract class JedisRequest {

public abstract Connection getConnection();

public abstract Protocol.Command getCommand();

public abstract List<byte[]> getArgs();

public String getConnectionString() {
return getConnection().getHost() + ":" + getConnection().getPort();
}

public static JedisRequest create(Connection connection, Protocol.Command command) {
return new AutoValue_JedisRequest(connection, command, emptyList());
}

public static JedisRequest create(
Connection connection, Protocol.Command command, List<byte[]> args) {
return new AutoValue_JedisRequest(connection, command, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class JedisClientTest extends AgentInstrumentationSpecification {
"$SemanticAttributes.DB_SYSTEM.key" "redis"
"$SemanticAttributes.DB_CONNECTION_STRING.key" "localhost:$port"
"$SemanticAttributes.DB_STATEMENT.key" "SET foo ?"
"$SemanticAttributes.NET_PEER_IP.key" "127.0.0.1"
"$SemanticAttributes.DB_OPERATION.key" "SET"
"$SemanticAttributes.NET_PEER_NAME.key" "localhost"
"$SemanticAttributes.NET_PEER_PORT.key" port
}
Expand All @@ -77,7 +77,7 @@ class JedisClientTest extends AgentInstrumentationSpecification {
"$SemanticAttributes.DB_SYSTEM.key" "redis"
"$SemanticAttributes.DB_CONNECTION_STRING.key" "localhost:$port"
"$SemanticAttributes.DB_STATEMENT.key" "SET foo ?"
"$SemanticAttributes.NET_PEER_IP.key" "127.0.0.1"
"$SemanticAttributes.DB_OPERATION.key" "SET"
"$SemanticAttributes.NET_PEER_NAME.key" "localhost"
"$SemanticAttributes.NET_PEER_PORT.key" port
}
Expand All @@ -91,7 +91,7 @@ class JedisClientTest extends AgentInstrumentationSpecification {
"$SemanticAttributes.DB_SYSTEM.key" "redis"
"$SemanticAttributes.DB_CONNECTION_STRING.key" "localhost:$port"
"$SemanticAttributes.DB_STATEMENT.key" "GET foo"
"$SemanticAttributes.NET_PEER_IP.key" "127.0.0.1"
"$SemanticAttributes.DB_OPERATION.key" "GET"
"$SemanticAttributes.NET_PEER_NAME.key" "localhost"
"$SemanticAttributes.NET_PEER_PORT.key" port
}
Expand All @@ -117,7 +117,7 @@ class JedisClientTest extends AgentInstrumentationSpecification {
"$SemanticAttributes.DB_SYSTEM.key" "redis"
"$SemanticAttributes.DB_CONNECTION_STRING.key" "localhost:$port"
"$SemanticAttributes.DB_STATEMENT.key" "SET foo ?"
"$SemanticAttributes.NET_PEER_IP.key" "127.0.0.1"
"$SemanticAttributes.DB_OPERATION.key" "SET"
"$SemanticAttributes.NET_PEER_NAME.key" "localhost"
"$SemanticAttributes.NET_PEER_PORT.key" port
}
Expand All @@ -131,7 +131,7 @@ class JedisClientTest extends AgentInstrumentationSpecification {
"$SemanticAttributes.DB_SYSTEM.key" "redis"
"$SemanticAttributes.DB_CONNECTION_STRING.key" "localhost:$port"
"$SemanticAttributes.DB_STATEMENT.key" "RANDOMKEY"
"$SemanticAttributes.NET_PEER_IP.key" "127.0.0.1"
"$SemanticAttributes.DB_OPERATION.key" "RANDOMKEY"
"$SemanticAttributes.NET_PEER_NAME.key" "localhost"
"$SemanticAttributes.NET_PEER_PORT.key" port
}
Expand Down

0 comments on commit b4cab9b

Please sign in to comment.