|
1 | 1 | ---
|
2 | 2 | title: Annotations
|
3 |
| -weight: 60 |
| 3 | +weight: 50 |
| 4 | +cSpell:ignore: proxys |
4 | 5 | ---
|
5 | 6 |
|
6 | 7 | For most users, the out-of-the-box instrumentation is completely sufficient and
|
7 | 8 | nothing more has to be done. Sometimes, however, users wish to create
|
8 | 9 | [spans](/docs/concepts/signals/traces/#spans) for their own custom code without
|
9 |
| -doing too much code change. |
| 10 | +needing to make many code changes. |
10 | 11 |
|
11 |
| -## Available annotations |
12 |
| - |
13 |
| -This feature uses spring-aop to wrap methods annotated with `@WithSpan` in a |
14 |
| -span. The arguments to the method can be captured as attributed on the created |
15 |
| -span by annotating the method parameters with `@SpanAttribute`. |
16 |
| - |
17 |
| -> **Note**: this annotation can only be applied to bean methods managed by the |
18 |
| -> spring application context. To learn more about aspect weaving in spring, see |
19 |
| -> [spring-aop](https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop). |
20 |
| -
|
21 |
| -| Feature | Property | Default Value | Description | |
22 |
| -| ----------- | ------------------------------------------ | ------------- | --------------------------------- | |
23 |
| -| `@WithSpan` | `otel.instrumentation.annotations.enabled` | true | Enables the WithSpan annotations. | |
| 12 | +If you add the `WithSpan` annotation to a method, the method is wrapped in a |
| 13 | +span. The `SpanAttribute` annotation allows you to capture the method arguments |
| 14 | +as attributes. |
24 | 15 |
|
25 | 16 | ```java
|
26 |
| -import org.springframework.stereotype.Component; |
27 |
| - |
28 | 17 | import io.opentelemetry.instrumentation.annotations.SpanAttribute;
|
29 | 18 | import io.opentelemetry.instrumentation.annotations.WithSpan;
|
30 |
| -import io.opentelemetry.api.trace.Span; |
31 |
| -import io.opentelemetry.api.trace.SpanKind; |
32 |
| - |
33 |
| -/** |
34 |
| - * Test WithSpan |
35 |
| - */ |
36 |
| -@Component |
37 |
| -public class TracedClass { |
38 | 19 |
|
39 | 20 | @WithSpan
|
40 |
| - public void tracedMethod() { |
| 21 | + public void tracedMethod(@SpanAttribute parameter) { |
41 | 22 | }
|
| 23 | +``` |
42 | 24 |
|
43 |
| - @WithSpan(value="span name") |
44 |
| - public void tracedMethodWithName() { |
45 |
| - Span currentSpan = Span.current(); |
46 |
| - currentSpan.addEvent("ADD EVENT TO tracedMethodWithName SPAN"); |
47 |
| - currentSpan.setAttribute("isTestAttribute", true); |
48 |
| - } |
| 25 | +{{% alert title="Note" color="info" %}} The OpenTelemetry annotations use Spring |
| 26 | +AOP based on proxys. |
| 27 | + |
| 28 | +These annotations work only for the methods of the proxy. You can learn more in |
| 29 | +the |
| 30 | +[Spring documentation](https://docs.spring.io/spring-framework/reference/core/aop/proxying.html). |
49 | 31 |
|
50 |
| - @WithSpan(kind = SpanKind.CLIENT) |
51 |
| - public void tracedClientSpan() { |
| 32 | +In the following example, the `WithSpan` annotation won't do anything when the |
| 33 | +GET endpoint is called: |
| 34 | + |
| 35 | +```java |
| 36 | +@RestController |
| 37 | +public class MyControllerManagedBySpring { |
| 38 | + |
| 39 | + @GetMapping("/ping") |
| 40 | + public void aMethod() { |
| 41 | + anotherMethod(); |
52 | 42 | }
|
53 | 43 |
|
54 |
| - public void tracedMethodWithAttribute(@SpanAttribute("attributeName") String parameter) { |
| 44 | + @WithSpan |
| 45 | + public void anotherMethod() { |
55 | 46 | }
|
56 | 47 | }
|
57 | 48 | ```
|
| 49 | + |
| 50 | +{{% /alert %}} |
| 51 | + |
| 52 | +{{% alert title="Note" color="info" %}} |
| 53 | + |
| 54 | +To be able to use the OpenTelemetry annotations, you have to add the Spring Boot |
| 55 | +Starter AOP dependency to your project: |
| 56 | + |
| 57 | +{{< tabpane text=true >}} {{% tab header="Maven (`pom.xml`)" lang=Maven %}} |
| 58 | + |
| 59 | +```xml |
| 60 | +<dependencies> |
| 61 | + <dependency> |
| 62 | + <groupId>org.springframework.boot</groupId> |
| 63 | + <artifactId>>spring-boot-starter-aop</artifactId> |
| 64 | + </dependency> |
| 65 | +</dependencies> |
| 66 | +``` |
| 67 | + |
| 68 | +{{% /tab %}} {{% tab header="Gradle (`gradle.build`)" lang=Gradle %}} |
| 69 | + |
| 70 | +```kotlin |
| 71 | +dependencies { |
| 72 | + implementation("org.springframework.boot:spring-boot-starter-aop") |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +{{% /tab %}} {{< /tabpane >}} |
| 77 | + |
| 78 | +{{% /alert %}} |
| 79 | + |
| 80 | +You can disable the OpenTelemetry annotations by setting the |
| 81 | +`otel.instrumentation.annotations.enabled` property to `false`. |
| 82 | + |
| 83 | +You can customize the span by using the elements of the `WithSpan` annotation: |
| 84 | + |
| 85 | +| Name | Type | Description | Default Value | |
| 86 | +| ------- | ---------- | --------------------- | ------------------- | |
| 87 | +| `value` | `String` | Span name | ClassName.Method | |
| 88 | +| `kind` | `SpanKind` | Span kind of the span | `SpanKind.INTERNAL` | |
| 89 | + |
| 90 | +You can set the attribute name from the `value` element of the `SpanAttribute` |
| 91 | +annotation: |
| 92 | + |
| 93 | +| Name | Type | Description | Default Value | |
| 94 | +| ------- | -------- | -------------- | --------------------- | |
| 95 | +| `value` | `String` | Attribute name | Method parameter name | |
0 commit comments