Git Product home page Git Product logo

Comments (6)

akkinoc avatar akkinoc commented on June 1, 2024 2

Thank you for your reply.
I would like to consider supporting this feature in future versions!

from logback-access-spring-boot-starter.

DarkAtra avatar DarkAtra commented on June 1, 2024 1

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.

akkinoc avatar akkinoc commented on June 1, 2024 1

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.

  1. 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);
    }

}
  1. 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;
}
  1. 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.

akkinoc avatar akkinoc commented on June 1, 2024

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.

DarkAtra avatar DarkAtra commented on June 1, 2024

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.

DarkAtra avatar DarkAtra commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.