-
-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Send GraphQL "operationName" in HTTP breadcrumbs (#3931)
This PR attempts to support sending GraphQL operation names with existing HTTP breadcrumbs. Co-authored-by: Max Chuquimia <> Co-authored-by: Philipp Hofmann <[email protected]>
- Loading branch information
1 parent
99ab5d0
commit cbd7725
Showing
12 changed files
with
198 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,19 @@ | ||
import Foundation | ||
|
||
public extension URLSessionTask { | ||
|
||
@objc | ||
func getGraphQLOperationName() -> String? { | ||
guard originalRequest?.value(forHTTPHeaderField: "Content-Type") == "application/json" else { return nil } | ||
guard let requestBody = originalRequest?.httpBody else { return nil } | ||
|
||
let requestInfo = try? JSONDecoder().decode(GraphQLRequest.self, from: requestBody) | ||
|
||
return requestInfo?.operationName | ||
} | ||
|
||
} | ||
|
||
private struct GraphQLRequest: Decodable { | ||
let operationName: String | ||
} |
This file contains 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 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 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
71 changes: 71 additions & 0 deletions
71
Tests/SentryTests/Swift/Extensions/URLSessionTaskTests.swift
This file contains 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,71 @@ | ||
import Foundation | ||
import Nimble | ||
@testable import Sentry | ||
import XCTest | ||
|
||
final class URLSessionTaskTests: XCTestCase { | ||
|
||
func testHTTPContentTypeInvalid() { | ||
let task = makeTask( | ||
headers: ["Content-Type": "image/jpeg"], | ||
body: "8J+YiQo=" | ||
) | ||
|
||
let operationName = task.getGraphQLOperationName() | ||
|
||
expect(operationName) == nil | ||
} | ||
|
||
func testHTTPBodyDataInvalid() { | ||
let task = makeTask( | ||
headers: ["Content-Type": "application/json"], | ||
body: "not json" | ||
) | ||
|
||
let operationName = task.getGraphQLOperationName() | ||
|
||
expect(operationName) == nil | ||
} | ||
|
||
func testHTTPBodyDataMissing() { | ||
let task = makeTask( | ||
headers: ["Content-Type": "application/json"], | ||
body: nil | ||
) | ||
|
||
let operationName = task.getGraphQLOperationName() | ||
|
||
expect(operationName) == nil | ||
} | ||
|
||
func testHTTPBodyDataValidGraphQL() { | ||
let task = makeTask( | ||
headers: ["Content-Type": "application/json"], | ||
body: """ | ||
{ | ||
"operationName": "MyOperation", | ||
"variables": { | ||
"id": "1234" | ||
}, | ||
"query": "query MyOperation($id: ID!) { node(id: $id) { id } }" | ||
} | ||
""" | ||
) | ||
|
||
let operationName = task.getGraphQLOperationName() | ||
|
||
expect(operationName) == "MyOperation" | ||
} | ||
|
||
} | ||
|
||
private extension URLSessionTaskTests { | ||
|
||
func makeTask(headers: [String: String], body: String?) -> URLSessionTask { | ||
var request = URLRequest(url: URL(string: "https://anything.com")!) | ||
request.httpBody = body?.data(using: .utf8) | ||
request.allHTTPHeaderFields = headers | ||
return URLSession(configuration: .ephemeral).dataTask(with: request) | ||
} | ||
|
||
} |