Skip to content

Commit

Permalink
feat(scheduler): metrics for all schedules (#27544)
Browse files Browse the repository at this point in the history
Class Schedule now provides static methods for accessing all schedules metrics with default configuration, such as `metricAllErrors` for viewing errors when executing targets.

Advances #23394

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
filletofish authored Oct 18, 2023
1 parent 7398f0c commit 5448009
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 7 deletions.
11 changes: 10 additions & 1 deletion packages/@aws-cdk/aws-scheduler-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,16 @@ EventBridge Scheduler publishes additional metrics when your schedule exhausts i

### Metrics for all schedules

TODO: Not yet implemented. See section in [L2 Event Bridge Scheduler RFC](https://github.com/aws/aws-cdk-rfcs/blob/master/text/0474-event-bridge-scheduler-l2.md)
Class `Schedule` provides static methods for accessing all schedules metrics with default configuration,
such as `metricAllErrors` for viewing errors when executing targets.

```ts
new cloudwatch.Alarm(this, 'SchedulesErrorAlarm', {
metric: Schedule.metricAllErrors(),
threshold: 0,
evaluationPeriods: 1,
});
```

### Metrics for a Group

Expand Down
94 changes: 94 additions & 0 deletions packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IResource, Resource } from 'aws-cdk-lib';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
import { CfnSchedule } from 'aws-cdk-lib/aws-scheduler';
import { Construct } from 'constructs';
import { IGroup } from './group';
Expand Down Expand Up @@ -72,6 +73,99 @@ export interface ScheduleProps {
* An EventBridge Schedule
*/
export class Schedule extends Resource implements ISchedule {
/**
* Return the given named metric for all schedules.
*
* @default - sum over 5 minutes
*/
public static metricAll(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
namespace: 'AWS/Scheduler',
metricName,
statistic: 'sum',
...props,
});
}

/**
* Metric for the number of invocations that were throttled across all schedules.
*
* @see https://docs.aws.amazon.com/scheduler/latest/UserGuide/scheduler-quotas.html
*
* @default - sum over 5 minutes
*/
public static metricAllThrottled(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metricAll('InvocationThrottleCount', props);
}

/**
* Metric for all invocation attempts across all schedules.
*
* @default - sum over 5 minutes
*/
public static metricAllAttempts(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metricAll('InvocationAttemptCount', props);
}
/**
* Emitted when the target returns an exception after EventBridge Scheduler calls the target API across all schedules.
*
* @default - sum over 5 minutes
*/
public static metricAllErrors(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metricAll('TargetErrorCount', props);
}

/**
* Metric for invocation failures due to API throttling by the target across all schedules.
*
* @default - sum over 5 minutes
*/
public static metricAllTargetThrottled(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metricAll('TargetErrorThrottledCount', props);
}

/**
* Metric for dropped invocations when EventBridge Scheduler stops attempting to invoke the target after a schedule's retry policy has been exhausted.
* Metric is calculated for all schedules.
*
* @default - sum over 5 minutes
*/
public static metricAllDropped(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metricAll('InvocationDroppedCount', props);
}

/**
* Metric for invocations delivered to the DLQ across all schedules.
*
* @default - sum over 5 minutes
*/
public static metricAllSentToDLQ(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metricAll('InvocationsSentToDeadLetterCount', props);
}

/**
* Metric for failed invocations that also failed to deliver to DLQ across all schedules.
*
* @default - sum over 5 minutes
*/
public static metricAllFailedToBeSentToDLQ(errorCode?: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
if (errorCode) {
return this.metricAll(`InvocationsFailedToBeSentToDeadLetterCount_${errorCode}`, props);
}

return this.metricAll('InvocationsFailedToBeSentToDeadLetterCount', props);
}

/**
* Metric for delivery of failed invocations to DLQ when the payload of the event sent to the DLQ exceeds the maximum size allowed by Amazon SQS.
* Metric is calculated for all schedules.
*
* @default - sum over 5 minutes
*/
public static metricAllSentToDLQTrunacted(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metricAll('InvocationsSentToDeadLetterCount_Truncated_MessageSizeExceeded', props);
}

/**
* The schedule group associated with this schedule.
*/
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@
}
}
}
},
"AllSchedulerErrorsAlarmA3246F8C": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"EvaluationPeriods": 1,
"MetricName": "TargetErrorCount",
"Namespace": "AWS/Scheduler",
"Period": 300,
"Statistic": "Sum",
"Threshold": 1
}
}
},
"Parameters": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
import * as cdk from 'aws-cdk-lib';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as scheduler from '../lib';
Expand Down Expand Up @@ -42,6 +43,12 @@ new scheduler.Schedule(stack, 'DisabledSchedule', {
enabled: false,
});

new cloudwatch.Alarm(stack, 'AllSchedulerErrorsAlarm', {
metric: scheduler.Schedule.metricAllErrors(),
threshold: 1,
evaluationPeriods: 1,
});

new IntegTest(app, 'integtest-schedule', {
testCases: [stack],
});
41 changes: 40 additions & 1 deletion packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Stack } from 'aws-cdk-lib';
import { App, Stack, Duration } from 'aws-cdk-lib';

import { Template } from 'aws-cdk-lib/assertions';
import * as iam from 'aws-cdk-lib/aws-iam';
Expand Down Expand Up @@ -62,4 +62,43 @@ describe('Schedule', () => {
State: 'DISABLED',
});
});

test('returns metric for delivery of failed invocations to DLQ', () => {
// WHEN
const metric = Schedule.metricAllFailedToBeSentToDLQ();

// THEN
expect(metric.namespace).toEqual('AWS/Scheduler');
expect(metric.metricName).toEqual('InvocationsFailedToBeSentToDeadLetterCount');
expect(metric.dimensions).toBeUndefined();
expect(metric.statistic).toEqual('Sum');
expect(metric.period).toEqual(Duration.minutes(5));
});

test('returns metric for delivery of failed invocations to DLQ for specific error code', () => {
// WHEN
const metric = Schedule.metricAllFailedToBeSentToDLQ('test_error_code');

// THEN
expect(metric.namespace).toEqual('AWS/Scheduler');
expect(metric.metricName).toEqual('InvocationsFailedToBeSentToDeadLetterCount_test_error_code');
expect(metric.dimensions).toBeUndefined();
expect(metric.statistic).toEqual('Sum');
expect(metric.period).toEqual(Duration.minutes(5));
});

test('returns metric for all errors with provided statistic and period', () => {
// WHEN
const metric = Schedule.metricAllErrors({
statistic: 'Maximum',
period: Duration.minutes(1),
});

// THEN
expect(metric.namespace).toEqual('AWS/Scheduler');
expect(metric.metricName).toEqual('TargetErrorCount');
expect(metric.dimensions).toBeUndefined();
expect(metric.statistic).toEqual('Maximum');
expect(metric.period).toEqual(Duration.minutes(1));
});
});

0 comments on commit 5448009

Please sign in to comment.