-
Notifications
You must be signed in to change notification settings - Fork 133
/
HTTPHeadersWriter.swift
83 lines (74 loc) · 3.19 KB
/
HTTPHeadersWriter.swift
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
83
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-Present Datadog, Inc.
*/
import Foundation
/// The `HTTPHeadersWriter` class facilitates the injection of trace propagation headers into network requests
/// targeted at a backend instrumented with Datadog and expecting `x-datadog-*` headers.
///
/// Usage:
///
/// var request = URLRequest(...)
///
/// let writer = HTTPHeadersWriter()
/// let span = Tracer.shared().startRootSpan(operationName: "network request")
/// Tracer.shared().inject(spanContext: span.context, writer: writer)
///
/// writer.traceHeaderFields.forEach { (field, value) in
/// request.setValue(value, forHTTPHeaderField: field)
/// }
///
/// // call span.finish() when the request completes
///
public class HTTPHeadersWriter: TracePropagationHeadersWriter {
/// A dictionary containing the required HTTP Headers for propagating trace information.
///
/// Usage:
///
/// writer.traceHeaderFields.forEach { (field, value) in
/// request.setValue(value, forHTTPHeaderField: field)
/// }
///
public private(set) var traceHeaderFields: [String: String] = [:]
/// The tracing sampler.
///
/// This value will decide of the `x-datadog-sampling-priority` header field value
/// and if `x-datadog-trace-id` and `x-datadog-parent-id` are propagated.
private let sampler: Sampler
/// Initializes the headers writer.
///
/// - Parameter samplingRate: The sampling rate applied for headers injection.
@available(*, deprecated, message: "This will be removed in future versions of the SDK. Use `init(sampleRate:)` instead.")
public convenience init(samplingRate: Float) {
self.init(sampleRate: samplingRate)
}
/// Initializes the headers writer.
///
/// - Parameter sampleRate: The sampling rate applied for headers injection, with 20% as the default.
public convenience init(sampleRate: Float = 20) {
self.init(sampler: Sampler(samplingRate: sampleRate))
}
/// Initializes the headers writer.
///
/// - Parameter sampler: The sampler used for headers injection.
public init(sampler: Sampler) {
self.sampler = sampler
}
/// Writes the trace ID, span ID, and optional parent span ID into the trace propagation headers.
///
/// - Parameter traceID: The trace ID.
/// - Parameter spanID: The span ID.
/// - Parameter parentSpanID: The parent span ID, if applicable.
public func write(traceID: TraceID, spanID: SpanID, parentSpanID: SpanID?) {
let samplingPriority = sampler.sample()
traceHeaderFields = [
TracingHTTPHeaders.samplingPriorityField: samplingPriority ? "1" : "0"
]
if samplingPriority {
traceHeaderFields[TracingHTTPHeaders.traceIDField] = traceID.idLoHex
traceHeaderFields[TracingHTTPHeaders.parentSpanIDField] = String(spanID, representation: .hexadecimal)
traceHeaderFields[TracingHTTPHeaders.tagsField] = "_dd.p.tid=\(traceID.idHiHex)"
}
}
}