Skip to content

Commit

Permalink
Merge pull request #141 from Infomaniak/feat-forward-request-sentry
Browse files Browse the repository at this point in the history
feat: Forward network request id to sentry
  • Loading branch information
PhilippeWeidmann authored Nov 25, 2024
2 parents 57997b7 + b03a0fc commit b0f5bec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Sources/InfomaniakCore/Networking/ApiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ open class ApiFetcher {
let dataResponse = await validatedRequest.serializingDecodable(ApiResponse<T>.self,
automaticallyCancelling: true,
decoder: decoder).response

SentryDebug.httpResponseBreadcrumb(urlRequest: request.convertible.urlRequest, urlResponse: dataResponse.response)

return try handleApiResponse(dataResponse)
}

Expand Down
35 changes: 34 additions & 1 deletion Sources/InfomaniakCore/Tracking/SentryDebug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
import Foundation
import Sentry

enum SentryDebug {
public enum SentryDebug {
enum Category {
static let fileMetadata = "FileMetadata"
static let networking = "Networking"
}

// MARK: FileMetadata
Expand All @@ -33,4 +34,36 @@ enum SentryDebug {
breadcrumb.data = metadata
SentrySDK.addBreadcrumb(breadcrumb)
}

public static func httpRequestBreadcrumb(requestId: String?, url: URL, method: String, statusCode: Int?) {
let breadcrumb = Breadcrumb(level: .info, category: Category.networking)
breadcrumb.type = "http"
breadcrumb.message = ""

var data = [
"url": url.absoluteString,
"method": method,
"status_code": statusCode ?? -1
] as [String: Any]

if let requestId {
data["request_id"] = requestId
}

breadcrumb.data = data

SentrySDK.addBreadcrumb(breadcrumb)
}

public static func httpResponseBreadcrumb(urlRequest: URLRequest?, urlResponse: HTTPURLResponse?) {
guard let url = urlRequest?.url,
let method = urlRequest?.httpMethod else { return }

httpRequestBreadcrumb(
requestId: urlResponse?.value(forHTTPHeaderField: "x-request-id"),
url: url,
method: method,
statusCode: urlResponse?.statusCode
)
}
}

0 comments on commit b0f5bec

Please sign in to comment.