Skip to content

Commit 4f8aae5

Browse files
feat(logs): filterName property in MetricFilter (#25246)
This PR is recreating and adding to https://github.com/robertd PR: #23053 Here, `filterName` property is being added for `MetricFilter` And, also adding an existing integ test: `integ.expose-metric.ts` with the `IntegTest` construct. Closes #17626 Co-authored-by: Robert Djurasaj(https://github.com/robertd) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 8a6b376 commit 4f8aae5

File tree

12 files changed

+51
-24
lines changed

12 files changed

+51
-24
lines changed

packages/@aws-cdk-testing/framework-integ/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ nyc.config.js
1313
!**/*.snapshot/**/asset.*/*.d.ts
1414

1515
!**/*.snapshot/**/asset.*/**
16+
17+
**/*.ts.snapshot

packages/@aws-cdk-testing/framework-integ/test/aws-logs/test/integ.expose-metric.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Alarm } from 'aws-cdk-lib/aws-cloudwatch';
22
import { App, RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib';
33
import { FilterPattern, LogGroup, MetricFilter } from 'aws-cdk-lib/aws-logs';
4+
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
45

56
/*
67
* Stack verification steps:
@@ -37,5 +38,10 @@ class ExposeMetricIntegStack extends Stack {
3738
}
3839

3940
const app = new App();
40-
new ExposeMetricIntegStack(app, 'aws-cdk-expose-metric-integ');
41+
const stack = new ExposeMetricIntegStack(app, 'aws-cdk-expose-metric-integ');
42+
43+
new IntegTest(app, 'LambdaTest', {
44+
testCases: [stack],
45+
});
46+
4147
app.synth();

packages/@aws-cdk-testing/framework-integ/test/aws-logs/test/integ.metricfilter.lit.js.snapshot/aws-cdk-metricfilter-integ.assets.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"version": "22.0.0",
2+
"version": "31.0.0",
33
"files": {
4-
"3447b1b7683bf722256f63f808f8ac3a927c270228f18c1ff0245b4d5fc3f919": {
4+
"02ed2687a3e340ff22289dcacf3e4ed024865b2aae1c13bf6bf5c995590480ac": {
55
"source": {
66
"path": "aws-cdk-metricfilter-integ.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "3447b1b7683bf722256f63f808f8ac3a927c270228f18c1ff0245b4d5fc3f919.json",
12+
"objectKey": "02ed2687a3e340ff22289dcacf3e4ed024865b2aae1c13bf6bf5c995590480ac.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk-testing/framework-integ/test/aws-logs/test/integ.metricfilter.lit.js.snapshot/aws-cdk-metricfilter-integ.template.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"MetricNamespace": "MyApp",
2222
"MetricValue": "$.latency"
2323
}
24-
]
24+
],
25+
"FilterName": "MyFilterName"
2526
}
2627
}
2728
},
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"22.0.0"}
1+
{"version":"31.0.0"}

packages/@aws-cdk-testing/framework-integ/test/aws-logs/test/integ.metricfilter.lit.js.snapshot/integ.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "22.0.0",
2+
"version": "31.0.0",
33
"testCases": {
44
"integ.metricfilter.lit": {
55
"stacks": [

packages/@aws-cdk-testing/framework-integ/test/aws-logs/test/integ.metricfilter.lit.js.snapshot/manifest.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "22.0.0",
2+
"version": "31.0.0",
33
"artifacts": {
44
"aws-cdk-metricfilter-integ.assets": {
55
"type": "cdk:asset-manifest",
@@ -17,7 +17,7 @@
1717
"validateOnSynth": false,
1818
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
1919
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
20-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/3447b1b7683bf722256f63f808f8ac3a927c270228f18c1ff0245b4d5fc3f919.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/02ed2687a3e340ff22289dcacf3e4ed024865b2aae1c13bf6bf5c995590480ac.json",
2121
"requiresBootstrapStackVersion": 6,
2222
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2323
"additionalDependencies": [
@@ -42,7 +42,10 @@
4242
"/aws-cdk-metricfilter-integ/MetricFilter/Resource": [
4343
{
4444
"type": "aws:cdk:logicalId",
45-
"data": "MetricFilter1B93B6E5"
45+
"data": "MetricFilter1B93B6E5",
46+
"trace": [
47+
"!!DESTRUCTIVE_CHANGES: WILL_REPLACE"
48+
]
4649
}
4750
],
4851
"/aws-cdk-metricfilter-integ/BootstrapVersion": [

packages/@aws-cdk-testing/framework-integ/test/aws-logs/test/integ.metricfilter.lit.js.snapshot/tree.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
}
2323
},
2424
"constructInfo": {
25-
"fqn": "@aws-cdk/aws-logs.CfnLogGroup",
25+
"fqn": "aws-cdk-lib.aws_logs.CfnLogGroup",
2626
"version": "0.0.0"
2727
}
2828
}
2929
},
3030
"constructInfo": {
31-
"fqn": "@aws-cdk/aws-logs.LogGroup",
31+
"fqn": "aws-cdk-lib.aws_logs.LogGroup",
3232
"version": "0.0.0"
3333
}
3434
},
@@ -52,39 +52,40 @@
5252
"metricName": "Latency",
5353
"metricValue": "$.latency"
5454
}
55-
]
55+
],
56+
"filterName": "MyFilterName"
5657
}
5758
},
5859
"constructInfo": {
59-
"fqn": "@aws-cdk/aws-logs.CfnMetricFilter",
60+
"fqn": "aws-cdk-lib.aws_logs.CfnMetricFilter",
6061
"version": "0.0.0"
6162
}
6263
}
6364
},
6465
"constructInfo": {
65-
"fqn": "@aws-cdk/aws-logs.MetricFilter",
66+
"fqn": "aws-cdk-lib.aws_logs.MetricFilter",
6667
"version": "0.0.0"
6768
}
6869
},
6970
"BootstrapVersion": {
7071
"id": "BootstrapVersion",
7172
"path": "aws-cdk-metricfilter-integ/BootstrapVersion",
7273
"constructInfo": {
73-
"fqn": "@aws-cdk/core.CfnParameter",
74+
"fqn": "aws-cdk-lib.CfnParameter",
7475
"version": "0.0.0"
7576
}
7677
},
7778
"CheckBootstrapVersion": {
7879
"id": "CheckBootstrapVersion",
7980
"path": "aws-cdk-metricfilter-integ/CheckBootstrapVersion",
8081
"constructInfo": {
81-
"fqn": "@aws-cdk/core.CfnRule",
82+
"fqn": "aws-cdk-lib.CfnRule",
8283
"version": "0.0.0"
8384
}
8485
}
8586
},
8687
"constructInfo": {
87-
"fqn": "@aws-cdk/core.Stack",
88+
"fqn": "aws-cdk-lib.Stack",
8889
"version": "0.0.0"
8990
}
9091
},
@@ -93,12 +94,12 @@
9394
"path": "Tree",
9495
"constructInfo": {
9596
"fqn": "constructs.Construct",
96-
"version": "10.1.189"
97+
"version": "10.1.270"
9798
}
9899
}
99100
},
100101
"constructInfo": {
101-
"fqn": "@aws-cdk/core.App",
102+
"fqn": "aws-cdk-lib.App",
102103
"version": "0.0.0"
103104
}
104105
}

packages/@aws-cdk-testing/framework-integ/test/aws-logs/test/integ.metricfilter.lit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class MetricFilterIntegStack extends Stack {
1414
logGroup,
1515
metricNamespace: 'MyApp',
1616
metricName: 'Latency',
17+
filterName: 'MyFilterName',
1718
filterPattern: FilterPattern.exists('$.latency'),
1819
metricValue: '$.latency',
1920
});

packages/aws-cdk-lib/aws-logs/lib/log-group.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,4 +584,11 @@ export interface MetricFilterOptions {
584584
* @default - No unit attached to metrics.
585585
*/
586586
readonly unit?: cloudwatch.Unit;
587+
588+
/**
589+
* The name of the metric filter.
590+
*
591+
* @default - Cloudformation generated name.
592+
*/
593+
readonly filterName?: string;
587594
}

