Skip to content

Commit c3bf08c

Browse files
committed
more fixes
1 parent 5ca0e5a commit c3bf08c

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
285285
// get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been
286286
// initialized somewhere else
287287
final List<String> contextTags = scopes.getOptions().getContextTags();
288-
LoggerPropertiesUtil.applyPropertiesToEvent(event, contextTags, contextData);
288+
LoggerPropertiesUtil.applyPropertiesToEvent(event, contextTags, contextData, "Context Data");
289289
}
290290

291291
return event;

sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ class SentryAppenderTest {
334334

335335
verify(fixture.transport)
336336
.send(
337-
checkEvent { event -> assertEquals(mapOf("key" to "value"), event.contexts["MDC"]) },
337+
checkEvent { event ->
338+
assertEquals(mapOf("key" to "value"), event.contexts["Context Data"])
339+
},
338340
anyOrNull(),
339341
)
340342
}
@@ -349,7 +351,7 @@ class SentryAppenderTest {
349351
verify(fixture.transport)
350352
.send(
351353
checkEvent { event ->
352-
assertEquals(mapOf("key" to "value"), event.contexts["MDC"])
354+
assertEquals(mapOf("key" to "value"), event.contexts["Context Data"])
353355
assertEquals(mapOf("contextTag1" to "contextTag1Value"), event.tags)
354356
},
355357
anyOrNull(),
@@ -366,7 +368,7 @@ class SentryAppenderTest {
366368
verify(fixture.transport)
367369
.send(
368370
checkEvent { event ->
369-
assertNotNull(event.contexts["MDC"]) {
371+
assertNotNull(event.contexts["Context Data"]) {
370372
val contextData = it as Map<*, *>
371373
assertNull(contextData["key1"])
372374
assertEquals("value", contextData["key2"])
@@ -384,7 +386,7 @@ class SentryAppenderTest {
384386
logger.warn("testing MDC tags")
385387

386388
verify(fixture.transport)
387-
.send(checkEvent { event -> assertNull(event.contexts["MDC"]) }, anyOrNull())
389+
.send(checkEvent { event -> assertNull(event.contexts["Context Data"]) }, anyOrNull())
388390
}
389391

390392
@Test
@@ -393,7 +395,10 @@ class SentryAppenderTest {
393395
logger.warn("testing without MDC tags")
394396

395397
verify(fixture.transport)
396-
.send(checkEvent { event -> assertFalse(event.contexts.containsKey("MDC")) }, anyOrNull())
398+
.send(
399+
checkEvent { event -> assertFalse(event.contexts.containsKey("Context Data")) },
400+
anyOrNull(),
401+
)
397402
}
398403

399404
@Test
@@ -593,7 +598,6 @@ class SentryAppenderTest {
593598
@Test
594599
fun `sets properties from ThreadContext as attributes on logs`() {
595600
val logger = fixture.getSut(minimumLevel = Level.INFO, contextTags = listOf("someTag"))
596-
ScopesAdapter.getInstance().options.logs.isEnabled = true
597601

598602
ThreadContext.put("someTag", "someValue")
599603
ThreadContext.put("otherTag", "otherValue")

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7091,6 +7091,7 @@ public final class io/sentry/util/LoggerPropertiesUtil {
70917091
public fun <init> ()V
70927092
public static fun applyPropertiesToAttributes (Lio/sentry/SentryAttributes;Ljava/util/List;Ljava/util/Map;)V
70937093
public static fun applyPropertiesToEvent (Lio/sentry/SentryEvent;Ljava/util/List;Ljava/util/Map;)V
7094+
public static fun applyPropertiesToEvent (Lio/sentry/SentryEvent;Ljava/util/List;Ljava/util/Map;Ljava/lang/String;)V
70947095
}
70957096

70967097
public final class io/sentry/util/MapObjectReader : io/sentry/ObjectReader {

sentry/src/main/java/io/sentry/util/LoggerPropertiesUtil.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ public final class LoggerPropertiesUtil {
1616
/**
1717
* Applies logger properties from a map to a Sentry event as tags and context. The properties that
1818
* have keys matching any of the `targetKeys` will be applied as tags, while the others will be
19-
* reported in the `MDC` context.
19+
* reported in an ad-hoc context.
2020
*
2121
* @param event the Sentry event to add tags to
2222
* @param targetKeys the list of property keys to apply as tags
2323
* @param properties the properties map (e.g. MDC) - this map will be modified by removing
2424
* properties which were applied as tags
25+
* @param contextName the name of the context to use for leftover properties
2526
*/
2627
@ApiStatus.Internal
2728
public static void applyPropertiesToEvent(
2829
final @NotNull SentryEvent event,
2930
final @NotNull List<String> targetKeys,
30-
final @NotNull Map<String, String> properties) {
31+
final @NotNull Map<String, String> properties,
32+
final @NotNull String contextName) {
3133
if (!targetKeys.isEmpty() && !properties.isEmpty()) {
3234
for (final String key : targetKeys) {
3335
final @Nullable String value = properties.remove(key);
@@ -37,10 +39,17 @@ public static void applyPropertiesToEvent(
3739
}
3840
}
3941
if (!properties.isEmpty()) {
40-
event.getContexts().put("MDC", properties);
42+
event.getContexts().put(contextName, properties);
4143
}
4244
}
4345

46+
public static void applyPropertiesToEvent(
47+
final @NotNull SentryEvent event,
48+
final @NotNull List<String> targetKeys,
49+
final @NotNull Map<String, String> properties) {
50+
applyPropertiesToEvent(event, targetKeys, properties, "MDC");
51+
}
52+
4453
/**
4554
* Applies logger properties from a properties map to SentryAttributes for logs. Only the
4655
* properties with keys that are found in `targetKeys` will be applied as attributes. Properties

0 commit comments

Comments
 (0)