-
-
Notifications
You must be signed in to change notification settings - Fork 459
Attach MDC properties to logs as attributes #4786
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
Open
lcian
wants to merge
24
commits into
main
Choose a base branch
from
lcian/feat/mdc-logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−42
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8fe41f8
Attach MDC properties to logs as attributes
lcian b10a3b4
changelog
lcian dbfe2d3
format
lcian 6af6433
improve
lcian 8bc254d
improve
lcian 9438ad1
improve
lcian 0ee7e50
Merge branch 'main' into lcian/feat/mdc-logs
lcian c0f1afb
improve
lcian 9343cac
improve
lcian cb4c5e6
improve
lcian 38b07b7
improve
lcian 817e001
Merge branch 'main' into lcian/feat/mdc-logs
lcian 5a62c9f
add to context inside util function
lcian 6a310df
use Map.remove
lcian 4244da7
fix
lcian b0b0103
send mdc.<key> for logs
lcian 75d16bb
Merge branch 'main' into lcian/feat/mdc-logs
lcian 48b6105
Update CHANGELOG.md
lcian 9cecc5f
update tests with mdc prefix
lcian cc786e7
fix
lcian 5ca0e5a
Merge branch 'main' into lcian/feat/mdc-logs
lcian c3bf08c
more fixes
lcian 27f8e37
more fixes
lcian 887930b
formatting etc
lcian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
76 changes: 76 additions & 0 deletions
76
sentry/src/main/java/io/sentry/util/LoggerPropertiesUtil.java
This file contains hidden or 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.util; | ||
|
||
import io.sentry.SentryAttribute; | ||
import io.sentry.SentryAttributes; | ||
import io.sentry.SentryEvent; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** Utility class for applying logger properties (e.g. MDC) to Sentry events and log attributes. */ | ||
@ApiStatus.Internal | ||
public final class LoggerPropertiesUtil { | ||
|
||
/** | ||
* Applies logger properties from a map to a Sentry event as tags and context. The properties that | ||
* have keys matching any of the `targetKeys` will be applied as tags, while the others will be | ||
* reported in an ad-hoc context. | ||
* | ||
* @param event the Sentry event to add tags to | ||
* @param targetKeys the list of property keys to apply as tags | ||
* @param properties the properties map (e.g. MDC) - this map will be modified by removing | ||
* properties which were applied as tags | ||
* @param contextName the name of the context to use for leftover properties | ||
*/ | ||
@ApiStatus.Internal | ||
public static void applyPropertiesToEvent( | ||
final @NotNull SentryEvent event, | ||
final @NotNull List<String> targetKeys, | ||
final @NotNull Map<String, String> properties, | ||
final @NotNull String contextName) { | ||
if (!targetKeys.isEmpty() && !properties.isEmpty()) { | ||
for (final String key : targetKeys) { | ||
final @Nullable String value = properties.remove(key); | ||
if (value != null) { | ||
event.setTag(key, value); | ||
} | ||
} | ||
} | ||
if (!properties.isEmpty()) { | ||
event.getContexts().put(contextName, properties); | ||
} | ||
} | ||
lcian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public static void applyPropertiesToEvent( | ||
final @NotNull SentryEvent event, | ||
final @NotNull List<String> targetKeys, | ||
final @NotNull Map<String, String> properties) { | ||
applyPropertiesToEvent(event, targetKeys, properties, "MDC"); | ||
} | ||
|
||
/** | ||
* Applies logger properties from a properties map to SentryAttributes for logs. Only the | ||
* properties with keys that are found in `targetKeys` will be applied as attributes. Properties | ||
* with null values are filtered out. | ||
* | ||
* @param attributes the SentryAttributes to add the properties to | ||
* @param targetKeys the list of property keys to apply as attributes | ||
* @param properties the properties map (e.g. MDC) | ||
*/ | ||
@ApiStatus.Internal | ||
public static void applyPropertiesToAttributes( | ||
lcian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final @NotNull SentryAttributes attributes, | ||
final @NotNull List<String> targetKeys, | ||
final @NotNull Map<String, String> properties) { | ||
if (!targetKeys.isEmpty() && !properties.isEmpty()) { | ||
for (final String key : targetKeys) { | ||
final @Nullable String value = properties.get(key); | ||
if (value != null) { | ||
attributes.add(SentryAttribute.stringAttribute("mdc." + key, value)); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.