15
15
import Dispatch
16
16
import Logging
17
17
import NIO
18
+ import BaggageContext
18
19
19
20
// MARK: - InitializationContext
20
21
@@ -49,12 +50,21 @@ extension Lambda {
49
50
extension Lambda {
50
51
/// Lambda runtime context.
51
52
/// The Lambda runtime generates and passes the `Context` to the Lambda handler as an argument.
52
- public final class Context : CustomDebugStringConvertible {
53
+ public final class Context : BaggageContext . Context , CustomDebugStringConvertible {
54
+
55
+ /// Contains contextual metadata such as request and trace identifiers, along with other information which may
56
+ /// be carried throughout asynchronous and cross-node boundaries (e.g. through HTTPClient calls).
57
+ public let baggage : Baggage
58
+
53
59
/// The request ID, which identifies the request that triggered the function invocation.
54
- public let requestID : String
60
+ public var requestID : String {
61
+ self . baggage. lambdaRequestID
62
+ }
55
63
56
64
/// The AWS X-Ray tracing header.
57
- public let traceID : String
65
+ public var traceID : String {
66
+ self . baggage. lambdaTraceID
67
+ }
58
68
59
69
/// The ARN of the Lambda function, version, or alias that's specified in the invocation.
60
70
public let invokedFunctionARN : String
@@ -68,10 +78,13 @@ extension Lambda {
68
78
/// For invocations from the AWS Mobile SDK, data about the client application and device.
69
79
public let clientContext : String ?
70
80
71
- /// `Logger` to log with
81
+ /// `Logger` to log with, it is automatically populated with `baggage` information (such as `traceID` and `requestID`).
72
82
///
73
83
/// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable.
74
- public let logger : Logger
84
+ public var logger : Logger {
85
+ self . _logger. with ( self . baggage)
86
+ }
87
+ private var _logger : Logger
75
88
76
89
/// The `EventLoop` the Lambda is executed on. Use this to schedule work with.
77
90
/// This is useful when implementing the `EventLoopLambdaHandler` protocol.
@@ -93,20 +106,18 @@ extension Lambda {
93
106
logger: Logger ,
94
107
eventLoop: EventLoop ,
95
108
allocator: ByteBufferAllocator ) {
96
- self . requestID = requestID
97
- self . traceID = traceID
109
+ var baggage = Baggage . background
110
+ baggage. lambdaRequestID = requestID
111
+ baggage. lambdaTraceID = traceID
112
+ self . baggage = baggage
98
113
self . invokedFunctionARN = invokedFunctionARN
99
114
self . cognitoIdentity = cognitoIdentity
100
115
self . clientContext = clientContext
101
116
self . deadline = deadline
102
117
// utility
103
118
self . eventLoop = eventLoop
104
119
self . allocator = allocator
105
- // mutate logger with context
106
- var logger = logger
107
- logger [ metadataKey: " awsRequestID " ] = . string( requestID)
108
- logger [ metadataKey: " awsTraceID " ] = . string( traceID)
109
- self . logger = logger
120
+ self . _logger = logger
110
121
}
111
122
112
123
public func getRemainingTime( ) -> TimeAmount {
@@ -146,3 +157,43 @@ extension Lambda {
146
157
}
147
158
}
148
159
}
160
+
161
+ // MARK: - Baggage Items
162
+
163
+ extension Baggage {
164
+
165
+ // MARK: - Baggage: RequestID
166
+
167
+ enum LambdaRequestIDKey : Key {
168
+ typealias Value = String
169
+ static var name : String ? { AmazonHeaders . requestID }
170
+ }
171
+
172
+ /// The request ID, which identifies the request that triggered the function invocation.
173
+ public internal( set) var lambdaRequestID : String {
174
+ get {
175
+ return self [ LambdaRequestIDKey . self] ! // !-safe, the runtime guarantees to always set an identifier, even in testing
176
+ }
177
+ set {
178
+ self [ LambdaRequestIDKey . self] = newValue
179
+ }
180
+ }
181
+
182
+ // MARK: - Baggage: TraceID
183
+
184
+ enum LambdaTraceIDKey : Key {
185
+ typealias Value = String
186
+ static var name : String ? { AmazonHeaders . traceID }
187
+ }
188
+
189
+ /// The AWS X-Ray tracing header.
190
+ public internal( set) var lambdaTraceID : String {
191
+ get {
192
+ return self [ LambdaTraceIDKey . self] ! // !-safe, the runtime guarantees to always set an identifier, even in testing
193
+ }
194
+ set {
195
+ self [ LambdaTraceIDKey . self] = newValue
196
+ }
197
+ }
198
+
199
+ }
0 commit comments