Skip to content

Commit

Permalink
Merge branch 'main' into cmpwrappers1
Browse files Browse the repository at this point in the history
  • Loading branch information
vlkerag committed Jan 17, 2024
2 parents cca3fea + 53fc759 commit 3e0053b
Show file tree
Hide file tree
Showing 32 changed files with 799 additions and 687 deletions.
15 changes: 12 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ module.exports = {
jest: true,
browser: true,
},
extends: ['@guardian/eslint-config-typescript'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'@guardian/eslint-config-typescript',
],
globals: { __PACKAGE_VERSION__: 'readonly' },
ignorePatterns: ['dist', 'coverage', 'cdk', 'monitoring', '*.ts', '*.*.ts'],
rules: {
// a lot of the 3rd party code uses these
'no-underscore-dangle': 0,
},

parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 6,
project: 'tsconfig.json',
sourceType: 'module',
},
overrides: [
{
// tests and config files sometimes do unorthodox things, and that's ok
files: ['**/*.test.*', '*.config.js'],
rules: {
'@typescript-eslint/no-unsafe-return': 0,
Expand Down
7 changes: 4 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ updates:
update-types:
- 'minor'
- 'patch'
ignore:
- dependency-name: 'inquirer'
- dependency-name: '@types/inquirer'
exclude-patterns:
- 'inquirer'
- '@types/inquirer'

- package-ecosystem: 'github-actions'
directory: '/'
schedule:
Expand Down
8 changes: 4 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"[typescript]": {
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.organizeImports": false
}
"source.organizeImports": "never"
}
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"source.fixAll.eslint": "explicit"
},
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.words": [
"SP’s",
Expand Down
10 changes: 7 additions & 3 deletions cdk/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ module.exports = {
node: true,
jest: true,
},
extends: ['@guardian/eslint-config-typescript'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'@guardian/eslint-config-typescript',
],
parserOptions: {
ecmaVersion: 2020,
tsconfigRootDir: __dirname,
sourceType: 'module',
project: ['./tsconfig.eslint.json'],
},
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/no-inferrable-types': 0,
'import/no-namespace': 2,
},
ignorePatterns: ['**/*.js', 'node_modules'],
ignorePatterns: ['node_modules'],
};
53 changes: 50 additions & 3 deletions cdk/lib/__snapshots__/monitoring.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,16 @@ exports[`The Monitoring stack matches the snapshot 1`] = `
},
"cmpmonitoringalarms68497862": {
"Properties": {
"AlarmDescription": "Alarm if the SUM of Errors is greater than or equal to the threshold (1) for 1 evaluation period",
"ActionsEnabled": true,
"AlarmActions": [
{
"Ref": "internalEmailRecipientA4594AC9",
},
],
"AlarmDescription": "This alarm is triggered if 4 out of 5 lambda executions fail in eu-west-1",
"AlarmName": "CMP Monitoring - PROD - eu-west-1",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"DatapointsToAlarm": 4,
"Dimensions": [
{
"Name": "FunctionName",
Expand All @@ -242,12 +250,18 @@ exports[`The Monitoring stack matches the snapshot 1`] = `
},
},
],
"EvaluationPeriods": 1,
"EvaluationPeriods": 5,
"MetricName": "Errors",
"Namespace": "AWS/Lambda",
"Period": 60,
"OKActions": [
{
"Ref": "internalEmailRecipientA4594AC9",
},
],
"Period": 120,
"Statistic": "Sum",
"Threshold": 1,
"TreatMissingData": "notBreaching",
},
"Type": "AWS::CloudWatch::Alarm",
},
Expand Down Expand Up @@ -289,6 +303,39 @@ exports[`The Monitoring stack matches the snapshot 1`] = `
},
"Type": "AWS::Events::Rule",
},
"internalEmailRecipientA4594AC9": {
"Properties": {
"Tags": [
{
"Key": "gu:cdk:version",
"Value": "TEST",
},
{
"Key": "gu:repo",
"Value": "guardian/consent-management-platform",
},
{
"Key": "Stack",
"Value": "cmp-monitoring",
},
{
"Key": "Stage",
"Value": "PROD",
},
],
},
"Type": "AWS::SNS::Topic",
},
"internalEmailRecipienttransparencyandconsentguardiancoukAC07DD37": {
"Properties": {
"Endpoint": "transparency.and.consent@guardian.co.uk",
"Protocol": "email",
"TopicArn": {
"Ref": "internalEmailRecipientA4594AC9",
},
},
"Type": "AWS::SNS::Subscription",
},
},
}
`;
43 changes: 36 additions & 7 deletions cdk/lib/monitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ import { GuStack } from '@guardian/cdk/lib/constructs/core';
import { GuLambdaFunction } from '@guardian/cdk/lib/constructs/lambda';
import type { App } from 'aws-cdk-lib';
import { Duration } from 'aws-cdk-lib';
import type {
IAlarmAction} from 'aws-cdk-lib/aws-cloudwatch';
import {
Alarm,
ComparisonOperator,
Metric,
Unit,
TreatMissingData,
Unit
} from 'aws-cdk-lib/aws-cloudwatch';
import { SnsAction } from 'aws-cdk-lib/aws-cloudwatch-actions';
import { Rule, RuleTargetInput, Schedule } from 'aws-cdk-lib/aws-events';
import { LambdaFunction } from 'aws-cdk-lib/aws-events-targets';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import { Topic } from 'aws-cdk-lib/aws-sns';
import { EmailSubscription } from 'aws-cdk-lib/aws-sns-subscriptions';

