-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathTracingSupport.swift
More file actions
82 lines (67 loc) · 2.27 KB
/
TracingSupport.swift
File metadata and controls
82 lines (67 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Logging
import NIOConcurrencyHelpers
import NIOCore
import NIOHTTP1
import NIOSSL
import Tracing
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
// MARK: - Centralized span attribute handling
@usableFromInline
struct TracingSupport {
@inlinable
static func handleResponseStatusCode(
_ span: Span,
_ status: HTTPResponseStatus,
keys: HTTPClient.TracingConfiguration.AttributeKeys
) {
if status.code >= 400 {
span.setStatus(.init(code: .error))
}
span.attributes[keys.responseStatusCode] = SpanAttribute.int64(Int64(status.code))
}
@inlinable
static func sanitizePath(_ path: String, redactionComponents: Set<String>) -> String {
redactionComponents.reduce(path) { path, component in
path.replacingOccurrences(of: component, with: "REDACTED")
}
}
@inlinable
static func sanitizeQuery(_ query: String, redactionComponents: Set<String>) -> String {
query.components(separatedBy: "&").map {
let nameAndValue = $0
.trimmingCharacters(in: .whitespaces)
.components(separatedBy: "=")
if redactionComponents.contains(nameAndValue[0]) {
return "\(nameAndValue[0])=REDACTED"
}
return $0
}.joined(separator: "&")
}
}
// MARK: - HTTPHeadersInjector
struct HTTPHeadersInjector: Injector, @unchecked Sendable {
static let shared: HTTPHeadersInjector = HTTPHeadersInjector()
private init() {}
func inject(_ value: String, forKey name: String, into headers: inout HTTPHeaders) {
headers.add(name: name, value: value)
}
}
// MARK: - Errors
internal struct HTTPRequestCancellationError: Error {}