Skip to content

Commit 3ffca9e

Browse files
Improve interface definition and add more default values
1 parent 4acf1ef commit 3ffca9e

File tree

4 files changed

+161
-144
lines changed

4 files changed

+161
-144
lines changed

packages/@aws-cdk/aws-applicationsignals-alpha/lib/slo/interval.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,36 @@ export abstract class Interval implements IInterval {
2626
* Implementation of Calendar Interval
2727
*/
2828
export class CalendarInterval extends Interval {
29+
/**
30+
* The duration value for the interval
31+
* Must be greater than 0
32+
*
33+
* @private
34+
*/
2935
private readonly duration: number;
36+
37+
/**
38+
* The unit of duration measurement
39+
* Can be MINUTE, HOUR, DAY, or MONTH
40+
*
41+
* @private
42+
*/
3043
private readonly unit: DurationUnit;
44+
45+
/**
46+
* The start time of the interval
47+
* Specified as Unix timestamp in milliseconds
48+
* Default starts from now
49+
*
50+
* @private
51+
*/
3152
private readonly startTime: number;
3253

3354
constructor(props: CalendarIntervalProps) {
3455
super();
3556
this.duration = props.duration;
3657
this.unit = props.unit;
37-
this.startTime = props.startTime;
58+
this.startTime = props.startTime?? Date.now();
3859
this.validate();
3960
}
4061

@@ -128,4 +149,4 @@ export class Goal {
128149
interval: this.props.interval._bind(),
129150
};
130151
}
131-
}
152+
}

packages/@aws-cdk/aws-applicationsignals-alpha/lib/slo/metric.ts

Lines changed: 91 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,6 @@
1-
import { MetricDataQuery, SliMetricBaseProps } from './slo';
21
import { ComparisonOperator, MetricStatistic, MetricType } from './constants';
32
import { KeyAttributes } from './keyAttributes';
43

