|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.annotations; |
| 7 | + |
| 8 | +import java.lang.annotation.ElementType; |
| 9 | +import java.lang.annotation.Retention; |
| 10 | +import java.lang.annotation.RetentionPolicy; |
| 11 | +import java.lang.annotation.Target; |
| 12 | + |
| 13 | +/** |
| 14 | + * This annotation creates a {@link io.opentelemetry.api.metrics.LongCounter Counter} instrument |
| 15 | + * recording the number of invocations of the annotated method or constructor. |
| 16 | + * |
| 17 | + * <p>By default, the Counter instrument will have the following attributes: |
| 18 | + * |
| 19 | + * <ul> |
| 20 | + * <li><b>code.namespace:</b> The fully qualified name of the class whose method is invoked. |
| 21 | + * <li><b>code.function:</b> The name of the annotated method, or "new" if the annotation is on a |
| 22 | + * constructor. |
| 23 | + * <li><b>exception.type:</b> This is only present if an Exception is thrown, and contains the |
| 24 | + * {@link Class#getCanonicalName() canonical name} of the Exception. |
| 25 | + * </ul> |
| 26 | + * |
| 27 | + * <p>Application developers can use this annotation to signal OpenTelemetry auto-instrumentation |
| 28 | + * that the Counter instrument should be created. |
| 29 | + * |
| 30 | + * <p>If you are a library developer, then probably you should NOT use this annotation, because it |
| 31 | + * is non-functional without the OpenTelemetry auto-instrumentation agent, or some other annotation |
| 32 | + * processor. |
| 33 | + */ |
| 34 | +@Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) |
| 35 | +@Retention(RetentionPolicy.RUNTIME) |
| 36 | +public @interface Counted { |
| 37 | + |
| 38 | + /** |
| 39 | + * Name of the Counter instrument. |
| 40 | + * |
| 41 | + * <p>The name should follow the instrument naming rule: <a |
| 42 | + * 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> |
| 43 | + * |
| 44 | + * <p>The default name is {@code method.invocation.count}. |
| 45 | + */ |
| 46 | + String value() default ""; |
| 47 | + |
| 48 | + /** |
| 49 | + * Description of the instrument. |
| 50 | + * |
| 51 | + * <p>Description strings should follow the instrument description rules: <a |
| 52 | + * 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> |
| 53 | + * |
| 54 | + * <p>This property would not take effect if the value is not specified. |
| 55 | + */ |
| 56 | + String description() default ""; |
| 57 | + |
| 58 | + /** |
| 59 | + * Unit of the instrument. |
| 60 | + * |
| 61 | + * <p>Unit strings should follow the instrument unit rules: <a |
| 62 | + * 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> |
| 63 | + * |
| 64 | + * <p>This property would not take effect if the value is not specified. |
| 65 | + */ |
| 66 | + String unit() default "{invocation}"; |
| 67 | + |
| 68 | + /** |
| 69 | + * List of key-value pairs to supply additional attributes. |
| 70 | + * |
| 71 | + * <p>Example: |
| 72 | + * |
| 73 | + * <pre> |
| 74 | + * {@literal @}Counted( |
| 75 | + * additionalAttributes = { |
| 76 | + * "key1", "value1", |
| 77 | + * "key2", "value2", |
| 78 | + * }) |
| 79 | + * </pre> |
| 80 | + */ |
| 81 | + String[] additionalAttributes() default {}; |
| 82 | + |
| 83 | + /** |
| 84 | + * Attribute name for the return value. |
| 85 | + * |
| 86 | + * <p>The name of the attribute for the return value of the method call. {@link Object#toString()} |
| 87 | + * will be called on the return value to convert it to a String. |
| 88 | + * |
| 89 | + * <p>By default, the instrument will not have an attribute with the return value. |
| 90 | + * |
| 91 | + * <p>Warning: be careful to fill it because it might cause an explosion of the cardinality on |
| 92 | + * your metric |
| 93 | + */ |
| 94 | + String returnValueAttribute() default ""; |
| 95 | +} |
0 commit comments