-
-
Notifications
You must be signed in to change notification settings - Fork 372
feat: Add support for tags and trace context fields for termination watch #5561
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
Merged
itaybre
merged 12 commits into
main
from
itay/cocoa-412-add-missing-information-to-watchdog-termination-events-unified_tags_and_context
Jul 8, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fd665b4
Update changelog
itaybre 94151c2
feat: Add support for dist and environment fields for termination watch
itaybre 45b6943
feat: Add support for tags and context fields for termination watch
itaybre c66ca51
Fix renamed properties
itaybre 982ff9d
Update changelog
itaybre 8af02df
fix rebase conflicts
itaybre d81fe76
Remove trace context calls
itaybre fee90b8
Fix tests after removing traceContext call
itaybre 6415334
Separate extensions for encoding and decoding context, user, tags, an…
itaybre 4c91ccd
Remove TraceContext persistence
itaybre efdd102
Remove unnecessary sanitization step
itaybre 31469ba
Pr suggestions
itaybre 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
11 changes: 11 additions & 0 deletions
11
Sources/Swift/Persistence/SentryScopePersistentStore+Context.swift
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,11 @@ | ||
| @_implementationOnly import _SentryPrivate | ||
|
|
||
| extension SentryScopePersistentStore { | ||
| func encode(context: [String: [String: Any]]) -> Data? { | ||
| return encode(context, "context", true) | ||
| } | ||
|
|
||
| func decodeContext(from data: Data) -> [String: [String: Any]]? { | ||
| return decode(from: data, "context") | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
Sources/Swift/Persistence/SentryScopePersistentStore+Helper.swift
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,55 @@ | ||
| @_implementationOnly import _SentryPrivate | ||
|
|
||
| extension SentryScopePersistentStore { | ||
| func encode<T>(_ genericModel: T, _ name: String, _ sanitize: Bool = false) -> Data? { | ||
| var model: Any = genericModel | ||
| if sanitize { | ||
| // We need to check if the context is a valid JSON object before encoding it. | ||
| // Otherwise it will throw an unhandled `NSInvalidArgumentException` exception. | ||
| // The error handler is required but will never be executed due to Swift type safety. | ||
| guard let wrapped = genericModel as? [AnyHashable: Any], | ||
| let sanitizedModel = sentry_sanitize(wrapped) else { | ||
| SentrySDKLog.error("Failed to sanitize \(name), reason: \(name) is not valid json: \(genericModel)") | ||
| return nil | ||
| } | ||
| model = sanitizedModel | ||
| } | ||
|
|
||
| guard let data = SentrySerialization.data(withJSONObject: model) else { | ||
| SentrySDKLog.error("Failed to serialize \(name), reason: \(name) is not valid json: \(genericModel)") | ||
| return nil | ||
| } | ||
|
|
||
| return data | ||
| } | ||
|
|
||
| func decode<T>(from data: Data, _ name: String) -> [String: T]? { | ||
| guard let deserialized = SentrySerialization.deserializeDictionary(fromJsonData: data) else { | ||
| SentrySDKLog.error("Failed to deserialize \(name), reason: data is not valid json") | ||
| return nil | ||
| } | ||
|
|
||
| // `SentrySerialization` is a wrapper around `NSJSONSerialization` which returns any type of data (`id`). | ||
| // It is the casted to a `NSDictionary`, which is then casted to a `[AnyHashable: Any]` in Swift. | ||
| // | ||
| // The responsibility of validating and casting the deserialized data from any data to a dictionary is delegated | ||
| // to the `SentrySerialization` class. | ||
| // | ||
| // As this decode Tags method specifically returns a dictionary of strings, we need to ensure that | ||
| // each value is a string. | ||
| // | ||
| // If the deserialized value is not a string, something clearly went wrong and we should discard the data. | ||
|
|
||
| // Iterate through the deserialized dictionary and check if the type is a dictionary. | ||
| // When all values are strings, we can safely cast it to `T` without allocating | ||
| // additional memory (like when mapping values). | ||
| for (key, value) in deserialized { | ||
| guard value is T else { | ||
| SentrySDKLog.error("Failed to deserialize \(name), reason: value for key \(key) is not a valid string") | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| return deserialized as? [String: T] | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
Sources/Swift/Persistence/SentryScopePersistentStore+Tags.swift
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,11 @@ | ||
| @_implementationOnly import _SentryPrivate | ||
|
|
||
| extension SentryScopePersistentStore { | ||
itaybre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func encode(tags: [String: String]) -> Data? { | ||
| return encode(tags, "tags", false) | ||
| } | ||
|
|
||
| func decodeTags(from data: Data) -> [String: String]? { | ||
| return decode(from: data, "tags") | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
Sources/Swift/Persistence/SentryScopePersistentStore+User.swift
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,20 @@ | ||
| @_implementationOnly import _SentryPrivate | ||
|
|
||
| extension SentryScopePersistentStore { | ||
| func encode(user: User) -> Data? { | ||
| guard let data = SentrySerialization.data(withJSONObject: user.serialize()) else { | ||
| SentrySDKLog.error("Failed to serialize user, reason: user is not valid json: \(user)") | ||
| return nil | ||
| } | ||
| return data | ||
| } | ||
|
|
||
| func decodeUser(from data: Data) -> User? { | ||
| return decoderUserHelper(data) | ||
| } | ||
|
|
||
| // Swift compiler can't infer T, even if I try to cast it | ||
itaybre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private func decoderUserHelper(_ data: Data) -> UserDecodable? { | ||
| return decodeFromJSONData(jsonData: data) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.