5-
// /**
6-
// * Base properties for SLI metric configuration
7-
// */
8-
// export type SliMetricBaseProps = {
9-
// /**
10-
// * The threshold value for the metric
11-
// *
12-
// * @required
13-
// */
14-
// readonly metricThreshold: number;
15-
//
16-
// /**
17-
// * The comparison operator
18-
// *
19-
// * @default - Based on metric type
20-
// */
21-
// readonly comparisonOperator?: ComparisonOperator;
22-
// } & (ApplicationSignalsMetricProps | CloudWatchMetricProps);
23-
//
24-
// /**
25-
// * Period-based metric properties
26-
// */
27-
// export interface PeriodBasedMetricProps extends SliMetricBaseProps {
28-
// /**
29-
// * The period in seconds
30-
// *
31-
// * @required
32-
// */
33-
// readonly periodSeconds: number;
34-
//
35-
// /**
36-
// * The statistic to use
37-
// *
38-
// * @required
39-
// */
40-
// readonly statistic: MetricStatistic;
41-
// }
42-
//
43-
// /**
44-
// * Request-based metric properties
45-
// */
46-
// export interface RequestBasedMetricProps extends SliMetricBaseProps {
47-
// /**
48-
// * The good count metrics
49-
// * Required for request-based SLOs
50-
// *
51-
// * @required
52-
// */
53-
// readonly goodCountMetrics: MetricDataQuery[];
54-
//
55-
// /**
56-
// * The total count metrics
57-
// * Required for request-based SLOs
58-
// *
59-
// * @required
60-
// */
61-
// readonly totalCountMetrics: MetricDataQuery[];
62-
//
63-
// /**
64-
// * The bad count metrics
65-
// * Optional for request-based SLOs
66-
// *
67-
// * @default - undefined
68-
// */
69-
// readonly badCountMetrics?: MetricDataQuery[];
70-
// }
71-
72-
734
/**
745
* Interface for metric dimension
756
*/
@@ -290,37 +221,128 @@ export interface MetricDataQuery {
290221
* Period-based metric properties with Application Signals
291222
*/
292223
export interface PeriodBasedAppSignalsMetricProps extends SliMetricBaseProps {
224+
/**
225+
* The type of metric being measured
226+
* Can be LATENCY or AVAILABILITY
227+
*
228+
* @required
229+
*/
293230
readonly metricType: MetricType;
231+
232+
/**
233+
* Key attributes for the service being monitored
234+
* Must include at least one of Type, Name, and Environment
235+
*
236+
* @required
237+
*/
294238
readonly keyAttributes: { [key: string]: string };
239+
240+
/**
241+
* The name of the operation being measured
242+
* Used to filter metrics for specific operation
243+
*
244+
*/
295245
readonly operationName?: string;
246+
247+
/**
248+
* The period in seconds for metric aggregation
249+
* Must be a multiple of 60
250+
*
251+
* @required
252+
*/
296253
readonly periodSeconds: number;
254+
255+
/**
256+
* The statistic to use for aggregation
257+
* Examples: Average, Sum, p99
258+
*
259+
* @required
260+
*/
297261
readonly statistic: MetricStatistic;
298262
}
299263

300264
/**
301265
* Period-based metric properties with CloudWatch metrics
302266
*/
303267
export interface PeriodBasedCloudWatchMetricProps extends SliMetricBaseProps {
268+
/**
269+
* The metric data queries to execute
270+
* Can include raw metrics and math expressions
271+
*
272+
* @required
273+
*/
304274
readonly metricDataQueries: MetricDataQuery[];
275+
276+
/**
277+
* The period in seconds for metric aggregation
278+
* Must be a multiple of 60
279+
*
280+
* @required
281+
*/
305282
readonly periodSeconds: number;
283+
284+
/**
285+
* The statistic to use for aggregation
286+
* Examples: Average, Sum, p99
287+
*
288+
* @required
289+
*/
306290
readonly statistic: MetricStatistic;
307291
}
308292

309293
/**
310294
* Request-based metric properties with Application Signals
311295
*/
312296
export interface RequestBasedAppSignalsMetricProps extends SliMetricBaseProps {
297+
/**
298+
* The type of metric being measured
299+
* Can be LATENCY or AVAILABILITY
300+
*
301+
* @required
302+
*/
313303
readonly metricType: MetricType;
304+
305+
/**
306+
* Key attributes for the applications being monitored
307+
* Must include at least one of Type, Name, and Environment
308+
*
309+
* @required
310+
*/
314311
readonly keyAttributes: { [key: string]: string };
312+
313+
/**
314+
* The name of the operation being measured
315+
*
316+
*/
315317
readonly operationName?: string;
316318
}
317319

318320
/**
319321
* Request-based metric properties with CloudWatch metrics
320322
*/
321323
export interface RequestBasedCloudWatchMetricProps extends SliMetricBaseProps {
324+
/**
325+
* Metrics that count successful requests
326+
* Optional if can be derived from total - bad
327+
* Used to calculate success rate
328+
*
329+
* @required
330+
*/
322331
readonly goodCountMetrics: MetricDataQuery[];
332+
333+
/**
334+
* Metrics that count total requests
335+
* Used as denominator for success rate
336+
*
337+
* @required
338+
*/
323339
readonly totalCountMetrics: MetricDataQuery[];
340+
341+
/**
342+
* Metrics that count failed requests
343+
* Optional if can be derived from total - good
344+
*
345+
*/
324346
readonly badCountMetrics?: MetricDataQuery[];
325347
}
326348

packages/@aws-cdk/aws-applicationsignals-alpha/lib/slo/slo-types.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { ISlo, PeriodBasedSloProps, RequestBasedSloProps } from './slo';
22
import { ComparisonOperator, MetricType } from './constants';
33
import { Goal } from './interval';
4-
import { PeriodBasedAppSignalsMetricProps, PeriodBasedCloudWatchMetricProps, RequestBasedAppSignalsMetricProps, RequestBasedCloudWatchMetricProps, SliMetricBaseProps } from './metric';
5-
4+
import { PeriodBasedAppSignalsMetricProps, PeriodBasedCloudWatchMetricProps, RequestBasedAppSignalsMetricProps, RequestBasedCloudWatchMetricProps } from './metric';
5+
import { Resource } from 'aws-cdk-lib';
6+
import { Construct } from 'constructs';
7+
import * as applicationsignals from 'aws-cdk-lib/aws-applicationsginals';
68

79
/**
810
* Base class for all SLO implementations
@@ -45,13 +47,27 @@ export abstract class ServiceLevelObjective extends Resource implements ISlo {
4547
}
4648

4749
protected getMetricComparisonOperator(metric: PeriodBasedAppSignalsMetricProps | PeriodBasedCloudWatchMetricProps | RequestBasedAppSignalsMetricProps | RequestBasedCloudWatchMetricProps): ComparisonOperator {
48-
return metric.comparisonOperator ?? getDefaultComparisonOperator(metric.metricType);
50+
return metric.comparisonOperator ?? this.getDefaultComparisonOperator(metric.metricType);
51+
}
52+
53+
protected getPeriod(periodSeconds?: number): number {
54+
return periodSeconds ?? 60;
4955
}
5056

5157
protected isAppSignalsMetric(metric: PeriodBasedAppSignalsMetricProps | PeriodBasedCloudWatchMetricProps | RequestBasedAppSignalsMetricProps | RequestBasedCloudWatchMetricProps): boolean {
5258
return 'metricType' in metric && 'keyAttributes' in metric;
5359
}
5460

61+
protected getDefaultComparisonOperator(metricType?: MetricType): ComparisonOperator {
62+
switch (metricType) {
63+
case MetricType.LATENCY:
64+
return ComparisonOperator.LESS_THAN_OR_EQUAL;
65+
case MetricType.AVAILABILITY:
66+
return ComparisonOperator.GREATER_THAN_OR_EQUAL;
67+
default:
68+
throw new Error('ComparisonOperator must be specified when metricType is not provided');
69+
}
70+
}
5571
abstract _bind(): applicationsignals.CfnServiceLevelObjectiveProps;
5672
}
5773

@@ -75,11 +91,11 @@ export class PeriodBasedSlo extends ServiceLevelObjective {
7591
metricType: metric.metricType,
7692
keyAttributes: metric.keyAttributes,
7793
operationName: metric.operationName,
78-
periodSeconds: metric.periodSeconds,
94+
periodSeconds: this.getPeriod(metric.periodSeconds),
7995
statistic: metric.statistic,
8096
} : {
8197
metricDataQueries: metric.metricDataQueries,
82-
periodSeconds: metric.periodSeconds,
98+
periodSeconds: this.getPeriod(metric.periodSeconds),
8399
statistic: metric.statistic,
84100
},
85101
comparisonOperator: this.getMetricComparisonOperator(metric),
@@ -120,17 +136,3 @@ export class RequestBasedSlo extends ServiceLevelObjective {
120136
};
121137
}
122138
}
123-
124-
/**
125-
* Helper function to get default comparison operator
126-
*/
127-
function getDefaultComparisonOperator(metricType?: MetricType): ComparisonOperator {
128-
switch (metricType) {
129-
case MetricType.LATENCY:
130-
return ComparisonOperator.LESS_THAN_OR_EQUAL;
131-
case MetricType.AVAILABILITY:
132-
return ComparisonOperator.GREATER_THAN_OR_EQUAL;
133-
default:
134-
throw new Error('ComparisonOperator must be specified when metricType is not provided');
135-
}
136-
}

0 commit comments

Comments
 (0)