You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarkus 2.13.x集成OpenTelemetry:HTTP Span命名与Jaeger展示问题求助

Quarkus 2.13.x集成OpenTelemetry问题解决方案

问题1:修改HTTP REST请求的Span名称

默认情况下,Quarkus OpenTelemetry生成的HTTP Span名称仅包含请求方法(如PUT/POST),@WithSpan注解只会新增子Span,无法修改自动生成的HTTP Span名称。可通过以下两种方式解决:

方式1:用JAX-RS拦截器自定义Span名称

创建JAX-RS拦截器,在REST方法执行时更新当前HTTP Span的业务名称:

import io.opentelemetry.api.trace.Span;
import jakarta.annotation.Priority;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
@Priority(Interceptor.Priority.PLATFORM_BEFORE + 100)
public class RestSpanCustomizer {

    @AroundInvoke
    public Object updateSpanName(InvocationContext ctx) throws Exception {
        Span currentSpan = Span.current();
        // 可自定义业务名称:比如取方法名、自定义注解值等
        String businessSpanName = ctx.getMethod().getName();
        // 示例:从自定义注解获取名称
        // BusinessSpan annotation = ctx.getMethod().getAnnotation(BusinessSpan.class);
        // String businessSpanName = annotation.value();
        
        currentSpan.updateName(businessSpanName);
        return ctx.proceed();
    }
}

在REST资源类上添加@Interceptors(RestSpanCustomizer.class),或通过beans.xml全局注册拦截器。

方式2:配置使用请求路径作为Span名称

若业务名称可复用请求路径,直接修改配置文件:

quarkus.opentelemetry.tracer.span-name-provider=http-path

此配置会将HTTP Span名称设置为完整请求路径(如/api/users),无需编写代码。

问题2:Jaeger中主Span随机被DB查询Span替代

该问题核心是Span父子上下文未正确传播,导致DB Span成为根Span而非HTTP Span的子Span,可从以下几点修复:

1. 处理异步操作的上下文传播

若代码中存在CompletableFuture、自定义线程池等异步逻辑,需手动传递OpenTelemetry上下文:

import io.opentelemetry.context.Context;

// 获取当前请求上下文
Context currentCtx = Context.current();
CompletableFuture.runAsync(() -> {
    // 将上下文绑定到当前线程
    try (var scope = currentCtx.makeCurrent()) {
        // 执行DB查询等操作
    }
});

优先使用Quarkus原生的Uni/Multi响应式API,它们默认会自动传播上下文。

2. 验证DB扩展的遥测集成

确保JDBC/Hibernate Panache扩展已正确集成OpenTelemetry:

  • 确认依赖中包含quarkus-opentelemetry-jdbc或对应ORM的遥测扩展
  • 检查配置quarkus.opentelemetry.instrumentation.jdbc.enabled=true是否开启(默认已开启)

3. 检查Span父子关系

在Jaeger中查看Span的Parent ID字段:

  • 正常情况下,DB Span的Parent ID应等于HTTP Span的Span ID
  • 若DB Span的Parent ID为空,说明上下文未传递,需排查异步代码或配置问题

4. 升级Quarkus补丁版本

Quarkus 2.13.x早期版本存在上下文传播bug,建议升级到该系列最新补丁版本,修复已知线程上下文问题。


内容的提问来源于stack exchange,提问作者sg2000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 01:00:11