-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.swift
54 lines (45 loc) · 1.99 KB
/
main.swift
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
51
52
53
54
//===----------------------------------------------------------------------===//
//
// This source file is part of the aws-xray-sdk-swift open source project
//
// Copyright (c) 2020 pokryfka and the aws-xray-sdk-swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import AWSLambdaEvents
import AWSLambdaRuntime
import AWSXRaySDK
import NIO // usleep
// TODO: Implement AWS plugins https://github.com/pokryfka/aws-xray-sdk-swift/issues/26
private var metadata: XRayRecorder.Segment.Metadata? = {
// let metadataKeys: [AWSLambdaEnv] = [.functionName, .funtionVersion, .memorySizeInMB]
// let metadataKeyValues = zip(metadataKeys, metadataKeys.map(\.value))
// .filter { $0.1 != nil }.map { ($0.0.rawValue, AnyEncodable($0.1)) }
// return XRayRecorder.Segment.Metadata(uniqueKeysWithValues: metadataKeyValues)
nil
}()
private class ExampleLambdaHandler: EventLoopLambdaHandler {
typealias In = Cloudwatch.ScheduledEvent
typealias Out = Void
// TODO: existing Lambda API does not allow us to pass EventLoopGroup used by Lambda Handler
private let recorder = XRayRecorder()
deinit {
recorder.shutdown()
}
private func doWork(on eventLoop: EventLoop) -> EventLoopFuture<Void> {
eventLoop.submit { usleep(100_000) }.map { _ in }
}
func handle(context: Lambda.Context, event: In) -> EventLoopFuture<Void> {
let traceContext: XRayRecorder.TraceContext = (try? .init(tracingHeader: context.traceID)) ?? .init()
return recorder.segment(name: "ExampleLambdaHandler", context: traceContext, metadata: metadata) {
self.doWork(on: context.eventLoop)
}.flatMap { // TODO: flash also, in fact especially when thare are errors, see testPropagatingError
self.recorder.flush(on: context.eventLoop)
}
}
}
Lambda.run(ExampleLambdaHandler())