Skip to content

Commit c0f1afb

Browse files
committed
improve
1 parent 0ee7e50 commit c0f1afb

File tree

6 files changed

+70
-70
lines changed

6 files changed

+70
-70
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import io.sentry.protocol.Message;
2323
import io.sentry.protocol.SdkVersion;
2424
import io.sentry.util.CollectionUtils;
25-
import io.sentry.util.ContextTagsUtil;
25+
import io.sentry.util.LoggerPropertiesUtil;
2626
import java.text.MessageFormat;
2727
import java.util.ArrayList;
2828
import java.util.Date;
@@ -163,7 +163,7 @@ protected void captureLog(@NotNull LogRecord loggingEvent) {
163163

164164
final Map<String, String> mdcProperties = MDC.getMDCAdapter().getCopyOfContextMap();
165165
if (mdcProperties != null) {
166-
ContextTagsUtil.applyContextTagsToLogAttributes(attributes, mdcProperties);
166+
LoggerPropertiesUtil.applyPropertiesToAttributes(attributes, mdcProperties);
167167
}
168168

169169
final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);
@@ -318,7 +318,7 @@ SentryEvent createEvent(final @NotNull LogRecord record) {
318318
// get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been
319319
// initialized somewhere else
320320
final List<String> contextTags = ScopesAdapter.getInstance().getOptions().getContextTags();
321-
ContextTagsUtil.applyContextTagsToEvent(event, contextTags, mdcProperties);
321+
LoggerPropertiesUtil.applyPropertiesToEvent(event, contextTags, mdcProperties);
322322
// put the rest of mdc tags in contexts
323323
if (!mdcProperties.isEmpty()) {
324324
event.getContexts().put("MDC", mdcProperties);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import io.sentry.protocol.Message;
2626
import io.sentry.protocol.SdkVersion;
2727
import io.sentry.util.CollectionUtils;
28-
import io.sentry.util.ContextTagsUtil;
28+
import io.sentry.util.LoggerPropertiesUtil;
2929
import java.util.Arrays;
3030
import java.util.Collections;
3131
import java.util.List;
@@ -233,7 +233,7 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
233233

234234
final Map<String, String> contextData = loggingEvent.getContextData().toMap();
235235
if (contextData != null) {
236-
ContextTagsUtil.applyContextTagsToLogAttributes(attributes, contextData);
236+
LoggerPropertiesUtil.applyPropertiesToAttributes(attributes, contextData);
237237
}
238238

239239
final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);
@@ -286,7 +286,7 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
286286
// initialized somewhere else
287287
final List<String> contextTags = scopes.getOptions().getContextTags();
288288
if (contextTags != null) {
289-
ContextTagsUtil.applyContextTagsToEvent(event, contextTags, contextData);
289+
LoggerPropertiesUtil.applyPropertiesToEvent(event, contextTags, contextData);
290290
}
291291
// put the rest of mdc tags in contexts
292292
if (!contextData.isEmpty()) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import io.sentry.protocol.Message;
3030
import io.sentry.protocol.SdkVersion;
3131
import io.sentry.util.CollectionUtils;
32-
import io.sentry.util.ContextTagsUtil;
32+
import io.sentry.util.LoggerPropertiesUtil;
3333
import java.nio.charset.StandardCharsets;
3434
import java.util.Arrays;
3535
import java.util.Collections;
@@ -153,7 +153,7 @@ protected void append(@NotNull ILoggingEvent eventObject) {
153153
// get tags from ScopesAdapter options to allow getting the correct tags if Sentry has been
154154
// initialized somewhere else
155155
final List<String> contextTags = ScopesAdapter.getInstance().getOptions().getContextTags();
156-
ContextTagsUtil.applyContextTagsToEvent(event, contextTags, mdcProperties);
156+
LoggerPropertiesUtil.applyPropertiesToEvent(event, contextTags, mdcProperties);
157157
// put the rest of mdc tags in contexts
158158
if (!mdcProperties.isEmpty()) {
159159
event.getContexts().put("MDC", mdcProperties);
@@ -189,7 +189,7 @@ protected void captureLog(@NotNull ILoggingEvent loggingEvent) {
189189

190190
final Map<String, String> mdcProperties = loggingEvent.getMDCPropertyMap();
191191
if (mdcProperties != null) {
192-
ContextTagsUtil.applyContextTagsToLogAttributes(attributes, mdcProperties);
192+
LoggerPropertiesUtil.applyPropertiesToAttributes(attributes, mdcProperties);
193193
}
194194

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

sentry/api/sentry.api

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6962,12 +6962,6 @@ public abstract interface class io/sentry/util/CollectionUtils$Predicate {
69626962
public abstract fun test (Ljava/lang/Object;)Z
69636963
}
69646964

6965-
public final class io/sentry/util/ContextTagsUtil {
6966-
public fun <init> ()V
6967-
public static fun applyContextTagsToEvent (Lio/sentry/SentryEvent;Ljava/util/List;Ljava/util/Map;)V
6968-
public static fun applyContextTagsToLogAttributes (Lio/sentry/SentryAttributes;Ljava/util/Map;)V
6969-
}
6970-
69716965
public final class io/sentry/util/DebugMetaPropertiesApplier {
69726966
public static field DEBUG_META_PROPERTIES_FILENAME Ljava/lang/String;
69736967
public fun <init> ()V
@@ -7086,6 +7080,12 @@ public final class io/sentry/util/LogUtils {
70867080
public static fun logNotInstanceOf (Ljava/lang/Class;Ljava/lang/Object;Lio/sentry/ILogger;)V
70877081
}
70887082

7083+
public final class io/sentry/util/LoggerPropertiesUtil {
7084+
public fun <init> ()V
7085+
public static fun applyPropertiesToAttributes (Lio/sentry/SentryAttributes;Ljava/util/Map;)V
7086+
public static fun applyPropertiesToEvent (Lio/sentry/SentryEvent;Ljava/util/List;Ljava/util/Map;)V
7087+
}
7088+
70897089
public final class io/sentry/util/MapObjectReader : io/sentry/ObjectReader {
70907090
public fun <init> (Ljava/util/Map;)V
70917091
public fun beginArray ()V

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

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.sentry.util;
2+
3+
import io.sentry.SentryAttribute;
4+
import io.sentry.SentryAttributes;
5+
import io.sentry.SentryEvent;
6+
import java.util.List;
7+
import java.util.Map;
8+
import org.jetbrains.annotations.ApiStatus;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
/** Utility class for applying logger properties (e.g. MDC) to Sentry events and log attributes. */
12+
@ApiStatus.Internal
13+
public final class LoggerPropertiesUtil {
14+
15+
/**
16+
* Applies logger properties from a map to a Sentry event as tags. Only the properties with keys
17+
* that are found in `targetKeys` will be applied as tags.
18+
*
19+
* @param event the Sentry event to add tags to
20+
* @param targetKeys the list of property keys to apply as tags
21+
* @param properties the properties map (e.g. MDC) - this map will be modified by removing
22+
* properties which were applied as tags
23+
*/
24+
@ApiStatus.Internal
25+
public static void applyPropertiesToEvent(
26+
final @NotNull SentryEvent event,
27+
final @NotNull List<String> targetKeys,
28+
final @NotNull Map<String, String> properties) {
29+
if (!targetKeys.isEmpty() && !properties.isEmpty()) {
30+
for (final String key : targetKeys) {
31+
if (properties.containsKey(key)) {
32+
event.setTag(key, properties.get(key));
33+
properties.remove(key);
34+
}
35+
}
36+
}
37+
}
38+
39+
/**
40+
* Applies logger properties from a properties map to SentryAttributes for logs. Properties with
41+
* null values are filtered out.
42+
*
43+
* @param attributes the SentryAttributes to add the properties to
44+
* @param properties the properties map (e.g. MDC)
45+
*/
46+
@ApiStatus.Internal
47+
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()));
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)