-
-
Notifications
You must be signed in to change notification settings - Fork 383
feat(network-details): Introduce data classes for extracting network details to session replay #7582
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
43jay
wants to merge
1
commit into
mobile-935/ios-swift-app
Choose a base branch
from
mobile-935/data-classes
base: mobile-935/ios-swift-app
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.
+158
β7
Open
feat(network-details): Introduce data classes for extracting network details to session replay #7582
Changes from all commits
Commits
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
155 changes: 155 additions & 0 deletions
155
Sources/Swift/Integrations/SessionReplay/SentryReplayNetworkDetails.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,155 @@ | ||
| import Foundation | ||
|
|
||
| /// Warning codes for network body capture issues. | ||
| enum NetworkBodyWarning: String { | ||
| case jsonTruncated = "JSON_TRUNCATED" | ||
| case textTruncated = "TEXT_TRUNCATED" | ||
| case invalidJson = "INVALID_JSON" | ||
| case bodyParseError = "BODY_PARSE_ERROR" | ||
| } | ||
|
|
||
| /// Main container for network request/response tracking. | ||
| /// | ||
| /// ObjC callers (SentryNetworkTracker) create this object and populate it | ||
| /// via `setRequest`/`setResponse`. Swift callers (SentrySRDefaultBreadcrumbConverter) | ||
| /// consume it via `serialize()`. | ||
| @objc | ||
| @_spi(Private) public class SentryReplayNetworkDetails: NSObject { | ||
|
|
||
| // MARK: - Nested Types (Swift-only) | ||
|
|
||
| /// Typed representation of captured body content. | ||
| enum BodyContent { | ||
| /// Parsed JSON body (dictionary or array). | ||
| case json(Any) | ||
| /// Text body (plain text, HTML, XML, etc.). | ||
| case text(String) | ||
|
|
||
| init(_ value: Any) { | ||
| if let string = value as? String { | ||
| self = .text(string) | ||
| } else { | ||
| self = .json(value) | ||
| } | ||
| } | ||
|
|
||
| var serializedValue: Any { | ||
| switch self { | ||
| case .json(let value): return value | ||
| case .text(let string): return string | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Captured request or response body with optional parsing warnings. | ||
| struct Body { | ||
| let content: BodyContent | ||
| let warnings: [NetworkBodyWarning] | ||
|
|
||
| init(content: Any, warnings: [NetworkBodyWarning] = []) { | ||
| self.content = BodyContent(content) | ||
| self.warnings = warnings | ||
| } | ||
|
|
||
| func serialize() -> [String: Any] { | ||
| var result = [String: Any]() | ||
| result["body"] = content.serializedValue | ||
| if !warnings.isEmpty { | ||
| result["warnings"] = warnings.map(\.rawValue) | ||
| } | ||
| return result | ||
| } | ||
| } | ||
|
|
||
| /// Captured HTTP request or response details (size, body, headers). | ||
| struct Detail { | ||
| let size: NSNumber? | ||
| let body: Body? | ||
| let headers: [String: String] | ||
|
|
||
| func serialize() -> [String: Any] { | ||
| var result = [String: Any]() | ||
| if let size { result["size"] = size } | ||
| if let body { result["body"] = body.serialize() } | ||
| result["headers"] = headers | ||
| return result | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Properties | ||
|
|
||
| /// Key used to store network details in breadcrumb data dictionary. | ||
| @objc public static let replayNetworkDetailsKey = "_networkDetails" | ||
|
|
||
| private(set) var method: String? | ||
| private(set) var statusCode: NSNumber? | ||
| private(set) var request: Detail? | ||
| private(set) var response: Detail? | ||
|
|
||
| /// Request body size in bytes, derived from request details. | ||
| var requestBodySize: NSNumber? { request?.size } | ||
|
|
||
| /// Response body size in bytes, derived from response details. | ||
| var responseBodySize: NSNumber? { response?.size } | ||
|
|
||
| // MARK: - Initialization | ||
|
|
||
| /// Creates a new instance with the given HTTP method. | ||
| @objc | ||
| public init(method: String?) { | ||
| self.method = method | ||
| super.init() | ||
| } | ||
|
|
||
| // MARK: - ObjC Setters | ||
|
|
||
| /// Sets request details from raw components. | ||
| /// | ||
| /// - Parameters: | ||
| /// - size: Request body size in bytes, or nil if unknown. | ||
| /// - body: Pre-parsed body content (dictionary, array, or string), or nil if not captured. | ||
| /// - headers: Filtered HTTP request headers. | ||
| @objc | ||
| public func setRequest(size: NSNumber?, body: Any?, headers: [String: String]) { | ||
| self.request = Detail( | ||
| size: size, | ||
| body: body.map { Body(content: $0) }, | ||
| headers: headers | ||
| ) | ||
| } | ||
|
|
||
| /// Sets response details from raw components. | ||
| /// | ||
| /// - Parameters: | ||
| /// - statusCode: HTTP status code. | ||
| /// - size: Response body size in bytes, or nil if unknown. | ||
| /// - body: Pre-parsed body content (dictionary, array, or string), or nil if not captured. | ||
| /// - headers: Filtered HTTP response headers. | ||
| @objc | ||
| public func setResponse(statusCode: Int, size: NSNumber?, body: Any?, headers: [String: String]) { | ||
| self.statusCode = NSNumber(value: statusCode) | ||
| self.response = Detail( | ||
| size: size, | ||
| body: body.map { Body(content: $0) }, | ||
| headers: headers | ||
| ) | ||
| } | ||
|
|
||
| // MARK: - Serialization | ||
|
|
||
| /// Serializes to dictionary for inclusion in breadcrumb data. | ||
| public func serialize() -> [String: Any] { | ||
| var result = [String: Any]() | ||
| if let method { result["method"] = method } | ||
| if let statusCode { result["statusCode"] = statusCode } | ||
| if let requestBodySize { result["requestBodySize"] = requestBodySize } | ||
| if let responseBodySize { result["responseBodySize"] = responseBodySize } | ||
| if let request { result["request"] = request.serialize() } | ||
| if let response { result["response"] = response.serialize() } | ||
| return result | ||
| } | ||
|
|
||
| public override var description: String { | ||
| "SentryReplayNetworkDetails: \(serialize())" | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: Are these strings defined anywhere?If they, can you add a link to the doc for future references?