Skip to content

Commit 7755376

Browse files
committed
adopts baggage context; readonly context
1 parent b9224e2 commit 7755376

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

Package.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ let package = Package(
2020
dependencies: [
2121
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.17.0")),
2222
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.0.0")),
23+
// .package(url: "https://github.com/slashmo/gsoc-swift-baggage-context.git", .branch("main")),
24+
.package(name: "swift-context", path: "/Users/ktoso/code/gsoc-swift-baggage-context"),
2325
.package(url: "https://github.com/swift-server/swift-backtrace.git", .upToNextMajor(from: "1.1.0")),
2426
],
2527
targets: [
@@ -30,6 +32,7 @@ let package = Package(
3032
]),
3133
.target(name: "AWSLambdaRuntimeCore", dependencies: [
3234
.product(name: "Logging", package: "swift-log"),
35+
.product(name: "BaggageContext", package: "swift-context"),
3336
.product(name: "Backtrace", package: "swift-backtrace"),
3437
.product(name: "NIOHTTP1", package: "swift-nio"),
3538
]),

Sources/AWSLambdaRuntimeCore/LambdaContext.swift

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import Dispatch
1616
import Logging
1717
import NIO
18+
import BaggageContext
1819

1920
// MARK: - InitializationContext
2021

@@ -49,12 +50,21 @@ extension Lambda {
4950
extension Lambda {
5051
/// Lambda runtime context.
5152
/// 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+
5359
/// 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+
}
5563

5664
/// The AWS X-Ray tracing header.
57-
public let traceID: String
65+
public var traceID: String {
66+
self.baggage.lambdaTraceID
67+
}
5868

5969
/// The ARN of the Lambda function, version, or alias that's specified in the invocation.
6070
public let invokedFunctionARN: String
@@ -68,10 +78,13 @@ extension Lambda {
6878
/// For invocations from the AWS Mobile SDK, data about the client application and device.
6979
public let clientContext: String?
7080

71-
/// `Logger` to log with
81+
/// `Logger` to log with, it is automatically populated with `baggage` information (such as `traceID` and `requestID`).
7282
///
7383
/// - 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
7588

7689
/// The `EventLoop` the Lambda is executed on. Use this to schedule work with.
7790
/// This is useful when implementing the `EventLoopLambdaHandler` protocol.
@@ -93,20 +106,18 @@ extension Lambda {
93106
logger: Logger,
94107
eventLoop: EventLoop,
95108
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
98113
self.invokedFunctionARN = invokedFunctionARN
99114
self.cognitoIdentity = cognitoIdentity
100115
self.clientContext = clientContext
101116
self.deadline = deadline
102117
// utility
103118
self.eventLoop = eventLoop
104119
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
110121
}
111122

112123
public func getRemainingTime() -> TimeAmount {
@@ -146,3 +157,43 @@ extension Lambda {
146157
}
147158
}
148159
}
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

Comments
 (0)