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

能否为Spring Cloud Sleuth生成的默认追踪Span添加API参数?

给Spring Cloud Sleuth + OpenTelemetry的Span添加API参数信息

方法1:手动在接口中注入参数到Span

可以直接在REST接口方法里通过Tracer实例,把请求参数写入当前Span的attributes中:

import io.opentelemetry.api.trace.Tracer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoApiController {
    private final Tracer tracer;

    public DemoApiController(Tracer tracer) {
        this.tracer = tracer;
    }

    @GetMapping("/api/query")
    public String queryData(@RequestParam String userId, @RequestParam String orderId) {
        // 将参数添加到当前Span的属性中
        tracer.currentSpan().setAttribute("api.param.user_id", userId);
        tracer.currentSpan().setAttribute("api.param.order_id", orderId);
        
        // 业务逻辑处理
        return "query completed";
    }
}

这样导出的Span就会包含这些自定义的参数属性。

方法2:用AOP统一拦截注入参数

如果不想在每个接口里重复编写代码,可以通过Spring AOP拦截所有Controller方法,自动提取参数并写入Span:

  1. 创建切面类:
import io.opentelemetry.api.trace.Tracer;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SpanParamInjectAspect {
    private final Tracer tracer;

    public SpanParamInjectAspect(Tracer tracer) {
        this.tracer = tracer;
    }

    // 拦截所有Controller包下的方法
    @Before("execution(* com.yourproject.controller.*.*(..))")
    public void addParamsToSpan(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        String[] paramNames = ((org.aspectj.lang.reflect.MethodSignature) joinPoint.getSignature()).getParameterNames();
        
        for (int i = 0; i < args.length; i++) {
            String paramKey = "api.param." + paramNames[i];
            // 过滤敏感参数,避免隐私数据泄露
            if (!paramNames[i].equalsIgnoreCase("password") && !paramNames[i].equalsIgnoreCase("token")) {
                tracer.currentSpan().setAttribute(paramKey, args[i].toString());
            }
        }
    }
}
  1. 确保项目引入Spring AOP依赖:
<!-- Maven依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

这种方式能批量给所有接口的Span添加参数,同时通过过滤逻辑避免敏感数据被导出。

方法3:通过OpenTelemetry配置自动捕获参数

OpenTelemetry支持通过配置自动捕获HTTP请求的查询参数或表单参数(需注意SDK版本兼容性),可以在application.properties中添加以下配置:

# 开启URL查询参数捕获
otel.instrumentation.http.capture-query-parameters=true
# 开启POST表单参数捕获(仅针对application/x-www-form-urlencoded类型)
otel.instrumentation.http.capture-form-parameters=true
# 可选:指定仅捕获特定参数,减少不必要的数据
otel.instrumentation.http.query-parameters=userId,orderId

此方式会将参数整合到Span的http.query或对应表单属性中,适合不需要自定义参数键名的场景。

注意:无论用哪种方式,都必须过滤敏感数据(如密码、令牌、隐私信息),防止这些数据被泄露到追踪系统中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 06:40:51