-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathhandler.js
More file actions
84 lines (79 loc) · 2.21 KB
/
handler.js
File metadata and controls
84 lines (79 loc) · 2.21 KB
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
const { CloudWatchClient, PutMetricDataCommand } = require("@aws-sdk/client-cloudwatch");
const axios = require("axios");
exports.http = async (event, context, callback) => {
const output = {};
const cloudwatch = new CloudWatchClient({});
const endpoints = event;
console.log('endpoints:', endpoints);
await Promise.all(endpoints.map(async (endpoint) => {
console.log(`Requesting ${endpoint}`);
let statusCode = 0;
let durationMS = 0;
let httpError = null;
const start = Date.now();
try {
const response = await axios.get(endpoint, { timeout: 10000 });
statusCode = response.status;
durationMS = Date.now() - start;
} catch (error) {
httpError = error.code || error.message;
statusCode = 0;
durationMS = 0;
}
output[endpoint] = {
HTTPError: httpError,
statusCode,
durationMS,
};
console.log(`${endpoint} : ${JSON.stringify(output[endpoint])}`);
// Push metrics to CloudWatch
const params = {
Namespace: 'Lambda-Ping/HTTP',
MetricData: [
{
MetricName: 'StatusCode',
Dimensions: [
{
Name: 'Endpoint',
Value: endpoint,
},
],
StatisticValues: {
SampleCount: 1,
Sum: statusCode,
Minimum: 0,
Maximum: 1000,
},
Unit: 'None',
},
{
MetricName: 'Latency',
Dimensions: [
{
Name: 'Endpoint',
Value: endpoint,
},
],
StatisticValues: {
SampleCount: 1,
Sum: durationMS,
Minimum: 0,
Maximum: 30000,
},
Unit: 'Milliseconds',
},
],
};
try {
await cloudwatch.send(new PutMetricDataCommand(params));
console.log(`Logged metrics in CloudWatch at: ${params.Namespace}`);
} catch (err) {
console.log("Unexpected issue posting metrics to CloudWatch");
console.log(err);
}
}));
// Log the finalised output object, as well as returning it to the requester.
console.log("Final results:");
console.log(JSON.stringify(output));
callback(null, output);
};