Cache couchbase operation attribute values per method (#3819)

* Cache couchbase operation attribute values per method

* avoid using reflection

* spotless
This commit is contained in:
Mateusz Rzeszutek 2021-08-11 16:54:11 +02:00 committed by GitHub
parent d9080a745b
commit 3ea22bb3ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 14 deletions

View File

@ -18,7 +18,6 @@ import io.opentelemetry.instrumentation.rxjava.TracedOnSubscribe;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import java.lang.reflect.Method;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.matcher.ElementMatcher;
@ -54,14 +53,15 @@ public class CouchbaseBucketInstrumentation implements TypeInstrumentation {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void subscribeResult( public static void subscribeResult(
@Advice.Origin Method method, @Advice.Origin("#t") Class<?> declaringClass,
@Advice.Origin("#m") String methodName,
@Advice.FieldValue("bucket") String bucket, @Advice.FieldValue("bucket") String bucket,
@Advice.Return(readOnly = false) Observable<?> result, @Advice.Return(readOnly = false) Observable<?> result,
@Advice.Local("otelCallDepth") CallDepth callDepth) { @Advice.Local("otelCallDepth") CallDepth callDepth) {
if (callDepth.decrementAndGet() > 0) { if (callDepth.decrementAndGet() > 0) {
return; return;
} }
CouchbaseRequest request = CouchbaseRequest.create(bucket, method); CouchbaseRequest request = CouchbaseRequest.create(bucket, declaringClass, methodName);
result = Observable.create(new TracedOnSubscribe<>(result, instrumenter(), request)); result = Observable.create(new TracedOnSubscribe<>(result, instrumenter(), request));
} }
} }
@ -77,7 +77,8 @@ public class CouchbaseBucketInstrumentation implements TypeInstrumentation {
@Advice.OnMethodExit(onThrowable = Throwable.class) @Advice.OnMethodExit(onThrowable = Throwable.class)
public static void subscribeResult( public static void subscribeResult(
@Advice.Origin Method method, @Advice.Origin("#t") Class<?> declaringClass,
@Advice.Origin("#m") String methodName,
@Advice.FieldValue("bucket") String bucket, @Advice.FieldValue("bucket") String bucket,
@Advice.Argument(value = 0, optional = true) Object query, @Advice.Argument(value = 0, optional = true) Object query,
@Advice.Return(readOnly = false) Observable<?> result, @Advice.Return(readOnly = false) Observable<?> result,
@ -88,7 +89,7 @@ public class CouchbaseBucketInstrumentation implements TypeInstrumentation {
CouchbaseRequest request = CouchbaseRequest request =
query == null query == null
? CouchbaseRequest.create(bucket, method) ? CouchbaseRequest.create(bucket, declaringClass, methodName)
: CouchbaseRequest.create(bucket, query); : CouchbaseRequest.create(bucket, query);
result = Observable.create(new TracedOnSubscribe<>(result, instrumenter(), request)); result = Observable.create(new TracedOnSubscribe<>(result, instrumenter(), request));
} }

View File

@ -18,7 +18,6 @@ import io.opentelemetry.instrumentation.rxjava.TracedOnSubscribe;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import java.lang.reflect.Method;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.matcher.ElementMatcher;
@ -51,14 +50,15 @@ public class CouchbaseClusterInstrumentation implements TypeInstrumentation {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void subscribeResult( public static void subscribeResult(
@Advice.Origin Method method, @Advice.Origin("#t") Class<?> declaringClass,
@Advice.Origin("#m") String methodName,
@Advice.Return(readOnly = false) Observable<?> result, @Advice.Return(readOnly = false) Observable<?> result,
@Advice.Local("otelCallDepth") CallDepth callDepth) { @Advice.Local("otelCallDepth") CallDepth callDepth) {
if (callDepth.decrementAndGet() > 0) { if (callDepth.decrementAndGet() > 0) {
return; return;
} }
CouchbaseRequest request = CouchbaseRequest.create(null, method); CouchbaseRequest request = CouchbaseRequest.create(null, declaringClass, methodName);
result = Observable.create(new TracedOnSubscribe<>(result, instrumenter(), request)); result = Observable.create(new TracedOnSubscribe<>(result, instrumenter(), request));
} }
} }

View File

@ -7,18 +7,27 @@ package io.opentelemetry.javaagent.instrumentation.couchbase.v2_0;
import com.google.auto.value.AutoValue; import com.google.auto.value.AutoValue;
import io.opentelemetry.instrumentation.api.db.SqlStatementInfo; import io.opentelemetry.instrumentation.api.db.SqlStatementInfo;
import java.lang.reflect.Method; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@AutoValue @AutoValue
public abstract class CouchbaseRequest { public abstract class CouchbaseRequest {
public static CouchbaseRequest create(@Nullable String bucket, Method method) { private static final ClassValue<Map<String, String>> methodOperationNames =
Class<?> declaringClass = method.getDeclaringClass(); new ClassValue<Map<String, String>>() {
String className = @Override
declaringClass.getSimpleName().replace("CouchbaseAsync", "").replace("DefaultAsync", ""); protected Map<String, String> computeValue(Class<?> type) {
String operation = className + "." + method.getName(); return new ConcurrentHashMap<>();
}
};
public static CouchbaseRequest create(
@Nullable String bucket, Class<?> declaringClass, String methodName) {
String operation =
methodOperationNames
.get(declaringClass)
.computeIfAbsent(methodName, m -> computeOperation(declaringClass, m));
return new AutoValue_CouchbaseRequest(bucket, null, operation, true); return new AutoValue_CouchbaseRequest(bucket, null, operation, true);
} }
@ -29,6 +38,12 @@ public abstract class CouchbaseRequest {
bucket, statement.getFullStatement(), statement.getOperation(), false); bucket, statement.getFullStatement(), statement.getOperation(), false);
} }
private static String computeOperation(Class<?> declaringClass, String methodName) {
String className =
declaringClass.getSimpleName().replace("CouchbaseAsync", "").replace("DefaultAsync", "");
return className + "." + methodName;
}
@Nullable @Nullable
public abstract String bucket(); public abstract String bucket();