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

Fix setting context tags on events captured by Spring #2060

Merged
merged 5 commits into from
May 24, 2022
Merged

Conversation

maciejwalkowiak
Copy link
Contributor

@maciejwalkowiak maciejwalkowiak commented May 20, 2022

📜 Description

Fixes setting context tags on events triggered by SentryExceptionResolver.

💡 Motivation and Context

Fixes #2043

💚 How did you test it?

📝 Checklist

  • I reviewed the submitted code
  • I added tests to verify the changes
  • I updated the docs if needed
  • No breaking changes

@Override
public @NotNull SentryEvent process(
@NotNull SentryEvent event, @Nullable Map<String, Object> hint) {
final Map<String, String> contextMap = MDC.getCopyOfContextMap();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could filter only to events with mechanism type HandlerExceptionResolver, otherwise for events send from logback this logic will be applied twice.

On the other hand, if we don't filter by mechanism type, context tags are also applied to events triggered manually with Sentry.captureException().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it bad if direct captures also get the MDC context added to the tags?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also filter by event.getLogger since it sets event.setLogger(record.getLoggerName()); for JUL and Logback, but IMO, we should add MDC anyways, if they don't want, they won't add in the options.getContextTags() in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if i am getting it right, we are keeping it as it is, correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but that's just my opinion, I know that we should always be skeptical about adding a lot of tags since high cardinality data is more expensive, so please double-check with @bruno-garcia

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep it this way to add MDC tags regardless of logging framework.

The way it is implemented it'll just override existing tags and should just work or de we expect any problems regarding MDC being thread local?

I think we also need PRs #2052 , #2054 and #2057 in case we're not dealing with spring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone does not use Spring Boot Starter, then they have to configure Logback explicitly in logback.xml - meaning they have to provide configuration - so it's somewhat expected that context tags set in logback.xml are taken into account. Then I believe we do not need any other change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed today with @maciejwalkowiak we do want #2052, #2054 and #2057 in order to support Sentry.init calls.

@maciejwalkowiak maciejwalkowiak marked this pull request as ready for review May 20, 2022 07:18
* Attaches context tags defined in {@link SentryOptions#getContextTags()} from {@link MDC} to
* {@link SentryEvent#getTags()}.
*/
public final class ContextTagsEventProcessor implements EventProcessor {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this event processor live in the sentry-spring module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It put it to sentry-spring-boot-starter because it needs slf4j and it is provided only in that module. If we decide to move it somewhere else and add there sl4fj perhaps it could be sentry module as there is nothing really Spring specific here.

@marandaneto
Copy link
Contributor

This PR fixes #2043 but only if you're using the sentry-spring-boot-starter module, if you use sentry-spring or the sentry-logback and sentry-log4j2 that won't work, am I right?

@adinauer
Copy link
Member

This PR fixes #2043 but only if you're using the sentry-spring-boot-starter module, if you use sentry-spring or the sentry-logback and sentry-log4j2 that won't work, am I right?

Yes, as @maciejwalkowiak mentioned above we expect devs to use logback.xml or similar config files when not dealing with spring. For spring (non boot) I wouldn't mind the slf4j dependency.

@adinauer
Copy link
Member

Opened #2065 to move it into the spring module.

@codecov-commenter
Copy link

Codecov Report

❗ No coverage uploaded for pull request base (6.x.x@6270232). Click here to learn what that means.
The diff coverage is n/a.

@@           Coverage Diff            @@
##             6.x.x    #2060   +/-   ##
========================================
  Coverage         ?   81.07%           
  Complexity       ?     3217           
========================================
  Files            ?      230           
  Lines            ?    11819           
  Branches         ?     1572           
========================================
  Hits             ?     9582           
  Misses           ?     1650           
  Partials         ?      587           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6270232...b06ccbc. Read the comment docs.

@adinauer adinauer merged commit 4835920 into 6.x.x May 24, 2022
@adinauer adinauer deleted the gh-2043 branch May 24, 2022 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants