Skip to content

Commit

Permalink
Cache couchbase operation attribute values per method (#3819)
Browse files Browse the repository at this point in the history
* Cache couchbase operation attribute values per method

* avoid using reflection

* spotless
  • Loading branch information
Mateusz Rzeszutek authored Aug 11, 2021
1 parent d9080a7 commit 3ea22bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import java.lang.reflect.Method;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -54,14 +53,15 @@ public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callD

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void subscribeResult(
@Advice.Origin Method method,
@Advice.Origin("#t") Class<?> declaringClass,
@Advice.Origin("#m") String methodName,
@Advice.FieldValue("bucket") String bucket,
@Advice.Return(readOnly = false) Observable<?> result,
@Advice.Local("otelCallDepth") CallDepth callDepth) {
if (callDepth.decrementAndGet() > 0) {
return;
}
CouchbaseRequest request = CouchbaseRequest.create(bucket, method);
CouchbaseRequest request = CouchbaseRequest.create(bucket, declaringClass, methodName);
result = Observable.create(new TracedOnSubscribe<>(result, instrumenter(), request));
}
}
Expand All @@ -77,7 +77,8 @@ public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callD

@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void subscribeResult(
@Advice.Origin Method method,
@Advice.Origin("#t") Class<?> declaringClass,
@Advice.Origin("#m") String methodName,
@Advice.FieldValue("bucket") String bucket,
@Advice.Argument(value = 0, optional = true) Object query,
@Advice.Return(readOnly = false) Observable<?> result,
Expand All @@ -88,7 +89,7 @@ public static void subscribeResult(

CouchbaseRequest request =
query == null
? CouchbaseRequest.create(bucket, method)
? CouchbaseRequest.create(bucket, declaringClass, methodName)
: CouchbaseRequest.create(bucket, query);
result = Observable.create(new TracedOnSubscribe<>(result, instrumenter(), request));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.CallDepth;
import java.lang.reflect.Method;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -51,14 +50,15 @@ public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callD

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
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.Local("otelCallDepth") CallDepth callDepth) {
if (callDepth.decrementAndGet() > 0) {
return;
}

CouchbaseRequest request = CouchbaseRequest.create(null, method);
CouchbaseRequest request = CouchbaseRequest.create(null, declaringClass, methodName);
result = Observable.create(new TracedOnSubscribe<>(result, instrumenter(), request));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@

import com.google.auto.value.AutoValue;
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;

@AutoValue
public abstract class CouchbaseRequest {

public static CouchbaseRequest create(@Nullable String bucket, Method method) {
Class<?> declaringClass = method.getDeclaringClass();
String className =
declaringClass.getSimpleName().replace("CouchbaseAsync", "").replace("DefaultAsync", "");
String operation = className + "." + method.getName();

private static final ClassValue<Map<String, String>> methodOperationNames =
new ClassValue<Map<String, String>>() {
@Override
protected Map<String, String> computeValue(Class<?> type) {
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);
}

Expand All @@ -29,6 +38,12 @@ public static CouchbaseRequest create(@Nullable String bucket, Object query) {
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
public abstract String bucket();

Expand Down

0 comments on commit 3ea22bb

Please sign in to comment.