-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add metric annotation instrumentation
- Loading branch information
1 parent
8b4e6d2
commit f7dd6b9
Showing
11 changed files
with
835 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...elemetry/javaagent/instrumentation/instrumentationannotations/CountedInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.instrumentationannotations; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.instrumentationannotations.KotlinCoroutineUtil.isKotlinSuspendMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.hasParameters; | ||
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.not; | ||
import static net.bytebuddy.matcher.ElementMatchers.whereAny; | ||
|
||
import com.google.common.base.Stopwatch; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import java.lang.reflect.Method; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.annotation.AnnotationSource; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.implementation.bytecode.assign.Assigner; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class CountedInstrumentation implements TypeInstrumentation { | ||
|
||
private final ElementMatcher.Junction<AnnotationSource> annotatedMethodMatcher; | ||
private final ElementMatcher.Junction<MethodDescription> annotatedParametersMatcher; | ||
// this matcher matches all methods that should be excluded from transformation | ||
private final ElementMatcher.Junction<MethodDescription> excludedMethodsMatcher; | ||
|
||
CountedInstrumentation() { | ||
annotatedMethodMatcher = | ||
isAnnotatedWith(named("application.io.opentelemetry.instrumentation.annotations.Counted")); | ||
annotatedParametersMatcher = | ||
hasParameters( | ||
whereAny( | ||
isAnnotatedWith( | ||
named( | ||
"application.io.opentelemetry.instrumentation.annotations.MetricAttribute")))); | ||
// exclude all kotlin suspend methods, these are handle in kotlinx-coroutines instrumentation | ||
excludedMethodsMatcher = | ||
AnnotationExcludedMethods.configureExcludedMethods().or(isKotlinSuspendMethod()); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return declaresMethod(annotatedMethodMatcher); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
ElementMatcher.Junction<MethodDescription> countedMethods = | ||
annotatedMethodMatcher.and(not(excludedMethodsMatcher)); | ||
|
||
ElementMatcher.Junction<MethodDescription> timedMethodsWithParameters = | ||
countedMethods.and(annotatedParametersMatcher); | ||
|
||
ElementMatcher.Junction<MethodDescription> timedMethodsWithoutParameters = | ||
countedMethods.and(not(annotatedParametersMatcher)); | ||
|
||
transformer.applyAdviceToMethod( | ||
timedMethodsWithoutParameters, CountedInstrumentation.class.getName() + "$CountedAdvice"); | ||
|
||
// Only apply advice for tracing parameters as attributes if any of the parameters are annotated | ||
// with @MetricsAttribute to avoid unnecessarily copying the arguments into an array. | ||
transformer.applyAdviceToMethod( | ||
timedMethodsWithParameters, | ||
CountedInstrumentation.class.getName() + "$CountedAttributesAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class CountedAttributesAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Origin Method originMethod, | ||
@Advice.Local("otelMethod") Method method, | ||
@Advice.AllArguments(typing = Assigner.Typing.DYNAMIC) Object[] args, | ||
@Advice.Local("otelRequest") MethodRequest request, | ||
@Advice.Local("stopwatch") Stopwatch stopwatch) { | ||
|
||
// Every usage of @Advice.Origin Method is replaced with a call to Class.getMethod, copy it | ||
// to local variable so that there would be only one call to Class.getMethod. | ||
method = originMethod; | ||
request = new MethodRequest(method, args); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Local("otelMethod") Method method, | ||
@Advice.Local("otelRequest") MethodRequest request, | ||
@Advice.Local("stopwatch") Stopwatch stopwatch, | ||
@Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue, | ||
@Advice.Thrown Throwable throwable) { | ||
AnnotationSingletons.recordCountWithAttributes(request); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class CountedAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Origin Method originMethod, | ||
@Advice.Local("otelMethod") Method method, | ||
@Advice.Local("stopwatch") Stopwatch stopwatch) { | ||
// Every usage of @Advice.Origin Method is replaced with a call to Class.getMethod, copy it | ||
// to local variable so that there would be only one call to Class.getMethod. | ||
method = originMethod; | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void stopSpan( | ||
@Advice.Local("otelMethod") Method method, | ||
@Advice.Local("stopwatch") Stopwatch stopwatch, | ||
@Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue, | ||
@Advice.Thrown Throwable throwable) { | ||
AnnotationSingletons.recordCount(method); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...y/javaagent/instrumentation/instrumentationannotations/CountedInstrumentationsModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.instrumentationannotations; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static java.util.Arrays.asList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.List; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class CountedInstrumentationsModule extends InstrumentationModule { | ||
|
||
public CountedInstrumentationsModule() { | ||
super("opentelemetry-instrumentation-annotations", "counted"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
return hasClassesNamed("application.io.opentelemetry.instrumentation.annotations.Counted"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return asList(new CountedInstrumentation()); | ||
} | ||
|
||
@Override | ||
public int order() { | ||
// Run first to ensure other automatic instrumentation is added after and therefore is executed | ||
// earlier in the instrumented method and create the span to attach attributes to. | ||
return -1000; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...try/javaagent/instrumentation/instrumentationannotations/TimedInstrumentationsModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.instrumentationannotations; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static java.util.Arrays.asList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.List; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class TimedInstrumentationsModule extends InstrumentationModule { | ||
|
||
public TimedInstrumentationsModule() { | ||
super("opentelemetry-instrumentation-annotations", "timed"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
return hasClassesNamed("application.io.opentelemetry.instrumentation.annotations.Timed"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return asList(new TimedInstrumentation()); | ||
} | ||
|
||
@Override | ||
public int order() { | ||
// Run first to ensure other automatic instrumentation is added after and therefore is executed | ||
// earlier in the instrumented method and create the span to attach attributes to. | ||
return -1000; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...tations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/CountedExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.opentelemetry.test.annotation; | ||
|
||
import io.opentelemetry.javaagent.instrumentation.instrumentationannotations.annotations.Counted; | ||
|
||
public class CountedExample { | ||
|
||
@Counted | ||
public String defaultExample() { | ||
return "defualt example"; | ||
} | ||
|
||
@Counted("another.name") | ||
public String exampleWithAnotherName() { | ||
return "example with another name."; | ||
} | ||
|
||
} |
Oops, something went wrong.