Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sfriberg committed Feb 13, 2023
1 parent 1144ffc commit 604b68d
Show file tree
Hide file tree
Showing 16 changed files with 1,281 additions and 1,005 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
package io.opentelemetry.instrumentation.api.annotation.support;

import io.opentelemetry.api.common.AttributeKey;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.AbstractList;
import java.util.Arrays;
Expand Down Expand Up @@ -340,42 +338,4 @@ private static AttributeBinding defaultBinding(String name) {
AttributeKey<String> key = AttributeKey.stringKey(name);
return (setter, arg) -> setter.put(key, arg.toString());
}

/**
* Creates a binding of the parameters of the traced method to span attributes.
*
* @param method the traced method
* @return the bindings of the parameters
*/
static AttributeBindings bind(
Method method, ParameterAttributeNamesExtractor parameterAttributeNamesExtractor) {
AttributeBindings bindings = EmptyAttributeBindings.INSTANCE;

Parameter[] parameters = method.getParameters();
if (parameters.length == 0) {
return bindings;
}

String[] attributeNames = parameterAttributeNamesExtractor.extract(method, parameters);
if (attributeNames == null || attributeNames.length != parameters.length) {
return bindings;
}

for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
String attributeName = attributeNames[i];
if (attributeName == null || attributeName.isEmpty()) {
continue;
}

bindings =
new CombinedAttributeBindings(
bindings,
i,
AttributeBindingFactory.createBinding(
attributeName, parameter.getParameterizedType()));
}

return bindings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package io.opentelemetry.instrumentation.api.annotation.support;

import io.opentelemetry.api.common.AttributesBuilder;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/** Represents the bindings of method parameters to attributes of a traced method. */
interface AttributeBindings {
Expand All @@ -24,4 +26,42 @@ interface AttributeBindings {
* @param args the method arguments
*/
void apply(AttributesBuilder target, Object[] args);

/**
* Creates a binding of the parameters of the traced method to span attributes.
*
* @param method the traced method
* @return the bindings of the parameters
*/
static AttributeBindings bind(
Method method, ParameterAttributeNamesExtractor parameterAttributeNamesExtractor) {
AttributeBindings bindings = EmptyAttributeBindings.INSTANCE;

Parameter[] parameters = method.getParameters();
if (parameters.length == 0) {
return bindings;
}

String[] attributeNames = parameterAttributeNamesExtractor.extract(method, parameters);
if (attributeNames == null || attributeNames.length != parameters.length) {
return bindings;
}

for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
String attributeName = attributeNames[i];
if (attributeName == null || attributeName.isEmpty()) {
continue;
}

bindings =
new CombinedAttributeBindings(
bindings,
i,
AttributeBindingFactory.createBinding(
attributeName, parameter.getParameterizedType()));
}

return bindings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class MethodSpanAttributesExtractor<REQUEST, RESPONSE>
private final Cache<Method, AttributeBindings> cache;
private final ParameterAttributeNamesExtractor parameterAttributeNamesExtractor;

public static <REQUEST, RESPONSE> MethodSpanAttributesExtractor<REQUEST, RESPONSE> newInstance(
public static <REQUEST, RESPONSE> MethodSpanAttributesExtractor<REQUEST, RESPONSE> create(
MethodExtractor<REQUEST> methodExtractor,
ParameterAttributeNamesExtractor parameterAttributeNamesExtractor,
MethodArgumentsExtractor<REQUEST> methodArgumentsExtractor) {
Expand Down Expand Up @@ -49,8 +49,7 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
Method method = methodExtractor.extract(request);
AttributeBindings bindings =
cache.computeIfAbsent(
method,
(Method m) -> AttributeBindingFactory.bind(m, parameterAttributeNamesExtractor));
method, (Method m) -> AttributeBindings.bind(m, parameterAttributeNamesExtractor));
if (!bindings.isEmpty()) {
Object[] args = methodArgumentsExtractor.extract(request);
bindings.apply(attributes, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class SpanAttributesExtractor {
private final Cache<Method, AttributeBindings> cache;
private final ParameterAttributeNamesExtractor parameterAttributeNamesExtractor;

public static SpanAttributesExtractor newInstance(
public static SpanAttributesExtractor create(
ParameterAttributeNamesExtractor parameterAttributeNamesExtractor) {
return new SpanAttributesExtractor(parameterAttributeNamesExtractor, new MethodCache<>());
}
Expand All @@ -28,12 +28,11 @@ public static SpanAttributesExtractor newInstance(
this.cache = cache;
}

public Attributes getAttributes(Method method, Object[] args) {
public Attributes extract(Method method, Object[] args) {
AttributesBuilder attributes = Attributes.builder();
AttributeBindings bindings =
cache.computeIfAbsent(
method,
(Method m) -> AttributeBindingFactory.bind(m, parameterAttributeNamesExtractor));
method, (Method m) -> AttributeBindings.bind(m, parameterAttributeNamesExtractor));
if (!bindings.isEmpty()) {
bindings.apply(attributes, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ dependencies {
// see the comment in opentelemetry-api-1.0.gradle for more details
compileOnly(project(":opentelemetry-ext-annotations-shaded-for-instrumenting", configuration = "shadow"))

// Used by byte-buddy but not brought in as a transitive dependency.
compileOnly("com.google.code.findbugs:annotations")
testCompileOnly("com.google.code.findbugs:annotations")

testImplementation("io.opentelemetry:opentelemetry-extension-annotations")
testImplementation(project(":instrumentation-annotations-support"))
testImplementation("net.bytebuddy:byte-buddy")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static Instrumenter<MethodRequest, Object> createInstrumenterWithAttribu
.addAttributesExtractor(
CodeAttributesExtractor.create(MethodRequestCodeAttributesGetter.INSTANCE))
.addAttributesExtractor(
MethodSpanAttributesExtractor.newInstance(
MethodSpanAttributesExtractor.create(
MethodRequest::method,
WithSpanParameterAttributeNamesExtractor.INSTANCE,
MethodRequest::args))
Expand Down
Loading

0 comments on commit 604b68d

Please sign in to comment.