-
-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix setting context tags on events captured by Spring (#2060)
* Fix setting context tags on events captured by Spring Fixes #2043 * Changelog. * Move ContextTagsEventProcessor from spring boot to spring module (#2065) * Fixes after merge Co-authored-by: Alexander Dinauer <adinauer@users.noreply.github.com> Co-authored-by: Alexander Dinauer <alexander.dinauer@sentry.io>
- Loading branch information
1 parent
6270232
commit 4835920
Showing
7 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
sentry-spring/src/main/java/io/sentry/spring/ContextTagsEventProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.sentry.spring; | ||
|
||
import io.sentry.EventProcessor; | ||
import io.sentry.Hint; | ||
import io.sentry.SentryEvent; | ||
import io.sentry.SentryOptions; | ||
import io.sentry.util.CollectionUtils; | ||
import java.util.Map; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.slf4j.MDC; | ||
|
||
/** | ||
* Attaches context tags defined in {@link SentryOptions#getContextTags()} from {@link MDC} to | ||
* {@link SentryEvent#getTags()}. | ||
*/ | ||
public final class ContextTagsEventProcessor implements EventProcessor { | ||
private final SentryOptions options; | ||
|
||
public ContextTagsEventProcessor(final @NotNull SentryOptions options) { | ||
this.options = options; | ||
} | ||
|
||
@Override | ||
public @NotNull SentryEvent process(@NotNull SentryEvent event, @Nullable Hint hint) { | ||
final Map<String, String> contextMap = MDC.getCopyOfContextMap(); | ||
if (contextMap != null) { | ||
final Map<String, String> mdcProperties = | ||
CollectionUtils.filterMapEntries(contextMap, entry -> entry.getValue() != null); | ||
if (!mdcProperties.isEmpty() && !options.getContextTags().isEmpty()) { | ||
for (final String contextTag : options.getContextTags()) { | ||
// if mdc tag is listed in SentryOptions, apply as event tag | ||
if (mdcProperties.containsKey(contextTag)) { | ||
event.setTag(contextTag, mdcProperties.get(contextTag)); | ||
} | ||
} | ||
} | ||
} | ||
return event; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
sentry-spring/src/test/kotlin/io/sentry/spring/ContextTagsEventProcessorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.sentry.spring | ||
|
||
import io.sentry.SentryEvent | ||
import io.sentry.SentryOptions | ||
import org.slf4j.MDC | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertTrue | ||
|
||
class ContextTagsEventProcessorTest { | ||
|
||
class Fixture { | ||
|
||
fun getSut(contextTags: List<String> = emptyList(), mdcTags: Map<String, String> = emptyMap()): ContextTagsEventProcessor { | ||
val options = SentryOptions().apply { contextTags.forEach { tag -> addContextTag(tag) } } | ||
val sut = ContextTagsEventProcessor(options) | ||
mdcTags.forEach { MDC.put(it.key, it.value) } | ||
return sut | ||
} | ||
} | ||
|
||
private val fixture = Fixture() | ||
|
||
@BeforeTest | ||
fun before() { | ||
MDC.clear() | ||
} | ||
|
||
@Test | ||
fun `does not copy tags if no tags are set on options`() { | ||
val sut = fixture.getSut() | ||
|
||
val result = sut.process(SentryEvent(), null) | ||
val tags = result.tags | ||
assertTrue(tags == null || tags.isEmpty()) | ||
} | ||
|
||
@Test | ||
fun `copies mdc tags`() { | ||
val sut = fixture.getSut(contextTags = listOf("user-id"), mdcTags = mapOf("user-id" to "user-id-value")) | ||
|
||
val result = sut.process(SentryEvent(), null) | ||
val tags = result.tags | ||
assertNotNull(tags) { | ||
assertTrue(it.containsKey("user-id")) | ||
assertEquals("user-id-value", it["user-id"]) | ||
} | ||
} | ||
|
||
@Test | ||
fun `does not copy tags not defined in options`() { | ||
val sut = fixture.getSut(contextTags = listOf("user-id"), mdcTags = mapOf("user-id" to "user-id-value", "request-id" to "request-id-value")) | ||
|
||
val result = sut.process(SentryEvent(), null) | ||
val tags = result.tags | ||
assertNotNull(tags) { | ||
assertTrue(it.containsKey("user-id")) | ||
assertFalse(it.containsKey("request-id")) | ||
} | ||
} | ||
|
||
@Test | ||
fun `does not copy tag not set in MDC`() { | ||
val sut = fixture.getSut(contextTags = listOf("user-id", "another-tag"), mdcTags = mapOf("user-id" to "user-id-value")) | ||
|
||
val result = sut.process(SentryEvent(), null) | ||
val tags = result.tags | ||
assertNotNull(tags) { | ||
assertTrue(it.containsKey("user-id")) | ||
assertFalse(it.containsKey("another-tag")) | ||
} | ||
} | ||
} |