-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathServiceContext+TraceId.swift
More file actions
50 lines (46 loc) · 1.7 KB
/
ServiceContext+TraceId.swift
File metadata and controls
50 lines (46 loc) · 1.7 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright SwiftAWSLambdaRuntime project authors
// Copyright (c) Amazon.com, Inc. or its affiliates.
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import ServiceContextModule
// MARK: - ServiceContext integration
/// A ``ServiceContextKey`` for the AWS X-Ray trace ID.
///
/// This allows downstream libraries that depend on `swift-service-context`
/// (but not on `AWSLambdaRuntime`) to access the current trace ID via
/// `ServiceContext.current?.traceID`.
private enum LambdaTraceIDKey: ServiceContextKey {
typealias Value = String
static var nameOverride: String? { AmazonHeaders.traceID }
}
extension ServiceContext {
/// The AWS X-Ray trace ID for the current Lambda invocation, if available.
///
/// This value is automatically set by the Lambda runtime before calling the handler
/// and is available to all code running within the handler's async task tree.
///
/// Downstream libraries can read this without depending on `AWSLambdaRuntime`:
/// ```swift
/// if let traceID = ServiceContext.current?.traceID {
/// // propagate traceID to outgoing HTTP requests, etc.
/// }
/// ```
public var traceID: String? {
get {
self[LambdaTraceIDKey.self]
}
set {
self[LambdaTraceIDKey.self] = newValue
}
}
}