Skip to content

Commit 9343cac

Browse files
committed
improve
1 parent c0f1afb commit 9343cac

File tree

9 files changed

+33
-28
lines changed

9 files changed

+33
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
- Attach MDC properties to logs as attributes ([#4786](https://github.com/getsentry/sentry-java/pull/4786))
88
- MDC properties set using supported logging frameworks (Logback, Log4j2, java.util.Logging) are now attached to structured logs as attributes.
99
- This means that you will be able to filter/aggregate logs in the product based on these properties.
10-
- All properties are sent. To avoid sending certain properties, use the `beforeSendLog` hook. You can also set up [Advanced Data Scrubbing rules](https://docs.sentry.io/security-legal-pii/scrubbing/advanced-datascrubbing/) to redact sensitive information.
10+
- Only properties with keys matching the configured `contextTags` are sent as attributes. This is consistent with how MDC properties are applied as tags to events.
11+
- To avoid sending certain properties, use the `beforeSendLog` hook. You can also set up [Advanced Data Scrubbing rules](https://docs.sentry.io/security-legal-pii/scrubbing/advanced-datascrubbing/) to redact sensitive information.
1112
- Note that keys containing spaces are not supported.
1213

1314
### Fixes

sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ protected void captureLog(@NotNull LogRecord loggingEvent) {
161161
attributes.add(SentryAttribute.stringAttribute("sentry.message.template", message));
162162
}
163163

164-
final Map<String, String> mdcProperties = MDC.getMDCAdapter().getCopyOfContextMap();
164+
final @Nullable Map<String, String> mdcProperties = MDC.getMDCAdapter().getCopyOfContextMap();
165165
if (mdcProperties != null) {
166-
LoggerPropertiesUtil.applyPropertiesToAttributes(attributes, mdcProperties);
166+
final List<String> contextTags = ScopesAdapter.getInstance().getOptions().getContextTags();
167+
LoggerPropertiesUtil.applyPropertiesToAttributes(attributes, contextTags, mdcProperties);
167168
}
168169

169170
final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);

sentry-jul/src/test/kotlin/io/sentry/jul/SentryHandlerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ class SentryHandlerTest {
573573
assertEquals("testing MDC properties in logs", log.body)
574574
val attributes = log.attributes!!
575575
assertEquals("someValue", attributes["someTag"]?.value)
576-
assertEquals("otherValue", attributes["otherTag"]?.value)
576+
assertNull(attributes["otherTag"])
577577
}
578578
)
579579
}

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,9 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
231231
SentryAttribute.stringAttribute("sentry.message.template", nonFormattedMessage));
232232
}
233233

234-
final Map<String, String> contextData = loggingEvent.getContextData().toMap();
235-
if (contextData != null) {
236-
LoggerPropertiesUtil.applyPropertiesToAttributes(attributes, contextData);
237-
}
234+
final @NotNull Map<String, String> contextData = loggingEvent.getContextData().toMap();
235+
final @NotNull List<String> contextTags = ScopesAdapter.getInstance().getOptions().getContextTags();
236+
LoggerPropertiesUtil.applyPropertiesToAttributes(attributes, contextTags, contextData);
238237

239238
final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);
240239
params.setOrigin("auto.log.log4j2");
@@ -278,16 +277,12 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
278277
event.setExtra("marker", loggingEvent.getMarker().toString());
279278
}
280279

