Skip to content

Commit

Permalink
Merge pull request #556 from beethovenxfi/v3-canary
Browse files Browse the repository at this point in the history
V3 canary
  • Loading branch information
franzns authored Dec 5, 2023
2 parents c16cbb9 + d6f20d1 commit 6d2eaea
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 All @@ -31,6 +33,7 @@ async function runIfNotAlreadyRunning(
res.sendStatus(200);
return;
}
const startJobTime = moment();
try {
runningJobs.add(jobId);

Expand All @@ -44,13 +47,17 @@ 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) {
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 6d2eaea

Please sign in to comment.