|
| 1 | +import * as awsLambda from 'aws-lambda'; |
| 2 | + |
| 3 | + |
| 4 | +import { getSecrets, getRedisConnection, getQueueDepth, publishMetrics } from './methods'; |
| 5 | + |
| 6 | + |
| 7 | +export interface Result { |
| 8 | + region: string; |
| 9 | + vpcId: string; |
| 10 | + count: number; |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +export const handler = async () => { |
| 15 | + try { |
| 16 | + const host = process.env.REDIS_ADDR; |
| 17 | + if (!host) { |
| 18 | + throw new Error('REDIS_ADDR environment variable not set'); |
| 19 | + } |
| 20 | + |
| 21 | + const portStr = process.env.REDIS_PORT; |
| 22 | + if (!portStr) { |
| 23 | + throw new Error('REDIS_PORT environment variable not set'); |
| 24 | + } |
| 25 | + const port = parseInt(portStr, 10); |
| 26 | + if (isNaN(port)) { |
| 27 | + throw new Error('REDIS_PORT environment variable does not parseInt'); |
| 28 | + } |
| 29 | + |
| 30 | + // parse queues |
| 31 | + const queuesBlob = process.env.QUEUES; |
| 32 | + if (!queuesBlob) { |
| 33 | + throw new Error('QUEUES environment variable not set'); |
| 34 | + } |
| 35 | + const queues: string[] = JSON.parse(queuesBlob); |
| 36 | + |
| 37 | + // get username/password |
| 38 | + const redisSecretArn = process.env.REDIS_SECRET_ARN; |
| 39 | + if (!redisSecretArn) { |
| 40 | + throw new Error('REDIS_SECRET_ARN environment variable not set'); |
| 41 | + } |
| 42 | + |
| 43 | + const redisSecretPasswordPath = process.env.REDIS_SECRET_PASSWORD_PATH; |
| 44 | + if (!redisSecretPasswordPath) { |
| 45 | + throw new Error('REDIS_SECRET_PASSWORD_PATH environment variable not set'); |
| 46 | + } |
| 47 | + |
| 48 | + const redisSecretUsernamePath = process.env.REDIS_SECRET_USERNAME_PATH; |
| 49 | + if (!redisSecretUsernamePath) { |
| 50 | + throw new Error('REDIS_SECRET_USERNAME_PATH environment variable not set'); |
| 51 | + } |
| 52 | + |
| 53 | + const { username, password } = await getSecrets({redisSecretArn, redisSecretPasswordPath, redisSecretUsernamePath }); |
| 54 | + |
| 55 | + // connect to redis instance, note this waits until the connection is 'ready'. |
| 56 | + const redisConnection = await getRedisConnection({ |
| 57 | + connectionName: 'queueDepthMetricPublisher', |
| 58 | + host, |
| 59 | + port, |
| 60 | + username, |
| 61 | + password, |
| 62 | + tls: {}, // always use tls, but be easy about it. |
| 63 | + }); |
| 64 | + |
| 65 | + // capture the values for the queues |
| 66 | + const results = await Promise.all(queues.map(async(queueName) => { |
| 67 | + let depth = await getQueueDepth({redisConnection, queueName}); |
| 68 | + if(!depth) { |
| 69 | + console.error(`Error fetching xlen for ${queueName}`); |
| 70 | + depth = -1; |
| 71 | + } |
| 72 | + return {queueName, depth}; |
| 73 | + })); |
| 74 | + |
| 75 | + if(results === undefined) { |
| 76 | + throw new Error('Error fetching ALL queue depths'); |
| 77 | + } |
| 78 | + |
| 79 | + // publish the metrics |
| 80 | + publishMetrics(results); |
| 81 | + |
| 82 | + |
| 83 | + } catch (error) { |
| 84 | + console.error('Error publishing metric data', error); |
| 85 | + throw error; |
| 86 | + } |
| 87 | +}; |
0 commit comments