Skip to content

Commit

Permalink
10432: Use CloudWatch instead of SE Cluster;
Browse files Browse the repository at this point in the history
  • Loading branch information
John Cruz committed Aug 14, 2024
1 parent 53d6ff7 commit e65b8f1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
43 changes: 17 additions & 26 deletions web-api/src/applicationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { Case } from '../../shared/src/business/entities/cases/Case';
import { CaseDeadline } from '../../shared/src/business/entities/CaseDeadline';
import { Client } from '@opensearch-project/opensearch';
import { CloudWatchClient } from '@aws-sdk/client-cloudwatch';
import { CognitoIdentityProvider } from '@aws-sdk/client-cognito-identity-provider';
import { Correspondence } from '../../shared/src/business/entities/Correspondence';
import { DocketEntry } from '../../shared/src/business/entities/DocketEntry';
Expand Down Expand Up @@ -80,7 +81,7 @@ import sass from 'sass';

let sqsCache: SQSClient;
let searchClientCache: Client;
let searchInfoClientCache: Client;
let cloudWatchClientCache: CloudWatchClient;

const entitiesByName = {
Case,
Expand Down Expand Up @@ -140,6 +141,21 @@ export const createApplicationContext = (
return await getChromiumBrowserAWS();
}
},
getCloudWatchClient: () => {
if (cloudWatchClientCache) return cloudWatchClientCache;

if (environment.stage === 'local') {
cloudWatchClientCache = {
send: (...args) => console.log('** SYSTEM PERFORMANCE LOGS', ...args),
} as CloudWatchClient;
} else {
cloudWatchClientCache = new CloudWatchClient({
region: environment.region,
});
}

return cloudWatchClientCache;
},
getCognito: (): CognitoIdentityProvider => {
if (environment.stage === 'local') {
return getLocalCognito();
Expand Down Expand Up @@ -189,31 +205,6 @@ export const createApplicationContext = (
},
getEnvironment,
getHttpClient: () => axios,
getInfoSearchClient: () => {
if (searchInfoClientCache) return searchInfoClientCache;
if (
environment.stage === 'local' ||
!environment.elasticsearchInfoEndpoint
) {
searchInfoClientCache = {
index: (...args) =>
console.log('** SYSTEM PERFORMANCE LOGS', ...args),
} as Client;
} else {
searchInfoClientCache = new Client({
...AwsSigv4Signer({
getCredentials: () => {
const credentialsProvider = defaultProvider();
return credentialsProvider();
},
region: 'us-east-1',
}),
node: `https://${environment.elasticsearchInfoEndpoint}:443`,
});
}

return searchInfoClientCache;
},
getIrsSuperuserEmail: () => process.env.IRS_SUPERUSER_EMAIL,
getMessageGateway: () => ({
sendEmailEventToQueue: async ({ applicationContext, emailParams }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {
MetricDatum,
PutMetricDataCommand,
PutMetricDataCommandInput,
StandardUnit,
} from '@aws-sdk/client-cloudwatch';
import { ServerApplicationContext } from '@web-api/applicationContext';
import { createISODateString } from '@shared/business/utilities/DateHandler';

export const saveSystemPerformanceData = async ({
applicationContext,
Expand All @@ -13,12 +18,35 @@ export const saveSystemPerformanceData = async ({
email: string;
};
}) => {
const client = applicationContext.getInfoSearchClient();
await client.index({
body: {
...performanceData,
date: createISODateString(),
const { stage } = applicationContext.getEnvironment();

const cloudwatchClient = applicationContext.getCloudWatchClient();
const metricData: MetricDatum[] = [
{
Dimensions: [
{ Name: 'SequenceName', Value: performanceData.sequenceName },
],
MetricName: 'SequenceDuration',
Unit: 'Seconds',
Value: performanceData.duration,
},
index: 'system-performance-logs',
});
...performanceData.actionPerformanceArray.map(action => ({
Dimensions: [
{ Name: 'SequenceName', Value: performanceData.sequenceName },
{ Name: 'ActionName', Value: action.actionName },
],
MetricName: 'ActionPerformance',
Unit: 'Seconds' as StandardUnit,
Value: action.duration,
})),
];

const params: PutMetricDataCommandInput = {
MetricData: metricData,
Namespace: `System-Performance-Log-${stage}`,
};

const command = new PutMetricDataCommand(params);

await cloudwatchClient.send(command);
};

0 comments on commit e65b8f1

Please sign in to comment.