281-
final Map<String, String> contextData =
282-
CollectionUtils.filterMapEntries(
283-
loggingEvent.getContextData().toMap(), entry -> entry.getValue() != null);
284-
if (!contextData.isEmpty()) {
280+
final Map<String, String> contextData = loggingEvent.getContextData().toMap();
281+
if (contextData != null) {
285282
// get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been
286283
// initialized somewhere else
287-
final List<String> contextTags = scopes.getOptions().getContextTags();
288-
if (contextTags != null) {
289-
LoggerPropertiesUtil.applyPropertiesToEvent(event, contextTags, contextData);
290-
}
284+
final @NotNull List<String> contextTags = scopes.getOptions().getContextTags();
285+
LoggerPropertiesUtil.applyPropertiesToEvent(event, contextTags, contextData);
291286
// put the rest of mdc tags in contexts
292287
if (!contextData.isEmpty()) {
293288
event.getContexts().put("Context Data", contextData);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ class SentryAppenderTest {
610610
assertEquals("testing MDC properties in logs", log.body)
611611
val attributes = log.attributes!!
612612
assertEquals("someValue", attributes["someTag"]?.value)
613-
assertEquals("otherValue", attributes["otherTag"]?.value)
613+
assertNull(attributes["otherTag"])
614614
}
615615
)
616616
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,9 @@ protected void captureLog(@NotNull ILoggingEvent loggingEvent) {
187187
arguments = loggingEvent.getArgumentArray();
188188
}
189189

190-
final Map<String, String> mdcProperties = loggingEvent.getMDCPropertyMap();
191-
if (mdcProperties != null) {
192-
LoggerPropertiesUtil.applyPropertiesToAttributes(attributes, mdcProperties);
193-
}
190+
final @NotNull Map<String, String> mdcProperties = loggingEvent.getMDCPropertyMap();
191+
final @NotNull List<String> contextTags = ScopesAdapter.getInstance().getOptions().getContextTags();
192+
LoggerPropertiesUtil.applyPropertiesToAttributes(attributes, contextTags, mdcProperties);
194193

195194
final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);
196195
params.setOrigin("auto.log.logback");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ class SentryAppenderTest {
838838
assertEquals("testing MDC properties in logs", log.body)
839839
val attributes = log.attributes!!
840840
assertEquals("someValue", attributes["someTag"]?.value)
841-
assertEquals("otherValue", attributes["otherTag"]?.value)
841+
assertNull(attributes["otherTag"])
842842
}
843843
)
844844
}

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7082,6 +7082,7 @@ public final class io/sentry/util/LogUtils {
70827082

70837083
public final class io/sentry/util/LoggerPropertiesUtil {
70847084
public fun <init> ()V
7085+
public static fun applyPropertiesToAttributes (Lio/sentry/SentryAttributes;Ljava/util/List;Ljava/util/Map;)V
70857086
public static fun applyPropertiesToAttributes (Lio/sentry/SentryAttributes;Ljava/util/Map;)V
70867087
public static fun applyPropertiesToEvent (Lio/sentry/SentryEvent;Ljava/util/List;Ljava/util/Map;)V
70877088
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Map;
88
import org.jetbrains.annotations.ApiStatus;
99
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
1011

1112
/** Utility class for applying logger properties (e.g. MDC) to Sentry events and log attributes. */
1213
@ApiStatus.Internal
@@ -37,18 +38,25 @@ public static void applyPropertiesToEvent(
3738
}
3839

3940
/**
40-
* Applies logger properties from a properties map to SentryAttributes for logs. Properties with
41-
* null values are filtered out.
41+
* Applies logger properties from a properties map to SentryAttributes for logs. Only the
42+
* properties with keys that are found in `targetKeys` will be applied as attributes. Properties
43+
* with null values are filtered out.
4244
*
4345
* @param attributes the SentryAttributes to add the properties to
46+
* @param targetKeys the list of property keys to apply as attributes
4447
* @param properties the properties map (e.g. MDC)
4548
*/
4649
@ApiStatus.Internal
4750
public static void applyPropertiesToAttributes(
48-
final @NotNull SentryAttributes attributes, final @NotNull Map<String, String> properties) {
49-
for (final Map.Entry<String, String> entry : properties.entrySet()) {
50-
if (entry.getValue() != null) {
51-
attributes.add(SentryAttribute.stringAttribute(entry.getKey(), entry.getValue()));
51+
final @NotNull SentryAttributes attributes,
52+
final @NotNull List<String> targetKeys,
53+
final @NotNull Map<String, String> properties) {
54+
if (!targetKeys.isEmpty() && !properties.isEmpty()) {
55+
for (final String key : targetKeys) {
56+
final @Nullable String value = properties.get(key);
57+
if (value != null) {
58+
attributes.add(SentryAttribute.stringAttribute(key, value));
59+
}
5260
}
5361
}
5462
}

0 commit comments

Comments
 (0)