Comments (6)
Thank you for your reply.
I would like to consider supporting this feature in future versions!
from logback-access-spring-boot-starter.
Hey @akkinoc, sorry for the delayed response, i was on vacation ^^
Spring Cloud allows you to retrieve the TraceContext
from the HttpServletRequest
via:
final ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
final TraceContext traceContext = (TraceContext) requestAttributes.getAttribute("org.springframework.cloud.sleuth.TraceContext", RequestAttributes.SCOPE_REQUEST);
// get tracing ids via
traceContext.traceId();
traceContext.spanId();
traceContext.parentId();
I'm not sure how it works when using the reactive stack though.
We're currently using custom DynamicConverter
implementations to resolve tracing ids the same way as i decribed above. However, that only works with synchronous appenders. I think this is because the TraceContext
is scoped to the request and asynchronous appenders can be executed after the request. Thus all request scoped beans would be already gone. I think you'd have to already resolve the tracing ids when the LogbackAccessEvent
is created to fix that issue.
from logback-access-spring-boot-starter.
Thank you for the detailed information!
I created a sample project.
https://github.com/akkinoc/try-logback-access-spring-boot-starter-with-sleuth
It worked with the following patch.
- Implement a web filter to store the attributes for logging by logback-access-spring-boot-starter.
@WebFilter
public class SleuthAttrsFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// Stores Spring Cloud Sleuth attributes for logging by logback-access-spring-boot-starter
TraceContext context = (TraceContext) request.getAttribute(TraceContext.class.getName());
request.setAttribute(SleuthAttrsFilter.class.getName() + ".traceId", context.traceId());
request.setAttribute(SleuthAttrsFilter.class.getName() + ".spanId", context.spanId());
chain.doFilter(request, response);
}
}
- Register the web filter in your application.
@Bean
public FilterRegistrationBean<SleuthAttrsFilter> sleuthAttrsFilter() {
SleuthAttrsFilter filter = new SleuthAttrsFilter();
FilterRegistrationBean<SleuthAttrsFilter> bean = new FilterRegistrationBean<>(filter);
bean.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.INCLUDE,
DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.ERROR);
return bean;
}
- Refer to the attributes stored from "logback-access.xml".
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%h %l %u [%t] "%r" %s %b [%reqAttribute{example.SleuthAttrsFilter.traceId}, %reqAttribute{example.SleuthAttrsFilter.spanId}]</pattern>
</encoder>
</appender>
<appender-ref ref="console"/>
</configuration>
Please try this patch 😃
from logback-access-spring-boot-starter.
Thank you for your suggestion!
I'm sorry, I'm not fluent in Spring Cloud Sleuth.
How can I retrieve traceId /spanId?
If you implement your own custom Appender, you may be able to solve it early.
from logback-access-spring-boot-starter.
Hello @akkinoc, thank you very much! I'll try the code you suggested next week, but I'm very sure this will work without any issues. Is there any chance that this functionality will be integrated directly in the starter or is it too big of an edge case?
Update: the suggested code works as expected (only for non webflux apps though). Ty again :D
from logback-access-spring-boot-starter.
I've updated the issue title and description as Spring Cloud Sleuth is no longer maintained and the tracing support has moved to micrometer tracing.
This is an updated version of the custom filter above:
import java.io.IOException;
import org.springframework.lang.Nullable;
import org.springframework.web.filter.OncePerRequestFilter;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.TraceContext;
import io.micrometer.tracing.Tracer;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class TracingAttributesFilter extends OncePerRequestFilter {
@Nullable
private final Tracer tracer;
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain)
throws ServletException, IOException {
if (tracer != null) {
final Span span = tracer.currentSpan();
if (span != null) {
final TraceContext traceContext = span.context();
if (traceContext != null) {
request.setAttribute(TracingAttributesFilter.class.getName() + "traceId", traceContext.traceId());
request.setAttribute(TracingAttributesFilter.class.getName() + "spanId", traceContext.spanId());
request.setAttribute(TracingAttributesFilter.class.getName() + "parentId", traceContext.parentId());
}
}
}
chain.doFilter(request, response);
}
}
from logback-access-spring-boot-starter.
Related Issues (20)
- java.lang.NoSuchMethodError: 'java.util.SortedMap kotlin.collections.MapsKt.sortedMapOf(java.util.Comparator, kotlin.Pair[])' HOT 6
- config file not loading when configured in application.properties HOT 4
- support slf4j (log4j2) HOT 3
- java.lang.NoSuchMethodError: 'java.util.SortedMap kotlin.collections.MapsKt.sortedMapOf(java.util.Comparator, kotlin.Pair[])' HOT 4
- Invalid characters in HTTP requests can cause NullPointerException in the access logs HOT 2
- Support Spring Boot 3 HOT 9
- The request body is being printed out only if the content type is set to application/x-www-form-urlencoded. Not otherwise.
- Hashing remote user HOT 1
- JSON Access log Logging HOT 2
- Jetty 12 Compatibility (Spring Boot 3.2.x) HOT 2
- support async appenders
- Support logback-access v2.0.0
- maybe make `spring-boot-starter-web` optional dependency HOT 2
- spring boot 2.2.2.RELEASE not work HOT 5
- Actuator on different port not logging HOT 2
- Add support for Undertow reactive web server
- Starter does not configure Tomcat when a servletContainer bean is configured
- Spring Boot Embedded Tomcat NullPointerException get-serverName HOT 3
- Options for working around LOGBACK-1526 in 3.x version of this lib HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logback-access-spring-boot-starter.