This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
71 lines (62 loc) · 1.79 KB
/
index.ts
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
import { Callback, Context, Handler } from 'aws-lambda';
import * as AWS from 'aws-sdk';
import fetch from 'node-fetch';
const cloudwatch = new AWS.CloudWatch();
const sidekiqUrl = process.env.SIDEKIQ_URL;
const sidekiqUsername = '';
const sidekiqPassword = process.env.SIDEKIQ_PASSWORD;
const appName = process.env.APP_NAME || 'unknown';
export const logMetric = async (attr: string, value: number) => {
const params: AWS.CloudWatch.PutMetricDataInput = {
MetricData: [
{
Dimensions: [
{
Name: 'App',
Value: appName,
},
],
MetricName: attr,
Timestamp: new Date(),
Unit: 'Count',
Value: value,
},
],
Namespace: 'Sidekiq',
};
return cloudwatch.putMetricData(params).promise();
};
export const fetchSidekiqData = async (url: string) => {
return fetch(url, {
headers: {
Accept: 'application/json',
Authorization: `Basic ${Buffer.from(
`${sidekiqUsername}:${sidekiqPassword}`,
).toString('base64')}`,
},
});
};
export const handler: Handler = async (
_event: unknown,
_context: Context,
_callback: Callback | undefined,
) => {
if (sidekiqUrl === undefined) {
console.log('No SIDEKIQ_URL set');
return;
}
const response = await fetchSidekiqData(sidekiqUrl);
if (!response.ok) {
console.log('Error:', response.status, response.statusText);
return;
}
const stats = await response.json();
await Promise.all([
logMetric('Enqueued', stats.sidekiq.enqueued),
logMetric('Busy', stats.sidekiq.busy),
logMetric('Retries', stats.sidekiq.retries),
logMetric('Processes', stats.sidekiq.processes),
logMetric('Scheduled', stats.sidekiq.scheduled),
logMetric('DefaultLatency', stats.sidekiq.default_latency),
]);
};