0

我正在尝试使用 Spring Boot 将 Opentelemetry 与 Zuul 集成,但没有成功。我想要做的是将 Opentelemetry 上下文从 Zuul 传递到链中的其他微服务,当响应进入 Zuul 时关闭跟踪

(创建跟踪) Zuul --call--> (new Span)微服务​​ A --call--> (new Span)微服务​​ B

(关闭跟踪)Zuul <--response-- 微服务A <-response-- 微服务B

有人做过类似的东西吗?

4

1 回答 1

1

我这样解决了:

@Component
public class HeaderRequestFilter extends ZuulFilter {
    
    @Autowired
    private Tracer tracer;
    
    @Override
    public int filterOrder() {
        // run before PreDecoration
        return FilterConstants.PRE_DECORATION_FILTER_ORDER - 1;
    }

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
                
        RequestContext ctx = RequestContext.getCurrentContext();

        Span span = tracer.spanBuilder(ctx.getRequest().getRequestURI()).startSpan();
        span.setAttribute("eurekaInstanceId", eurekaInstanceId);        
        tracer.withSpan(span);
        
        OpenTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), ctx, new Setter<RequestContext>() {
            @Override
            public void set(RequestContext carrier, String key, String value) {             
                carrier.addZuulRequestHeader(key, value);
            }
        });

        return null;
    }
}

和响应,其中跨度是闭合的

@Component
public class HeaderResponseFilter extends ZuulFilter {

    @Autowired
    private Tracer tracer;
    
    
    @Override
    public int filterOrder() {
        // Run before PreDecoration
        return FilterConstants.PRE_DECORATION_FILTER_ORDER - 1; 
    }

    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        // Close span
        tracer.getCurrentSpan().end();
        
        return null;
    }
}
于 2020-10-01T13:05:24.003 回答