export class Monitoring extends GuStack {
constructor(scope: App, id: string, props: GuStackProps) {
Expand All @@ -24,6 +30,8 @@ export class Monitoring extends GuStack {

const lambdaBaseName = 'cmp-monitoring';

const prodDurationInMinutes = 2;

const policyStatement = new PolicyStatement({
effect: Effect.ALLOW,
actions: ['cloudwatch:PutMetricData'],
Expand Down Expand Up @@ -55,12 +63,12 @@ export class Monitoring extends GuStack {

// Defining metric for lambda errors each minute
const errorMetric = monitoringLambdaFunction.metricErrors({
period: Duration.minutes(1),
period: Duration.minutes(prodDurationInMinutes),
});

// Defining metric for lambda errors each minute
monitoringLambdaFunction.metricInvocations({
period: Duration.minutes(1),
period: Duration.minutes(prodDurationInMinutes),
});

const lambdaEventTarget = new LambdaFunction(monitoringLambdaFunction, {
Expand All @@ -70,23 +78,44 @@ export class Monitoring extends GuStack {
}),
});


const monitoringDuration: Duration =
stage === 'PROD' ? Duration.minutes(2) : Duration.days(1); // Every day for CODE; Every 2 minutes for PROD.
stage === 'PROD' ? Duration.minutes(prodDurationInMinutes) : Duration.days(1); // Every day for CODE; Every 2 minutes for PROD.

new Rule(this, 'cmp monitoring schedule', {
schedule: Schedule.rate(monitoringDuration),
targets: [lambdaEventTarget],
});


// Error Alarm
new Alarm(this, 'cmp-monitoring-alarms', {
const alarm = new Alarm(this, 'cmp-monitoring-alarms', {
comparisonOperator:
ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
threshold: 1,
evaluationPeriods: 1,
evaluationPeriods: 5, // This value is the number of periods to watch. Here, we're evaluating 5 executions of the lambda. The lambda is triggered every 2minutes so it's check over a 10 minute timeframe.
actionsEnabled: true,
datapointsToAlarm: 4, // This value is the number of failed data-points/executions that will trigger the alarm. so 4 out of 5
treatMissingData: TreatMissingData.NOT_BREACHING,
metric: errorMetric,
alarmName: `CMP Monitoring - ${stage} - ${region}`,
alarmDescription:
'Alarm if the SUM of Errors is greater than or equal to the threshold (1) for 1 evaluation period',
`This alarm is triggered if 4 out of 5 lambda executions fail in ${region}`,
});

if(this.stage === "PROD"){
const emailSubscription = new EmailSubscription(
"transparency.and.consent@guardian.co.uk"
);

const internalEmailMessaging = new Topic(this, "internalEmailRecipient");
internalEmailMessaging.addSubscription(emailSubscription);

const alarmAction: IAlarmAction = new SnsAction(internalEmailMessaging);

alarm.addAlarmAction(alarmAction)
alarm.addOkAction(alarmAction)
}

}
}
2 changes: 1 addition & 1 deletion cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"@guardian/eslint-config-typescript": "^7.0.0",
"@guardian/prettier": "^5.0.0",
"@guardian/tsconfig": "^0.2.0",
"@guardian/tsconfig": "^0.2.0",
"@types/jest": "^29.5.5",
"@types/node": "20.8.2",
"aws-cdk": "2.95.1",
Expand Down
2 changes: 1 addition & 1 deletion cdk/tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["lib/**/*", "bin/**/*"],
"include": ["lib/**/*", "bin/**/*", ".eslintrc.js"],
"exclude": ["node_modules"]
}
3 changes: 2 additions & 1 deletion cdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"node_modules",
"cdk.out",
"lib/**/*.test.ts",
"lib/**/__snapshots__/**"
"lib/**/__snapshots__/**",
"**/*.d.ts"
]
}
10 changes: 7 additions & 3 deletions monitoring/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ module.exports = {
node: true,
jest: true,
},
extends: ['@guardian/eslint-config-typescript'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'@guardian/eslint-config-typescript',
],
parserOptions: {
ecmaVersion: 2020,
tsconfigRootDir: __dirname,
sourceType: 'module',
project: ['./tsconfig.eslint.json'],
},
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/no-inferrable-types': 0,
'import/no-namespace': 2,
},
ignorePatterns: ['**/*.js', 'node_modules'],
ignorePatterns: ['node_modules', 'dist'],
};
2 changes: 1 addition & 1 deletion monitoring/localRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type LocalRunCliArguments = {

class NoArgumentsError extends Error {
constructor(
message: string = 'Arguments provided are missing or incorrect',
message = 'Arguments provided are missing or incorrect',
) {
super(message);
this.name = 'NoArgumentsError';
Expand Down
14 changes: 7 additions & 7 deletions monitoring/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"validate": "npm-run-all test lint build"
},
"dependencies": {
"@aws-sdk/client-cloudwatch": "^3.484.0",
"@aws-sdk/client-cloudwatch": "^3.490.0",
"commander": "^11.1.0",
"dotenv": "^16.3.1",
"inquirer": "^8.2.6",
Expand All @@ -23,16 +23,16 @@
"uuid": "^9.0.1"
},
"devDependencies": {
"@aws-sdk/client-lambda": "^3.484.0",
"@aws-sdk/client-lambda": "^3.490.0",
"@babel/parser": "^7.23.6",
"@guardian/eslint-config-typescript": "^9.0.0",
"@guardian/eslint-config-typescript": "^9.0.1",
"@guardian/prettier": "^7.0.0",
"@guardian/tsconfig": "^0.2.0",
"@tsconfig/node18": "^18.2.2",
"@types/aws-lambda": "^8.10.130",
"@types/inquirer": "^8.2.7",
"@types/aws-lambda": "^8.10.131",
"@types/inquirer": "^8.2.10",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.6",
"@types/node": "^20.11.1",
"@vercel/ncc": "^0.38.1",
"aws-lambda": "^1.0.7",
"eslint": "^8.56.0",
Expand All @@ -47,4 +47,4 @@
"overrides": {
"clone-deep": "4.0.1"
}
}
}
7 changes: 4 additions & 3 deletions monitoring/src/check-page/common-functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
PutMetricDataCommandInput} from '@aws-sdk/client-cloudwatch';
PutMetricDataCommandInput
} from '@aws-sdk/client-cloudwatch';
import {
CloudWatchClient,
PutMetricDataCommand
Expand Down Expand Up @@ -365,7 +366,7 @@ export const sendMetricData = async (
log_info(`config.platform.toUpperCase() ${config.platform.toUpperCase()})`);
const region = config.region;
const client = new CloudWatchClient({ region: region });
const params: PutMetricDataCommandInput = {
const params = {
MetricData: [
{
MetricName: 'CmpLoadingTime',
Expand Down Expand Up @@ -399,7 +400,7 @@ export const sendMetricData = async (
},
],
Namespace: 'Application',
};
} satisfies PutMetricDataCommandInput;

const command = new PutMetricDataCommand(params);

Expand Down

1 comment on commit 3e0053b

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 80.35% 278/346
🟡 Branches 67.76% 103/152
🟡 Functions 78.02% 71/91
🟡 Lines 79.82% 269/337

Test suite run success

339 tests passing in 18 suites.

Report generated by 🧪jest coverage report action from 3e0053b

Please sign in to comment.