Skip to content
Open
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
21cbf96
add metric annotation instrumentation
Duncan-tree-zhou May 14, 2024
ff05043
support returnValueAttribute and exception attribtue for Timed and Co…
Duncan-tree-zhou Jun 9, 2024
f5d339f
move timed and counted instrumentation into same gradle module
laurit Jun 10, 2024
22d6c43
Use a single instrumentation module for counted and timed instrumenta…
laurit Jun 10, 2024
aa3b47c
Paratmeter.getName() synthesizes a name when it is not present
laurit Jun 11, 2024
36a9ab5
Merge branch 'main' into add_metric_annotation_instrument
laurit Jun 17, 2024
d954ebe
cache counter/histogram per method
laurit Jun 18, 2024
9eac550
merge
laurit Jun 18, 2024
37733c1
make named required for @Counted and @Timed
laurit Jun 18, 2024
6cdfff9
change default unit to seconds
laurit Jun 18, 2024
771cc58
resue code from @WithSpan implementation to extract method paramter a…
laurit Jun 18, 2024
42aa73b
fractional duration
laurit Jun 18, 2024
c9ea9a0
update samples
laurit Jun 18, 2024
43855a5
handle async methods
laurit Jun 19, 2024
a844fa3
move new annotations to incubator module
laurit Jun 20, 2024
2c2c3d5
rework static attribute support
laurit Jun 21, 2024
7fefa91
Merge branch 'main' into add_metric_annotation_instrument
laurit Jun 21, 2024
3e3b100
disable indy
laurit Jun 21, 2024
de28d29
add javadoc
laurit Jun 26, 2024
99c2937
Merge branch 'main' into add_metric_annotation_instrument
laurit Jul 10, 2024
8a01999
move classes to incubator package
laurit Jul 10, 2024
172f2ac
Merge branch 'main' into add_metric_annotation_instrument
laurit Aug 13, 2024
6ba7771
address review comment
laurit Aug 13, 2024
0010367
move return value attribute to separate annotation
laurit Aug 14, 2024
430e690
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Jan 7, 2025
f6887ae
update module name
Duncan-tree-zhou Jan 7, 2025
630eeac
update module name
Duncan-tree-zhou Jan 7, 2025
372418a
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Jan 16, 2025
3484ebf
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Jan 19, 2025
4dbdc28
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Jan 28, 2025
5eb706d
update to
Duncan-tree-zhou Jan 28, 2025
06caf31
update to
Duncan-tree-zhou Jan 28, 2025
c2fdbb4
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Feb 9, 2025
6b6e3c4
merge main, use 'code.function.name' instead of 'code.function
Duncan-tree-zhou Feb 9, 2025
45e3c81
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Mar 5, 2025
bb4cb98
change MetricsAttribute to Attritbute
Duncan-tree-zhou Mar 5, 2025
7652fdb
merge main
Duncan-tree-zhou Apr 1, 2025
f9a2a44
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Apr 12, 2025
e301a9b
update javadoc
Duncan-tree-zhou Apr 12, 2025
e2b43a1
update javadoc
Duncan-tree-zhou Apr 12, 2025
f576d0e
add Attribute test case for Counted
Duncan-tree-zhou Apr 12, 2025
82d885c
add Attribute test case for Timed
Duncan-tree-zhou Apr 12, 2025
292aec2
update javadoc
Duncan-tree-zhou Apr 12, 2025
589e23a
rerun
Duncan-tree-zhou Apr 12, 2025
e97b25e
update javadoc
Duncan-tree-zhou Apr 14, 2025
3ea13f7
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Apr 15, 2025
90c87bf
remove timed.unit
Duncan-tree-zhou Apr 15, 2025
aaf4b3a
rename AsyncOperationEndXX to AsyncEndXX, rename AttributeForReturnVa…
Duncan-tree-zhou Apr 21, 2025
bd31e37
update javadoc for Attribute and AttributeReturnValue
Duncan-tree-zhou Apr 21, 2025
efbf2cd
revert change log
Duncan-tree-zhou Apr 21, 2025
d5a7365
Revert "rename AsyncOperationEndXX to AsyncEndXX, rename AttributeFor…
Duncan-tree-zhou Apr 22, 2025
f92cd3e
rename AttributeForReturnValue to AttributeReturnValue
Duncan-tree-zhou Apr 22, 2025
3b10ca2
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Apr 24, 2025
771fe71
rename
Duncan-tree-zhou Apr 24, 2025
874ad04
rename AsyncOperationEndHandler#handle to AsyncOperationCallback#onEnd
Duncan-tree-zhou Apr 24, 2025
298704a
update README.md for instrumentation-annotations
Duncan-tree-zhou May 12, 2025
5d76916
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou May 12, 2025
0c12705
merge main
Duncan-tree-zhou May 21, 2025
cfaeb91
fix fossa
Duncan-tree-zhou May 21, 2025
f33af44
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Jun 10, 2025
166b445
for re-run ci
Duncan-tree-zhou Jun 10, 2025
5dbf740
remove test
Duncan-tree-zhou Jun 10, 2025
2bbca47
./gradlew spotlessApply
otelbot[bot] Jun 10, 2025
1e1f594
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Jul 6, 2025
cff297f
use semconv stable
Duncan-tree-zhou Jul 6, 2025
4033cb5
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Jul 14, 2025
3b946f0
mrege main and fix conflicts
Duncan-tree-zhou Jul 19, 2025
5d33384
change value to name
Duncan-tree-zhou Jul 19, 2025
00b5577
change value to name
Duncan-tree-zhou Jul 19, 2025
e6f7a10
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Jul 19, 2025
a29759a
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Jul 27, 2025
1e16d9d
remove deplicate annotation case
Duncan-tree-zhou Jul 27, 2025
6939ed4
add overlay case for StaticAttribtue
Duncan-tree-zhou Jul 27, 2025
603c597
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Jul 28, 2025
8673767
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Jul 29, 2025
5468f46
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Jul 31, 2025
79c49f3
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Aug 3, 2025
19e8c9c
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Aug 5, 2025
077a233
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Aug 10, 2025
72d2732
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Aug 11, 2025
1b8459d
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Aug 13, 2025
b72ab8c
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Aug 18, 2025
f4ea230
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Aug 21, 2025
d719e7e
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Sep 7, 2025
917bc58
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Sep 21, 2025
6da511d
skip test task check for instrumentation-annotations-incubator becaus…
Duncan-tree-zhou Sep 21, 2025
a07c2fc
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Sep 22, 2025
a1d6a4b
Merge branch 'otel_main' into add_metric_annotation_instrument
Duncan-tree-zhou Oct 14, 2025
b0ebdad
Merge branch 'main' into add_metric_annotation_instrument
Duncan-tree-zhou Oct 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions .fossa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ targets:
- type: gradle
path: ./
target: ':instrumentation-annotations'
- type: gradle
path: ./
target: ':instrumentation-annotations-incubator'
- type: gradle
path: ./
target: ':instrumentation-annotations-support'
Expand Down Expand Up @@ -205,9 +208,6 @@ targets:
- type: gradle
path: ./
target: ':instrumentation:opentelemetry-extension-kotlin-1.0:javaagent'
- type: gradle
path: ./
target: ':instrumentation:opentelemetry-instrumentation-annotations-1.16:javaagent'
- type: gradle
path: ./
target: ':instrumentation:opentelemetry-instrumentation-api:javaagent'
Expand Down Expand Up @@ -769,6 +769,15 @@ targets:
- type: gradle
path: ./
target: ':instrumentation:opentelemetry-api:opentelemetry-api-1.50:javaagent'
- type: gradle
path: ./
target: ':instrumentation:opentelemetry-instrumentation-annotations:opentelemetry-instrumentation-annotations-1.16:javaagent'
- type: gradle
path: ./
target: ':instrumentation:opentelemetry-instrumentation-annotations:opentelemetry-instrumentation-annotations-common:javaagent'
- type: gradle
path: ./
target: ':instrumentation:opentelemetry-instrumentation-annotations:opentelemetry-instrumentation-annotations-incubator:javaagent'
- type: gradle
path: ./
target: ':instrumentation:pekko:pekko-actor-1.0:javaagent'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ configurations.configureEach {
substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api")).using(project(":instrumentation-api"))
substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-incubator")).using(project(":instrumentation-api-incubator"))
substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations")).using(project(":instrumentation-annotations"))
substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations-incubator")).using(project(":instrumentation-annotations-incubator"))
substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations-support")).using(
project(":instrumentation-annotations-support")
)
Expand Down
13 changes: 13 additions & 0 deletions instrumentation-annotations-incubator/build.gradle.kts
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,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 value() 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.
Copy link
Contributor

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.namespace and code.function have been deprecated in the semantic conventions and has been replaced by code.function.name in the which is the fully qualified name. Should this be updated in this PR as well?

* <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 value();

/**
* 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 value();

/**
* 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,15 @@
/*
* 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(@Attribute String attribute1, @Attribute("attribute2") long attribute2) {}
}
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.annotations.incubator;

public class TimedUsageExamples {

@Timed("customizedName")
public void method() {}

@Timed("methodWithAttributes")
public void attributes(@Attribute String attribute1, @Attribute("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() {}
}
Loading
Loading