Skip to content

Commit 2faf0ea

Browse files
jeanbisuttitiffany76jaydeluca
authored
Improve the annotation doc of the OTel starter (#4806)
Co-authored-by: Tiffany Hrabusa <[email protected]> Co-authored-by: Jay DeLuca <[email protected]>
1 parent be65f15 commit 2faf0ea

File tree

3 files changed

+78
-36
lines changed

3 files changed

+78
-36
lines changed

content/en/docs/zero-code/java/spring-boot-starter/additional-instrumentations.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Additional instrumentation
3-
weight: 50
3+
weight: 60
44
---
55

66
The OpenTelemetry Spring Boot starter provides
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,95 @@
11
---
22
title: Annotations
3-
weight: 60
3+
weight: 50
4+
cSpell:ignore: proxys
45
---
56

67
For most users, the out-of-the-box instrumentation is completely sufficient and
78
nothing more has to be done. Sometimes, however, users wish to create
89
[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.
1011

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.
2415

2516
```java
26-
import org.springframework.stereotype.Component;
27-
2817
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
2918
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 {
3819

3920
@WithSpan
40-
public void tracedMethod() {
21+
public void tracedMethod(@SpanAttribute parameter) {
4122
}
23+
```
4224

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).
4931

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();
5242
}
5343

54-
public void tracedMethodWithAttribute(@SpanAttribute("attributeName") String parameter) {
44+
@WithSpan
45+
public void anotherMethod() {
5546
}
5647
}
5748
```
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 |

static/refcache.json

+4
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,10 @@
17471747
"StatusCode": 206,
17481748
"LastSeen": "2024-04-03T12:51:54.758528517+02:00"
17491749
},
1750+
"https://docs.spring.io/spring-framework/reference/core/aop/proxying.html": {
1751+
"StatusCode": 206,
1752+
"LastSeen": "2024-07-09T08:58:07.531333949Z"
1753+
},
17501754
"https://docs.spring.io/spring-security/reference/servlet/authorization/architecture.html#authz-authorities": {
17511755
"StatusCode": 206,
17521756
"LastSeen": "2024-02-12T22:02:33.483262-05:00"

0 commit comments

Comments
 (0)