-
Notifications
You must be signed in to change notification settings - Fork 1k
add metric annotation instrumentation #11354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 21 commits
21cbf96
ff05043
f5d339f
22d6c43
aa3b47c
36a9ab5
d954ebe
9eac550
37733c1
6cdfff9
771cc58
42aa73b
c9ea9a0
43855a5
a844fa3
2c2c3d5
7fefa91
3e3b100
de28d29
99c2937
8a01999
172f2ac
6ba7771
0010367
430e690
f6887ae
630eeac
372418a
3484ebf
4dbdc28
5eb706d
06caf31
c2fdbb4
6b6e3c4
45e3c81
bb4cb98
7652fdb
f9a2a44
e301a9b
e2b43a1
f576d0e
82d885c
292aec2
589e23a
e97b25e
3ea13f7
90c87bf
aaf4b3a
bd31e37
efbf2cd
d5a7365
f92cd3e
3b10ca2
771fe71
874ad04
298704a
5d76916
0c12705
cfaeb91
f33af44
166b445
5dbf740
2bbca47
1e1f594
cff297f
4033cb5
3b946f0
5d33384
00b5577
e6f7a10
a29759a
1e16d9d
6939ed4
603c597
8673767
5468f46
79c49f3
19e8c9c
077a233
72d2732
1b8459d
b72ab8c
f4ea230
d719e7e
917bc58
6da511d
a07c2fc
a1d6a4b
b0ebdad
f7da3ef
711fa32
bb36b37
b28a200
9d3b99f
31282d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| plugins { | ||
| id("otel.java-conventions") | ||
| id("otel.japicmp-conventions") | ||
| id("otel.publish-conventions") | ||
|
|
||
| id("otel.animalsniffer-conventions") | ||
| } | ||
|
|
||
| group = "io.opentelemetry.instrumentation" | ||
|
|
||
| dependencies { | ||
| api("io.opentelemetry:opentelemetry-api") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.annotations.incubator; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * This annotation creates a {@link io.opentelemetry.api.metrics.LongCounter Counter} instrument | ||
| * recording the number of invocations of the annotated method or constructor. | ||
| * | ||
| * <p>By default, the Counter instrument will have the following attributes: | ||
| * | ||
| * <ul> | ||
| * <li><b>code.namespace:</b> The fully qualified name of the class whose method is invoked. | ||
| * <li><b>code.function:</b> The name of the annotated method. | ||
| * <li><b>exception.type:</b> This is only present if an Exception is thrown, and contains the | ||
| * {@link Class#getName name} of the Exception class. | ||
| * </ul> | ||
| * | ||
| * <p>Application developers can use this annotation to signal OpenTelemetry auto-instrumentation | ||
| * that the Counter instrument should be created. | ||
Duncan-tree-zhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * <p>If you are a library developer, then probably you should NOT use this annotation, because it | ||
| * is non-functional without the OpenTelemetry auto-instrumentation agent, or some other annotation | ||
| * processor. | ||
| */ | ||
| @Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface Counted { | ||
|
|
||
| /** | ||
| * Name of the Counter instrument. | ||
Duncan-tree-zhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * <p>The name should follow the instrument naming rule: <a | ||
Duncan-tree-zhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-naming-rule">https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-naming-rule</a> | ||
| */ | ||
| String value(); | ||
|
|
||
| /** | ||
| * Description of the instrument. | ||
| * | ||
| * <p>Description strings should follow the instrument description rules: <a | ||
Duncan-tree-zhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description">https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description</a> | ||
| * | ||
| * <p>This property would not take effect if the value is not specified. | ||
Duncan-tree-zhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| String description() default ""; | ||
|
|
||
| /** | ||
| * Unit of the instrument. | ||
Duncan-tree-zhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * <p>Unit strings should follow the instrument unit rules: <a | ||
| * href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit">https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit</a> | ||
| * | ||
| * <p>This property would not take effect if the value is not specified. | ||
Duncan-tree-zhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| String unit() default "{invocation}"; | ||
|
|
||
| /** | ||
| * Attribute name for the return value. | ||
| * | ||
| * <p>The name of the attribute for the return value of the method call. {@link Object#toString()} | ||
| * may be called on the return value to convert it to a String. | ||
| * | ||
| * <p>By default, the instrument will not record an attribute with the return value. | ||
| * | ||
| * <p>Warning: be careful to fill it because it might cause an explosion of the cardinality on | ||
| * your metric | ||
| */ | ||
| String returnValueAttribute() default ""; | ||
laurit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.annotations.incubator; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * This annotation marks that a parameter of a method or constructor annotated with {@link Timed} or | ||
| * {@link Counted} should be added as an attribute to the instrument. | ||
| * | ||
| * <p>Application developers can use this annotation to signal OpenTelemetry auto-instrumentation | ||
| * that the attribute should be created. | ||
| * | ||
| * <p>If you are a library developer, then probably you should NOT use this annotation, because it | ||
| * is non-functional without the OpenTelemetry auto-instrumentation agent, or some other annotation | ||
| * processor. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we doc the attribute type that will be used for the metric attribute?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The attribute type is chosen based on the same logic as with |
||
| */ | ||
| @Target(ElementType.PARAMETER) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface MetricAttribute { | ||
|
||
|
|
||
| /** | ||
| * Optional name of the attribute. | ||
| * | ||
| * <p>If not specified and the code is compiled using the `{@code -parameters}` argument to | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * `javac`, the parameter name will be used instead. If the parameter name is not available, e.g., | ||
| * because the code was not compiled with that flag, the attribute will be ignored. | ||
| * | ||
| * <p>Warning: be careful to fill it because it might cause an explosion of the cardinality on | ||
| * your metric | ||
laurit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| String value() default ""; | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.annotations.incubator; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Repeatable; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * This annotation allows for adding attributes to the metrics recorded using {@link Timed} and | ||
| * {@link Counted} annotations. | ||
| * | ||
| * <p>Application developers can use this annotation to signal OpenTelemetry auto-instrumentation | ||
| * that the attribute should be created. | ||
| * | ||
| * <p>If you are a library developer, then probably you should NOT use this annotation, because it | ||
| * is non-functional without the OpenTelemetry auto-instrumentation agent, or some other annotation | ||
| * processor. | ||
| */ | ||
| @Target(ElementType.METHOD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Repeatable(StaticMetricAttributes.class) | ||
| public @interface StaticMetricAttribute { | ||
|
||
|
|
||
| /** Name of the attribute. */ | ||
| String name(); | ||
|
|
||
| /** Value of the attribute. */ | ||
| String value(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.annotations.incubator; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * This annotation allows for adding attributes to the metrics recorded using {@link Timed} and | ||
| * {@link Counted} annotations. | ||
| * | ||
| * <p>Application developers can use this annotation to signal OpenTelemetry auto-instrumentation | ||
| * that the attribute should be created. | ||
| * | ||
| * <p>If you are a library developer, then probably you should NOT use this annotation, because it | ||
| * is non-functional without the OpenTelemetry auto-instrumentation agent, or some other annotation | ||
| * processor. | ||
| */ | ||
| @Target(ElementType.METHOD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface StaticMetricAttributes { | ||
|
|
||
| /** Array of {@link StaticMetricAttribute} annotations describing the added attributes. */ | ||
| StaticMetricAttribute[] value(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.annotations.incubator; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * This annotation creates a {@link io.opentelemetry.api.metrics.DoubleHistogram Histogram} | ||
| * instrument observing the duration of invocations of the annotated method or constructor. | ||
| * | ||
| * <p>By default, the Histogram instrument will have the following attributes: | ||
| * | ||
| * <ul> | ||
| * <li><b>code.namespace:</b> The fully qualified name of the class whose method is invoked. | ||
| * <li><b>code.function:</b> The name of the annotated method. | ||
| * <li><b>exception.type:</b> This is only present if an Exception is thrown, and contains the | ||
| * {@link Class#getName name} of the Exception class. | ||
| * </ul> | ||
| * | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you document what the default is for buckets? |
||
| * <p>Application developers can use this annotation to signal OpenTelemetry auto-instrumentation | ||
| * that the Histogram instrument should be created. | ||
| * | ||
| * <p>If you are a library developer, then probably you should NOT use this annotation, because it | ||
| * is non-functional without the OpenTelemetry auto-instrumentation agent, or some other annotation | ||
| * processor. | ||
| */ | ||
| @Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface Timed { | ||
|
|
||
| /** | ||
| * Name of the Histogram instrument. | ||
| * | ||
| * <p>The name should follow the instrument naming rule: <a | ||
| * href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-naming-rule">https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-naming-rule</a> | ||
| */ | ||
| String value(); | ||
|
|
||
| /** | ||
| * Description for the instrument. | ||
| * | ||
| * <p>Description strings should follow the instrument description rules: <a | ||
| * href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description">https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description</a> | ||
| */ | ||
| String description() default ""; | ||
|
|
||
| /** | ||
| * The unit for the instrument. | ||
| * | ||
| * <p>Default is seconds. | ||
| */ | ||
| TimeUnit unit() default TimeUnit.SECONDS; | ||
|
|
||
| /** | ||
| * Attribute name for the return value. | ||
| * | ||
| * <p>The name of the attribute for the return value of the method call. {@link Object#toString()} | ||
| * may be called on the return value to convert it to a String. | ||
| * | ||
| * <p>By default, the instrument will not record an attribute with the return value. | ||
| * | ||
| * <p>Warning: be careful to fill it because it might cause an explosion of the cardinality on | ||
| * your metric | ||
| */ | ||
| String returnValueAttribute() default ""; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.annotations.incubator; | ||
|
|
||
| public class CountedUsageExamples { | ||
|
|
||
| @Counted("customizedName") | ||
| public void method() {} | ||
|
|
||
| @Counted("methodWithAttributes") | ||
| public void attributes( | ||
| @MetricAttribute String attribute1, @MetricAttribute("attribute2") long attribute2) {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.annotations.incubator; | ||
|
|
||
| public class TimedUsageExamples { | ||
|
|
||
| @Timed("customizedName") | ||
| public void method() {} | ||
|
|
||
| @Timed("methodWithAttributes") | ||
| public void attributes( | ||
| @MetricAttribute String attribute1, @MetricAttribute("attribute2") long attribute2) {} | ||
| } |
| 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.instrumentation.api.annotation.support; | ||
|
|
||
| import io.opentelemetry.api.common.AttributesBuilder; | ||
| import java.lang.reflect.Method; | ||
| import java.util.function.BiConsumer; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** Helper class for binding method parameters and return value to attributes. */ | ||
| public final class MethodBinder { | ||
|
|
||
| /** Create binding for method return value. */ | ||
| @Nullable | ||
| public static BiConsumer<AttributesBuilder, Object> bindReturnValue( | ||
| Method method, String attributeName) { | ||
| Class<?> returnType = method.getReturnType(); | ||
| if (returnType == void.class) { | ||
| return null; | ||
| } | ||
| AttributeBinding binding = AttributeBindingFactory.createBinding(attributeName, returnType); | ||
| return binding::apply; | ||
| } | ||
|
|
||
| /** Create binding for method parameters. */ | ||
| @Nullable | ||
| public static BiConsumer<AttributesBuilder, Object[]> bindParameters( | ||
| Method method, ParameterAttributeNamesExtractor parameterAttributeNamesExtractor) { | ||
| AttributeBindings bindings = AttributeBindings.bind(method, parameterAttributeNamesExtractor); | ||
| if (bindings.isEmpty()) { | ||
| return null; | ||
| } | ||
| return bindings::apply; | ||
| } | ||
|
|
||
| private MethodBinder() {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.annotation.support.async; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** Callback that is called when async computation completes. */ | ||
| public interface AsyncOperationEndHandler<REQUEST, RESPONSE> { | ||
|
||
| void handle( | ||
| Context context, REQUEST request, @Nullable RESPONSE response, @Nullable Throwable error); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm really looking forward for this to be merged. However I've noticed that
code.namespaceandcode.functionhave been deprecated in the semantic conventions and has been replaced bycode.function.namein the which is the fully qualified name. Should this be updated in this PR as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for pointing out. the
code.namespaceandcode.functionare already replaced bycode.function.name. check MetricsAnnotationHelper.java line 38.