Annotations
The content of this page may be outdated and some links may be invalid. A newer version of this page exists in English. To see the changes to the English page since this page was last updated: visit GitHub compare 3d179dbe..1edcb499 and search for More information ...
content/en/docs/zero-code/java/spring-boot-starter/annotations.md.
Pour la plupart des utilisateurs, l’instrumentation prête à l’emploi est suffisante et rien de plus n’a besoin d’être fait. Cependant, parfois, les utilisateurs souhaitent créer des spans pour leur propre code personnalisé sans avoir besoin de faire beaucoup de changements de code.
Si vous ajoutez l’annotation WithSpan à une méthode, la méthode est enveloppée dans un span. L’annotation SpanAttribute vous permet de capturer les arguments de la méthode comme attributs.
package otel; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.annotations.SpanAttribute; import io.opentelemetry.instrumentation.annotations.WithSpan; import org.springframework.stereotype.Component; /** Test WithSpan */ @Component public class TracedClass { @WithSpan public void tracedMethod() {} @WithSpan(value = "span name") public void tracedMethodWithName() { Span currentSpan = Span.current(); currentSpan.addEvent("ADD EVENT TO tracedMethodWithName SPAN"); currentSpan.setAttribute("isTestAttribute", true); } @WithSpan(kind = SpanKind.CLIENT) public void tracedClientSpan() {} public void tracedMethodWithAttribute(@SpanAttribute("attributeName") String parameter) {} } Les annotations OpenTelemetry utilisent Spring AOP basé sur des proxys.
Ces annotations ne fonctionnent que pour les méthodes du proxy. Vous pouvez en apprendre plus dans la documentation Spring.
Dans l’exemple suivant, l’annotation WithSpan ne fera rien lorsque le point de terminaison GET est appelé :
@RestController public class MyControllerManagedBySpring { @GetMapping("/ping") public void aMethod() { anotherMethod(); } @WithSpan public void anotherMethod() { } } Pour pouvoir utiliser les annotations OpenTelemetry, vous devez ajouter la dépendance Spring Boot Starter AOP à votre projet :
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> </dependencies> dependencies { implementation("org.springframework.boot:spring-boot-starter-aop") } Vous pouvez désactiver les annotations OpenTelemetry en définissant la propriété otel.instrumentation.annotations.enabled à false.
Vous pouvez personnaliser le span en utilisant les éléments de l’annotation WithSpan :
| Nom | Type | Description | Valeur par défaut |
|---|---|---|---|
value | String | Nom du span | ClassName.Method |
kind | SpanKind | Type de span du span | SpanKind.INTERNAL |
Vous pouvez définir le nom de l’attribut à partir de l’élément value de l’annotation SpanAttribute :
| Nom | Type | Description | Valeur par défaut |
|---|---|---|---|
value | String | Nom de l’attribut | Nom du paramètre de la méthode |
Prochaines étapes
Au-delà de l’utilisation d’annotations, l’API OpenTelemetry vous permet d’obtenir un traceur qui peut être utilisé pour l’instrumentation personnalisée.
Feedback
Cette page est-elle utile?
Thank you. Your feedback is appreciated!
Please let us know how we can improve this page. Your feedback is appreciated!