Skip to content

Commit

Permalink
Merge branch 'v3-canary' into cdk
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Dec 5, 2023
2 parents d5469a4 + d6f20d1 commit 6fb7790
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
63 changes: 63 additions & 0 deletions modules/metrics/cron-duration-metrics.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
CloudWatchClient,
CloudWatchClientConfig,
MetricDatum,
PutMetricDataCommand,
} from '@aws-sdk/client-cloudwatch';
import { env } from '../../app/env';

export interface NotificationsCloudwatchMetric {
merticData: MetricDatum[];
nameSpace: string;
}

export class CloudwatchCronDurationMetricsPublisher {
protected environment: string;
protected configuration?: CloudWatchClientConfig;
private client: CloudWatchClient;
public namespace: string;

constructor(
namespace: string = 'default',
environment: string = env.DEPLOYMENT_ENV,
configuration?: CloudWatchClientConfig,
) {
this.namespace = namespace;
this.environment = environment;
this.client = new CloudWatchClient({ region: env.AWS_REGION, ...configuration });
}

public async publish(metricName: string, count?: number): Promise<void> {
const [major, minor, patch] = process.versions.node.split('.').map(Number);
try {
const command = new PutMetricDataCommand({
MetricData: [
{
MetricName: metricName,
Dimensions: [
{
Name: 'Environment',
Value: this.environment,
},
{
Name: 'NodeVersion',
Value: major.toString(),
},
],
Unit: 'None',
Timestamp: new Date(),
Value: count ? count : 1,
},
],
Namespace: this.namespace,
});

await this.client.send(command);
} catch (err) {
// Sentry.captureException(`Failed to publish "${metricName}" in "${this.environment}: ${err}`);
console.log(`Failed to publish "${metricName}" in "${this.environment}: ${err}`);
}
}
}

export const cronsDurationMetricPublisher = new CloudwatchCronDurationMetricsPublisher('backendCronsDuration');
7 changes: 7 additions & 0 deletions worker/job-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { networkContext } from '../modules/network/network-context.service';
import { veBalService } from '../modules/vebal/vebal.service';
import { veBalVotingListService } from '../modules/vebal/vebal-voting-list.service';
import { cronsMetricPublisher } from '../modules/metrics/metrics.client';
import moment from 'moment';
import { cronsDurationMetricPublisher } from '../modules/metrics/cron-duration-metrics.client';

const runningJobs: Set<string> = new Set();

Expand Down Expand Up @@ -51,6 +53,7 @@ async function runIfNotAlreadyRunning(
tags: sentryTags,
});

const startJobTime = moment();
try {
runningJobs.add(jobId);

Expand All @@ -59,15 +62,19 @@ async function runIfNotAlreadyRunning(

await fn();

const durationSuccess = moment.duration(moment().diff(startJobTime)).asSeconds();
if (process.env.AWS_ALERTS === 'true') {
await cronsMetricPublisher.publish(`${jobId}-done`);
await cronsDurationMetricPublisher.publish(`${jobId}-done`, durationSuccess);
}
console.log(`Successful job ${jobId}-done`);
} catch (error) {
Sentry.captureException(error);

const durationError = moment.duration(moment().diff(startJobTime)).asSeconds();
if (process.env.AWS_ALERTS === 'true') {
await cronsMetricPublisher.publish(`${jobId}-error`);
await cronsDurationMetricPublisher.publish(`${jobId}-error`, durationError);
}
console.log(`Error job ${jobId}-error`, error);
next(error);
Expand Down

0 comments on commit 6fb7790

Please sign in to comment.