-
Notifications
You must be signed in to change notification settings - Fork 876
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AddingSpanAttributes annotation (#7787)
Co-authored-by: Mateusz Rzeszutek <[email protected]>
- Loading branch information
Showing
24 changed files
with
1,766 additions
and
1,043 deletions.
There are no files selected for viewing
9 changes: 8 additions & 1 deletion
9
docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
Comparing source compatibility of against | ||
No changes. | ||
+++ NEW ANNOTATION: PUBLIC(+) ABSTRACT(+) io.opentelemetry.instrumentation.annotations.AddingSpanAttributes (not serializable) | ||
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a. | ||
+++ NEW INTERFACE: java.lang.annotation.Annotation | ||
+++ NEW SUPERCLASS: java.lang.Object | ||
+++ 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 (+) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...va/io/opentelemetry/instrumentation/api/annotation/support/CombinedAttributeBindings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.annotation.support; | ||
|
||
import io.opentelemetry.api.common.AttributesBuilder; | ||
|
||
/** AttributeBindings implementation that is able to chain multiple AttributeBindings. */ | ||
final class CombinedAttributeBindings implements AttributeBindings { | ||
private final AttributeBindings parent; | ||
private final int index; | ||
private final AttributeBinding binding; | ||
|
||
public CombinedAttributeBindings(AttributeBindings parent, int index, AttributeBinding binding) { | ||
this.parent = parent; | ||
this.index = index; | ||
this.binding = binding; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void apply(AttributesBuilder target, Object[] args) { | ||
parent.apply(target, args); | ||
if (args != null && args.length > index) { | ||
Object arg = args[index]; | ||
if (arg != null) { | ||
binding.apply(target, arg); | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
.../java/io/opentelemetry/instrumentation/api/annotation/support/EmptyAttributeBindings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.annotation.support; | ||
|
||
import io.opentelemetry.api.common.AttributesBuilder; | ||
|
||
/** Singleton empty implementation of AttributeBindings. */ | ||
enum EmptyAttributeBindings implements AttributeBindings { | ||
INSTANCE; | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void apply(AttributesBuilder target, Object[] args) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...java/io/opentelemetry/instrumentation/api/annotation/support/SpanAttributesExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.api.annotation.support; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.instrumentation.api.internal.cache.Cache; | ||
import java.lang.reflect.Method; | ||
|
||
/** Extractor of {@link io.opentelemetry.api.common.Attributes} for a traced method. */ | ||
public final class SpanAttributesExtractor { | ||
|
||
private final Cache<Method, AttributeBindings> cache; | ||
private final ParameterAttributeNamesExtractor parameterAttributeNamesExtractor; | ||
|
||
public static SpanAttributesExtractor create( | ||
ParameterAttributeNamesExtractor parameterAttributeNamesExtractor) { | ||
return new SpanAttributesExtractor(parameterAttributeNamesExtractor, new MethodCache<>()); | ||
} | ||
|
||
SpanAttributesExtractor( | ||
ParameterAttributeNamesExtractor parameterAttributeNamesExtractor, | ||
Cache<Method, AttributeBindings> cache) { | ||
this.parameterAttributeNamesExtractor = parameterAttributeNamesExtractor; | ||
this.cache = cache; | ||
} | ||
|
||
public Attributes extract(Method method, Object[] args) { | ||
AttributesBuilder attributes = Attributes.builder(); | ||
AttributeBindings bindings = | ||
cache.computeIfAbsent( | ||
method, (Method m) -> AttributeBindings.bind(m, parameterAttributeNamesExtractor)); | ||
if (!bindings.isEmpty()) { | ||
bindings.apply(attributes, args); | ||
} | ||
return attributes.build(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ions/src/main/java/io/opentelemetry/instrumentation/annotations/AddingSpanAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* This annotation marks that an execution of this method or constructor is able to add attributes | ||
* to the current span {@link io.opentelemetry.api.trace.Span}. | ||
* | ||
* <p>Application developers can use this annotation to signal OpenTelemetry auto-instrumentation | ||
* that attributes annotated with the {@link | ||
* io.opentelemetry.instrumentation.annotations.SpanAttribute} should be detected and added to the | ||
* current span. | ||
* | ||
* <p>If no span is currently active no new span will be created, and no attributes will be | ||
* extracted. | ||
* | ||
* <p>Similar to {@link | ||
* io.opentelemetry.api.trace.Span#setAttribute(io.opentelemetry.api.common.AttributeKey, | ||
* java.lang.Object) Span.setAttribute() } methods, if the key is already mapped to a value the old | ||
* value is replaced by the extracted value. | ||
* | ||
* <p>If you are a library developer, then probably you should NOT use this annotation, because it | ||
* is non-functional without some form of auto-instrumentation. | ||
*/ | ||
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface AddingSpanAttributes {} |
28 changes: 28 additions & 0 deletions
28
.../java/io/opentelemetry/instrumentation/annotations/AddingSpanAttributesUsageExamples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.annotations; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
|
||
/** | ||
* This class is not a classical test. It's just a demonstration of possible usages of {@link | ||
* AddingSpanAttributes} annotation together with some explanations. The goal of this class is to | ||
* serve as an early detection system for inconvenient API and unintended API breakage. | ||
*/ | ||
@SuppressWarnings("unused") | ||
public class AddingSpanAttributesUsageExamples { | ||
|
||
/** | ||
* The current {@link Span} will be updated to contain the annotated method parameters as | ||
* attributes. | ||
* | ||
* @param attribute1 A span attribute with the default name of {@code attribute1}. | ||
* @param value A span attribute with the name "attribute2". | ||
*/ | ||
@AddingSpanAttributes | ||
public void attributes( | ||
@SpanAttribute String attribute1, @SpanAttribute("attribute2") long value) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.