Skip to content
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

Servlet and Webflux Tracing are Inconsistent in 3.0 #33495

Closed
braunsonm opened this issue Dec 8, 2022 · 7 comments
Closed

Servlet and Webflux Tracing are Inconsistent in 3.0 #33495

braunsonm opened this issue Dec 8, 2022 · 7 comments
Assignees
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid

Comments

@braunsonm
Copy link

We use a mix of Servlet and Webflux spring applications. In Spring Boot 3.0 there are differences in how traces are logged and how they are actually recorded on the tracing backend. The logging differences makes it especially difficult to use tools like Grafana and associate trace IDs from Loki since the position of that trace ID seems to move depending on the app type.

I have created a simple sample to reproduce this issue here: https://github.com/braunsonm/spring-inconsistency-sample. In this sample the two applications are identical except one is servlet and the other is webflux. The sample endpoint uses a RestTemplate and WebClient respectively to call https://google.com to generate a simple external source in our traces. This was generated using https://start.spring.io on Spring Boot 3.0

I'll go through the differences now:

Logging

In Servlet, you must use logging.pattern.level as described in the docs to include the trace and span IDs in your logs. I am using %5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}] as recommended from the docs. This creates a log like so:

2022-12-08T14:31:33.019 DEBUG [,59da07ccc24890648d97903cc81dcf6b,2d70739e9632e97f] 41859 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

However in Webflux, you are unable to get your trace and span IDs even with the recommended setting. You get a log line like so:

2022-12-08T15:02:10.916 DEBUG [,,] 49485 --- [or-http-epoll-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : [3b1900d8-1] Completed 200 OK

Notice how neither the trace nor the span are set in the log line. However it still does upload the span to the tracing backend.

Traces

The trace that is uploaded in Servlet looks how you would expect, a single trace with multiple spans.

image

In Webflux, multiple traces are submitted for the same request, when only separate spans should be created. This makes the correlation unusable as you can't tell which trace for the security filters is actually part of the request you are looking for.

image

(This is just 1 REST call)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 8, 2022
@bclozel
Copy link
Member

bclozel commented Dec 9, 2022

Thanks for the details report @braunsonm

I think the logging section of this issue is a duplicate of #33372, see the discussion there. We're aware that the situation is different from what Sleuth did, but there are many things to consider, especially performance. See #33372 (comment) and reactor/reactor-core#3149.

As for the Traces part, I think that this is due to two things:

As a first step @braunsonm, could you try creating the following bean and see how things behave in your sample application? This would replicate the fix to come with #33444.

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public ServerHttpObservationFilter webfluxObservationFilter(ObservationRegistry registry) {
	return new ServerHttpObservationFilter(registry);
}

@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Dec 9, 2022
@braunsonm
Copy link
Author

braunsonm commented Dec 9, 2022

@bclozel Adding the ServerHttpObservationFilter seems to have no effect on the trace output. Here is with the bean added:
image

I haven't had much luck getting the trace and span to appear in the logs like discussed at #33372 and I share the concerns that this would require invasive code changes in our codebases. The solutions provided are able to add context around specific sections of the reactor pipeline but it does not solve the areas I posted above which come from Spring itself, like the logging that occurs when going through the security filter chain, I would expect those to log with the trace ID not only my own logging.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 9, 2022
@jzheaux
Copy link
Contributor

jzheaux commented Dec 9, 2022

@bclozel I will try out the sample with Spring Security looking up the parent observation and report back.

@belovaf
Copy link

belovaf commented Dec 25, 2022

I have exactly the same issue with webflux applications.
All spring security spans are uncorrelated and have different trace ids.

@belovaf
Copy link

belovaf commented Dec 25, 2022

@jzheaux
After some research I found causes and propose a solution:

  1. Both ObservationAuthenticationManager and ObservationReactiveAuthorizationManager create observation without parent. We can wrap their methods with Mono.deferContextual() and manually set parent from context by key ObservationThreadLocalAccessor.KEY.
  2. ObservationWebFilterChainDecorator creates all observations without parent. We can apply same fix here.

I tested locally these fixes and all work fine.

@janchristian-haddorp
Copy link

Just to add here... The issue of "not connected traces" can be easily reproduced with a simple Spring Boot / Spring Security application (as shown in #33719).

Looking forward to an update and/or fix.

@philwebb philwebb added for: team-attention An issue we'd like other members of the team to review and removed for: team-attention An issue we'd like other members of the team to review labels Jan 10, 2023
@bclozel
Copy link
Member

bclozel commented Jan 11, 2023

Thanks for the analysis @belovaf !
I'm closing this issue in favor of spring-projects/spring-security#12524

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jan 11, 2023
@bclozel bclozel added status: invalid An issue that we don't feel is valid for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

7 participants