-
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 all 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
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,20 @@ | ||
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") | ||
} | ||
|
||
tasks.test { | ||
// This module does not have tests, but has example classes in the test directory. Gradle 9 fails | ||
// the build when there are source files in the test directory but no tests to run so we disable | ||
// the test task. | ||
enabled = false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 metric. | ||
* | ||
* <p>`{@link Object#toString()}` will be called on the parameter value to convert it to a String. | ||
* | ||
* <p>Application developers can use this annotation to signal OpenTelemetry auto-instrumentation | ||
* that the attribute should be captured. | ||
* | ||
* <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. | ||
* | ||
* <p>Warning: be careful using this because it might cause an explosion of the cardinality on your | ||
* metric. | ||
*/ | ||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Attribute { | ||
|
||
/** | ||
* Optional name of the attribute. | ||
* | ||
* <p>If not specified and the code is compiled using the `{@code -parameters}` argument to | ||
* `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. | ||
*/ | ||
String name() default ""; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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. | ||
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. I'm really looking forward for this to be merged. However I've noticed that |
||
* <li><b>code.function:</b> The name of the annotated method. | ||
* <li><b>error.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 a Counter metric should be captured. | ||
* | ||
* <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 metric. | ||
* | ||
* <p>The name should follow the metric naming rules: <a | ||
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-name-syntax">https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-name-syntax</a> | ||
*/ | ||
String name(); | ||
|
||
/** | ||
* Description of the metric. | ||
* | ||
* <p>Description strings should follow the metric 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> | ||
* | ||
* <p>This property will not take effect if the value is not specified. | ||
*/ | ||
String description() default ""; | ||
|
||
/** | ||
* Unit of the metric. | ||
* | ||
* <p>Unit strings should follow the metric 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 will not take effect if the value is not specified. | ||
*/ | ||
String unit() default "{invocation}"; | ||
} |
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 allows for adding the method return value as an attribute to metrics recorded | ||
* using the {@link Timed} and {@link Counted} annotations. | ||
* | ||
* <p>{@link Object#toString()} will be called on the return value to convert it to a String. | ||
* | ||
* <p>Application developers can use this annotation to signal OpenTelemetry auto-instrumentation | ||
* that the attribute should be captured. | ||
* | ||
* <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. | ||
* | ||
* <p>Warning: be careful using this because it might cause an explosion of the cardinality on your | ||
* metric. | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ReturnValueAttribute { | ||
|
||
/** | ||
* Attribute name for the return value. | ||
* | ||
* <p>The name of the attribute for the return value of the method call. | ||
*/ | ||
String value(); | ||
} |
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 static 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 static attributes should be captured. | ||
* | ||
* <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(StaticAttributes.class) | ||
public @interface StaticAttribute { | ||
|
||
/** Name of the static attribute. */ | ||
String name(); | ||
|
||
/** Value of the static 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 static 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 static attributes should be captured. | ||
* | ||
* <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 StaticAttributes { | ||
|
||
/** Array of {@link StaticAttribute} annotations describing the attributes to capture. */ | ||
StaticAttribute[] value(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.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>error.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 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 metric. | ||
* | ||
* <p>The name should follow the metric naming rules: <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 name(); | ||
|
||
/** | ||
* Description for the metric. | ||
* | ||
* <p>Description strings should follow the metric 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 ""; | ||
} |
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(name = "customizedName") | ||
public void method() {} | ||
|
||
@Counted(name = "methodWithAttributes") | ||
public void attributes( | ||
@Attribute String attribute1, @Attribute(name = "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(name = "customizedName") | ||
public void method() {} | ||
|
||
@Timed(name = "methodWithAttributes") | ||
public void attributes( | ||
@Attribute String attribute1, @Attribute(name = "attribute2") long attribute2) {} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.