Skip to content

Commit c1f694f

Browse files
Add default value for traceID header (#246)
* Add default value for traceID header * Implement Invocation traceID test
1 parent d06d22c commit c1f694f

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

Sources/AWSLambdaRuntimeCore/ControlPlaneRequest.swift

+1-5
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,10 @@ struct Invocation: Hashable {
5151
throw Lambda.RuntimeError.invocationMissingHeader(AmazonHeaders.invokedFunctionARN)
5252
}
5353

54-
guard let traceID = headers.first(name: AmazonHeaders.traceID) else {
55-
throw Lambda.RuntimeError.invocationMissingHeader(AmazonHeaders.traceID)
56-
}
57-
5854
self.requestID = requestID
5955
self.deadlineInMillisSinceEpoch = unixTimeInMilliseconds
6056
self.invokedFunctionARN = invokedFunctionARN
61-
self.traceID = traceID
57+
self.traceID = headers.first(name: AmazonHeaders.traceID) ?? "Root=\(AmazonHeaders.generateXRayTraceID());Sampled=0"
6258
self.clientContext = headers["Lambda-Runtime-Client-Context"].first
6359
self.cognitoIdentity = headers["Lambda-Runtime-Cognito-Identity"].first
6460
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
@testable import AWSLambdaRuntimeCore
16+
import NIOHTTP1
17+
import XCTest
18+
19+
class InvocationTest: XCTestCase {
20+
func testInvocationTraceID() throws {
21+
let headers = HTTPHeaders([
22+
(AmazonHeaders.requestID, "test"),
23+
(AmazonHeaders.deadline, String(Date(timeIntervalSinceNow: 60).millisSinceEpoch)),
24+
(AmazonHeaders.invokedFunctionARN, "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime"),
25+
])
26+
27+
var invocation: Invocation?
28+
29+
XCTAssertNoThrow(invocation = try Invocation(headers: headers))
30+
XCTAssertNotNil(invocation)
31+
32+
guard !invocation!.traceID.isEmpty else {
33+
XCTFail("Invocation traceID is empty")
34+
return
35+
}
36+
}
37+
}

scripts/soundness.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
1919

2020
function replace_acceptable_years() {
2121
# this needs to replace all acceptable forms with 'YEARS'
22-
sed -e 's/2017-2018/YEARS/' -e 's/2017-2020/YEARS/' -e 's/2017-2021/YEARS/' -e 's/2020-2021/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' -e 's/2021/YEARS/'
22+
sed -e 's/2017-2018/YEARS/' -e 's/2017-2020/YEARS/' -e 's/2017-2021/YEARS/' -e 's/2020-2021/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' -e 's/2021/YEARS/' -e 's/2022/YEARS/'
2323
}
2424

2525
printf "=> Checking for unacceptable language... "

0 commit comments

Comments
 (0)