Skip to content

Commit

Permalink
add metric annotation instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncan-tree-zhou committed May 24, 2024
1 parent 98d0c3d commit 95f0f0d
Show file tree
Hide file tree
Showing 16 changed files with 1,076 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ out/

# Others #
##########
instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/annotations/
/logs/*
/bin
/out
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
Comparing source compatibility of against
No changes.
+++ NEW ANNOTATION: PUBLIC(+) ABSTRACT(+) io.opentelemetry.instrumentation.annotations.Counted (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW INTERFACE: java.lang.annotation.Annotation
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String[] additionalAttributes()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String description()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String returnValueAttribute()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String unit()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String value()
+++ NEW ANNOTATION: java.lang.annotation.Target
+++ NEW ELEMENT: value=java.lang.annotation.ElementType.METHOD,java.lang.annotation.ElementType.CONSTRUCTOR (+)
+++ NEW ANNOTATION: java.lang.annotation.Retention
+++ NEW ELEMENT: value=java.lang.annotation.RetentionPolicy.RUNTIME (+)
+++ NEW ANNOTATION: PUBLIC(+) ABSTRACT(+) io.opentelemetry.instrumentation.annotations.MetricAttribute (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW INTERFACE: java.lang.annotation.Annotation
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String value()
+++ NEW ANNOTATION: java.lang.annotation.Target
+++ NEW ELEMENT: value=java.lang.annotation.ElementType.PARAMETER (+)
+++ NEW ANNOTATION: java.lang.annotation.Retention
+++ NEW ELEMENT: value=java.lang.annotation.RetentionPolicy.RUNTIME (+)
+++ NEW ANNOTATION: PUBLIC(+) ABSTRACT(+) io.opentelemetry.instrumentation.annotations.Timed (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW INTERFACE: java.lang.annotation.Annotation
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String[] additionalAttributes()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String description()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String returnValueAttribute()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String unit()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String value()
+++ NEW ANNOTATION: java.lang.annotation.Target
+++ NEW ELEMENT: value=java.lang.annotation.ElementType.METHOD,java.lang.annotation.ElementType.CONSTRUCTOR (+)
+++ NEW ANNOTATION: java.lang.annotation.Retention
+++ NEW ELEMENT: value=java.lang.annotation.RetentionPolicy.RUNTIME (+)
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.annotations;

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, or "new" if the annotation is on a
* constructor.
* <li><b>exception.type:</b> This is only present if an Exception is thrown, and contains the
* {@link Class#getCanonicalName() canonical name} of the Exception.
* </ul>
*
* <p>Application developers can use this annotation to signal OpenTelemetry auto-instrumentation
* that the Counter 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 Counted {

/**
* Name of the Counter 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>
*
* <p>The default name is {@code method.invocation.count}.
*/
String value() default "";

/**
* Description of 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>
*
* <p>This property would not take effect if the value is not specified.
*/
String description() default "";

/**
* Unit of the instrument.
*
* <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.
*/
String unit() default "{invocation}";

/**
* List of key-value pairs to supply additional attributes.
*
* <p>Example:
*
* <pre>
* {@literal @}Counted(
* additionalAttributes = {
* "key1", "value1",
* "key2", "value2",
* })
* </pre>
*/
String[] additionalAttributes() default {};

/**
* Attribute name for the return value.
*
* <p>The name of the attribute for the return value of the method call. {@link Object#toString()}
* will be called on the return value to convert it to a String.
*
* <p>By default, the instrument will not have 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,39 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.annotations;

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.
*/
@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
* `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
*/
String value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.annotations;

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.LongHistogram 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, or "new" of the annotation is on a
* constructor.
* <li><b>exception.type:</b> This is only present if an Exception is thrown, and contains the
* {@link Class#getCanonicalName canonical name} of the Exception.
* </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 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>
*
* <p>The default name is {@code method.invocation.duration}.
*/
String value() default "";

/**
* 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.
*/
String unit() default "s";

/**
* List of key-value pairs to supply additional attributes.
*
* <p>Example:
*
* <pre>
* {@literal @}Timed(
* additionalAttributes = {
* "key1", "value1",
* "key2", "value2",
* })
* </pre>
*/
String[] additionalAttributes() default {};

/**
* Attribute name for the return value.
*
* <p>The name of the attribute for the return value of the method call. {@link Object#toString()}
* will be called on the return value to convert it to a String.
*
* <p>By default, the instrument will not have 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,19 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.annotations;

public class CountedUsageExamples {

@Counted()
public void method1() {}

@Counted("customizedName")
public void method2() {}

@Counted
public void attributes(
@MetricAttribute String attribute1, @MetricAttribute("attribute2") long attribute2) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.annotations;

public class TimedUsageExamples {

@Timed()
public void method1() {}

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

@Timed
public void attributes(
@MetricAttribute String attribute1, @MetricAttribute("attribute2") long attribute2) {}
}
Loading

0 comments on commit 95f0f0d

Please sign in to comment.