Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sources/AWSLambdaRuntime/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public enum Lambda {
) async throws where Handler: StreamingLambdaHandler {
var handler = handler

let logGroupName = Lambda.env("AWS_LAMBDA_LOG_GROUP_NAME")
let logStreamName = Lambda.env("AWS_LAMBDA_LOG_STREAM_NAME")

do {
while !Task.isCancelled {

Expand Down Expand Up @@ -128,7 +131,9 @@ public enum Lambda {
deadline: LambdaClock.Instant(
millisecondsSinceEpoch: invocation.metadata.deadlineInMillisSinceEpoch
),
logger: requestLogger
logger: requestLogger,
logGroupName: logGroupName,
logStreamName: logStreamName
)
)
requestLogger.trace("Handler finished processing invocation")
Expand Down
36 changes: 30 additions & 6 deletions Sources/AWSLambdaRuntime/LambdaContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
let cognitoIdentity: String?
let clientContext: ClientContext?
let logger: Logger
let logGroupName: String
let logStreamName: String

init(
requestID: String,
Expand All @@ -108,7 +110,9 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
deadline: LambdaClock.Instant,
cognitoIdentity: String?,
clientContext: ClientContext?,
logger: Logger
logger: Logger,
logGroupName: String,
logStreamName: String
) {
self.requestID = requestID
self.traceID = traceID
Expand All @@ -118,6 +122,8 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
self.cognitoIdentity = cognitoIdentity
self.clientContext = clientContext
self.logger = logger
self.logGroupName = logGroupName
self.logStreamName = logStreamName
}
}

Expand Down Expand Up @@ -169,7 +175,7 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
*,
deprecated,
message:
"This method will be removed in a future major version update. Use init(requestID:traceID:tenantID:invokedFunctionARN:deadline:cognitoIdentity:clientContext:logger) instead."
"This method will be removed in a future major version update. Use init(requestID:traceID:tenantID:invokedFunctionARN:deadline:cognitoIdentity:clientContext:logger:logGroupName:logStreamName) instead."
)
public init(
requestID: String,
Expand Down Expand Up @@ -199,7 +205,9 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
deadline: LambdaClock.Instant,
cognitoIdentity: String? = nil,
clientContext: ClientContext? = nil,
logger: Logger
logger: Logger,
logGroupName: String? = nil,
logStreamName: String? = nil
) {
self.storage = _Storage(
requestID: requestID,
Expand All @@ -209,10 +217,22 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
deadline: deadline,
cognitoIdentity: cognitoIdentity,
clientContext: clientContext,
logger: logger
logger: logger,
logGroupName: logGroupName ?? "",
logStreamName: logStreamName ?? ""
)
}

/// The name of the Amazon CloudWatch Logs group for the function.
public var logGroupName: String {
self.storage.logGroupName
}

/// The name of the Amazon CloudWatch Logs stream for the current invocation of the function.
public var logStreamName: String {
self.storage.logStreamName
}

public func getRemainingTime() -> Duration {
let deadline = self.deadline
return LambdaClock().now.duration(to: deadline)
Expand All @@ -230,15 +250,19 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
tenantID: String?,
invokedFunctionARN: String,
timeout: Duration,
logger: Logger
logger: Logger,
logGroupName: String? = nil,
logStreamName: String? = nil
) -> LambdaContext {
LambdaContext(
requestID: requestID,
traceID: traceID,
tenantID: tenantID,
invokedFunctionARN: invokedFunctionARN,
deadline: LambdaClock().now.advanced(by: timeout),
logger: logger
logger: logger,
logGroupName: logGroupName,
logStreamName: logStreamName
)
}
}
62 changes: 62 additions & 0 deletions Tests/AWSLambdaRuntimeTests/LambdaContextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,66 @@ struct LambdaContextTests {
#expect(remainingTime <= Duration.seconds(31), "Remaining time should be approximately 30 seconds")
#expect(remainingTime >= Duration.seconds(-29), "Remaining time should be approximately -30 seconds")
}

@Test("logGroupName returns the value passed at initialization")
@available(LambdaSwift 2.0, *)
func logGroupNameReturnsInitializedValue() {
let context = LambdaContext.__forTestsOnly(
requestID: "test-request",
traceID: "test-trace",
tenantID: nil,
invokedFunctionARN: "test-arn",
timeout: .seconds(30),
logger: Logger(label: "test"),
logGroupName: "/aws/lambda/my-function"
)

#expect(context.logGroupName == "/aws/lambda/my-function")
}

@Test("logStreamName returns the value passed at initialization")
@available(LambdaSwift 2.0, *)
func logStreamNameReturnsInitializedValue() {
let context = LambdaContext.__forTestsOnly(
requestID: "test-request",
traceID: "test-trace",
tenantID: nil,
invokedFunctionARN: "test-arn",
timeout: .seconds(30),
logger: Logger(label: "test"),
logStreamName: "2024/01/01/[$LATEST]abcdef1234567890"
)

#expect(context.logStreamName == "2024/01/01/[$LATEST]abcdef1234567890")
}

@Test("logGroupName defaults to nil")
@available(LambdaSwift 2.0, *)
func logGroupNameDefaultsToNil() {
let context = LambdaContext.__forTestsOnly(
requestID: "test-request",
traceID: "test-trace",
tenantID: nil,
invokedFunctionARN: "test-arn",
timeout: .seconds(30),
logger: Logger(label: "test")
)

#expect(context.logGroupName == "")
}

@Test("logStreamName defaults to nil")
@available(LambdaSwift 2.0, *)
func logStreamNameDefaultsToNil() {
let context = LambdaContext.__forTestsOnly(
requestID: "test-request",
traceID: "test-trace",
tenantID: nil,
invokedFunctionARN: "test-arn",
timeout: .seconds(30),
logger: Logger(label: "test")
)

#expect(context.logStreamName == "")
}
}
Loading