注解
对于大多数用户来说,开箱即用的插桩功能已经完全足够,无需进行任何其他操作。 然而,在某些情况下,用户可能希望为自己的自定义代码创建 Span,而无需进行许多代码更改。
如果你在方法上添加 WithSpan
注解,则该方法会被包装在一个 Span 中。
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;
/** 测试 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() {}
@WithSpan
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 启动器 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
注解的元素来自定义 Span:
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
value | String | Span 名称 | 类名.方法名 |
kind | SpanKind | Span 类型 | SpanKind.INTERNAL |
你可以使用 SpanAttribute
注解的 value
元素来自定义属性名称:
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
value | String | 属性名称 | 方法参数名 |
下一步操作
除使用注解外,OpenTelemetry API 还允许你获取一个可用于自定义插桩的追踪器。
Feedback
Was this page helpful?
Thank you. Your feedback is appreciated!
Please let us know how we can improve this page. Your feedback is appreciated!