-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
103 lines (91 loc) · 3.3 KB
/
index.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
console.log('----------------------------------------');
console.log('----------------------------------------');
console.log('----- MAKE SURE DAEMON IS RUNNING ----');
console.log('-------- npm run start-daemon --------');
console.log('----------------------------------------');
console.log('----------------------------------------');
const AWSXRay = require('aws-xray-sdk-core');
const { addPromiseSegment, addSegment } = require('../index.js');
const Enquirer = require('enquirer');
process.env.LAMBDA_TASK_ROOT = 'The taskiest root of them all';
async function runWithTracing(functionToHaveTraced) {
return new Promise(resolve => {
AWSXRay.setDaemonAddress('127.0.0.1:2000');
AWSXRay.enableAutomaticMode();
const segment = new AWSXRay.Segment('aws-xray-lambda-promise-subsegment');
const namespace = AWSXRay.getNamespace();
namespace.run(async function () {
AWSXRay.setSegment(segment);
console.log('segment', segment);
try {
await functionToHaveTraced(segment);
segment.close();
} catch (error) {
console.error(error);
segment.close(error);
}
resolve();
});
});
}
function addAnotherSubsegment(name, promiseFactory, parentSegment) {
return addPromiseSegment({
segmentName: name,
promiseFactory,
metadata: {
sampleMetadata: 'example'
},
annotations: {
sampleAnnotation: 'example'
},
parentSegment
});
}
async function nestedTracing() {
await runWithTracing(() => addAnotherSubsegment('layer 0', (parentSegmentOne) => new Promise(resolve => {
setTimeout(() => {
addAnotherSubsegment('layer 1', () => new Promise(innerResolveOne => {
setTimeout((parentSegmentTwo) =>
addAnotherSubsegment('layer 2', () => new Promise(innerResolveTwo => {
setTimeout(() => {
innerResolveTwo();
innerResolveOne();
resolve();
}, 300);
}), parentSegmentTwo), 300);
}), parentSegmentOne);
}, 300);
})));
}
async function basicTracing() {
await runWithTracing(() => addPromiseSegment({
segmentName: 'no metadata annotations parentSegment',
promiseFactory: () => new Promise(resolve => setTimeout(resolve, 300)),
__ignoreLambdaSafetyCheck: true
}));
}
(async function() {
const BASIC_EXAMPLE = 'Basic';
const NESTED_EXAMPLE = 'Nested';
const ADD_SEGMENT = 'addSegment';
enquirer = new Enquirer();
const response = await enquirer.prompt({
type: 'select',
name: 'example',
message: 'Which example?',
choices: [
BASIC_EXAMPLE,
NESTED_EXAMPLE,
ADD_SEGMENT
]
});
if(response.example === BASIC_EXAMPLE) {
return basicTracing();
} else if(response.example === NESTED_EXAMPLE) {
return nestedTracing();
} else if(response.example === ADD_SEGMENT) {
return runWithTracing(() => addSegment('add-segment-example', Promise.resolve({ all: 'of it works' })));
}
})()
.then(console.log)
.catch(console.error);