packages/aws-cdk-lib/aws-logs/lib/metric-filter.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ export class MetricFilter extends Resource {
2323
private readonly metricNamespace: string;
2424

2525
constructor(scope: Construct, id: string, props: MetricFilterProps) {
26-
super(scope, id);
26+
super(scope, id, {
27+
physicalName: props.filterName,
28+
});
2729

2830
this.metricName = props.metricName;
2931
this.metricNamespace = props.metricNamespace;
3032

31-
if (Object.keys(props.dimensions ?? {}).length > 3) {
32-
throw new Error('MetricFilter only supports a maximum of 3 Dimensions');
33+
const numberOfDimensions = Object.keys(props.dimensions ?? {}).length;
34+
if (numberOfDimensions > 3) {
35+
throw new Error(`MetricFilter only supports a maximum of 3 dimensions but received ${numberOfDimensions}.`);
3336
}
3437

3538
// It looks odd to map this object to a singleton list, but that's how
@@ -42,6 +45,7 @@ export class MetricFilter extends Resource {
4245
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-metricfilter.html
4346
new CfnMetricFilter(this, 'Resource', {
4447
logGroupName: props.logGroup.logGroupName,
48+
filterName: this.physicalName,
4549
filterPattern: props.filterPattern.logPatternString,
4650
metricTransformations: [{
4751
metricNamespace: props.metricNamespace,

packages/aws-cdk-lib/aws-logs/test/metricfilter.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('metric filter', () => {
1414
logGroup,
1515
metricNamespace: 'AWS/Test',
1616
metricName: 'Latency',
17+
filterName: 'FooBazBar',
1718
metricValue: '$.latency',
1819
filterPattern: FilterPattern.exists('$.latency'),
1920
});
@@ -27,6 +28,7 @@ describe('metric filter', () => {
2728
}],
2829
FilterPattern: '{ $.latency = "*" }',
2930
LogGroupName: { Ref: 'LogGroupF5B46931' },
31+
FilterName: 'FooBazBar',
3032
});
3133
});
3234

@@ -83,7 +85,7 @@ describe('metric filter', () => {
8385
Baz: 'Qux',
8486
Qux: 'Quux',
8587
},
86-
})).toThrow(/MetricFilter only supports a maximum of 3 Dimensions/);
88+
})).toThrow(/MetricFilter only supports a maximum of 3 dimensions but received/);
8789
});
8890

8991
test('metric filter exposes metric', () => {

0 commit comments

Comments
 (0)