Opentelemetry集成Zuul

huangapple go评论75阅读模式
英文:

Opentememetry Zuul integration

问题

我正在尝试但未能成功地将 Opentelemetry 与使用 Spring Boot 的 Zuul 集成。我尝试的目标是将 Opentelemetry 上下文从 Zuul 传递到链中的其他微服务,当响应传回 Zuul 时关闭跟踪。

(创建跟踪)Zuul --调用--> (新建 Span)微服务 A --调用--> (新建 Span)微服务 B

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

是否有人做过类似于这样的事情?

英文:

I'm trying with no success to integrate Opentelemetry with Zuul using Spring Boot. What I'm trying to do is to pass Opentelemetry context from Zuul to other microservices in the chain and when the response came into Zuul close the trace

(Create trace) Zuul --call--> (new Span) Microservice A --call--> (new Span) Microservice B

(Close trace ) Zuul <--response-- Microservice A <-response-- Microservice B

Anyone has made something similar to this?

答案1

得分: 1

我是ChatGPT,你的中文翻译,以下是你提供的内容翻译结果:

我是用以下方式解决的:

@Component
public class HeaderRequestFilter extends ZuulFilter {
    
    @Autowired
    private Tracer tracer;
    
    @Override
    public int filterOrder() {
        // 在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;
    }
}

响应部分,在关闭span时:

@Component
public class HeaderResponseFilter extends ZuulFilter {

    @Autowired
    private Tracer tracer;
    
    
    @Override
    public int filterOrder() {
        // 在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() {
        // 关闭span
        tracer.getCurrentSpan().end();
        
        return null;
    }
}
英文:

I solved this way:

@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(&quot;eurekaInstanceId&quot;, eurekaInstanceId);        
        tracer.withSpan(span);
        
        OpenTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), ctx, new Setter&lt;RequestContext&gt;() {
			@Override
			public void set(RequestContext carrier, String key, String value) {				
				carrier.addZuulRequestHeader(key, value);
			}
		});

        return null;
    }
}

and the response, where the span is closed

@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;
    }
}

huangapple
  • 本文由 发表于 2020年9月30日 00:12:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64123550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定