-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
39 lines (31 loc) · 928 Bytes
/
handler.js
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
import AWSXRay from 'aws-xray-sdk-core'
import { slowFunction, fasterFunction, unreliableFunction } from './functions'
AWSXRay.enableManualMode()
const asyncSubsegment = async (name, parent, fn) => {
const subsegment = parent.addNewSubsegment(name)
try {
return await fn()
} catch (e) {
subsegment.addError(e)
throw e
} finally {
subsegment.close()
}
}
const fetchData = async (segment) => {
await asyncSubsegment('Slow Function', segment, slowFunction)
await asyncSubsegment('Faster Function', segment, fasterFunction)
return asyncSubsegment('Unreliable Function', segment, unreliableFunction)
}
export const hello = async (event, context, callback) => {
const segment = new AWSXRay.Segment('hello function')
try {
const data = await fetchData(segment)
callback(null, data)
} catch(err) {
segment.addError(err)
callback(err)
} finally {
segment.close()
}
}