-
Notifications
You must be signed in to change notification settings - Fork 782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding TraceId to Response - Improvements? #633
Comments
You can extend the |
Note to myself: Add a test and embed that test in the docs |
👍 Thanks and yes, it does. Wish :):) spring.sleuth.responseheader = true |
Heh - I'll close this issue for now but if anyone is interested in reopening it please upvote. If there are quite a few people interested in it we'll come back to the discussion. |
is there any example for this, thanks |
BTW there is a discussion about this going on here - openzipkin/openzipkin.github.io#48 . So in case of questions "why" we don't allow this by default please let's go to that issue. I'll prepare sample code of how you can achieve this and update the documentation too. |
One note... If you want to do this with versions prior to @Bean
@Primary
TraceFilter myTraceFilter(BeanFactory beanFactory, final Tracer tracer) {
return new TraceFilter(beanFactory) {
@Override protected void addResponseTags(HttpServletResponse response,
Throwable e) {
// execute the default behaviour
super.addResponseTags(response, e);
// for readability we're returning trace id in a hex form
response.addHeader("ZIPKIN-TRACE-ID",
Span.idToHex(tracer.getCurrentSpan().getTraceId()));
// we can also add some custom tags
tracer.addTag("custom", "tag");
}
};
} |
@marcingrzejszczak We used @Slf4j
@RequiredArgsConstructor
public class TraceHeaderInterceptor extends HandlerInterceptorAdapter {
private static final String TRACE_ID = "TraceId";
private final BeanFactory beanFactory;
private Tracer tracer;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
if (getTracer().isTracing() && StringUtils.isEmpty(response.getHeader(TRACE_ID))) {
response
.setHeader(TRACE_ID, Span.idToHex(getTracer().getCurrentSpan().getTraceId()));
}
return true;
}
private Tracer getTracer() {
if (tracer == null) {
tracer = beanFactory.getBean(Tracer.class);
}
return tracer;
}
} |
Seems like TraceFilter was removed on 2.0.0. Is there an alternative to have the trace ID on response headers? |
We're doing the following bean registration @Bean
public FilterRegistrationBean traceWebFilter(
TracingFilter tracingFilter) {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(tracingFilter);
filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE, REQUEST);
filterRegistrationBean.setOrder(TraceWebServletAutoConfiguration.TRACING_FILTER_ORDER);
return filterRegistrationBean;
}
@Bean
public FilterRegistrationBean exceptionThrowingFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new ExceptionLoggingFilter());
filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE, REQUEST);
filterRegistrationBean.setOrder(TraceWebServletAutoConfiguration.TRACING_FILTER_ORDER + 1);
return filterRegistrationBean;
}
@Bean
@ConditionalOnMissingBean
public TracingFilter tracingFilter(HttpTracing tracing) {
return (TracingFilter) TracingFilter.create(tracing);
} You can create your own filter that starts after the |
Can this be as simple as an annotation |
If you create such an annotation then for sure it can :) |
response.isCommitted() and response can't Modify |
with spring-cloud Hoxton.RELEASE, I have done following implementation
|
Is this (https://stackoverflow.com/questions/41222405/adding-the-traceid-from-spring-cloud-sleuth-to-response) still the best way to do it in > 1.2.0?
Thanks.
The text was updated successfully, but these errors were encountered: