アノテーション
ほとんどのユーザーにとって、すぐに使える計装は完全に十分であり、何もする必要はありません。 しかし時には、ユーザーが多くのコード変更を必要とせずに、独自のカスタムコードに対してスパンを作成したい場合があります。
メソッドにWithSpan
アノテーションを追加すると、そのメソッドはスパンでラップされます。
SpanAttribute
アノテーションを使用すると、メソッドの引数を属性としてキャプチャできます。
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) {}
}
OpenTelemetryアノテーションは、プロキシに基づくSpring AOPを使用します。
これらのアノテーションは、プロキシのメソッドに対してのみ機能します。詳細については、Springドキュメントを参照してください。
次の例では、GETエンドポイントが呼び出されたときに、WithSpan
アノテーションは何もしません。
@RestController
public class MyControllerManagedBySpring {
@GetMapping("/ping")
public void aMethod() {
anotherMethod();
}
@WithSpan
public void anotherMethod() {
}
}
OpenTelemetryアノテーションを使用できるようにするには、Spring Boot Starter AOP依存関係をプロジェクトに追加する必要があります。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
dependencies {
implementation("org.springframework.boot:spring-boot-starter-aop")
}
otel.instrumentation.annotations.enabled
プロパティをfalse
に設定することで、OpenTelemetryアノテーションを無効にできます。
WithSpan
アノテーションの要素を使用してスパンをカスタマイズできます。
名前 | 型 | 説明 | デフォルト値 |
---|---|---|---|
value | String | スパン名 | ClassName.Method |
kind | SpanKind | スパンのスパンの種類 | SpanKind.INTERNAL |
SpanAttribute
アノテーションのvalue
要素から属性名を設定できます。
名前 | 型 | 説明 | デフォルト値 |
---|---|---|---|
value | String | 属性名 | メソッドパラメータ名 |
次のステップ
アノテーションの使用以外にも、OpenTelemetry APIを使用すると、カスタム計装に使用できるトレーサーを取得できます。
フィードバック
このページは役に立ちましたか?
Thank you. Your feedback is appreciated!
Please let us know how we can improve this page. Your feedback is appreciated!