Skip to content

Commit 5558fff

Browse files
authored
feat(aws-logs): extractMetric() returns Metric object (#939)
This makes it convenient to immediately use that Metric object to define Alarms and Dashboards. Fixes #850.
1 parent f58d98c commit 5558fff

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
12
import cdk = require('@aws-cdk/cdk');
23
import { LogStream } from './log-stream';
34
import { cloudformation } from './logs.generated';
@@ -96,6 +97,7 @@ export abstract class LogGroupRef extends cdk.Construct {
9697
* @param jsonField JSON field to extract (example: '$.myfield')
9798
* @param metricNamespace Namespace to emit the metric under
9899
* @param metricName Name to emit the metric under
100+
* @returns A Metric object representing the extracted metric
99101
*/
100102
public extractMetric(jsonField: string, metricNamespace: string, metricName: string) {
101103
new MetricFilter(this, `${metricNamespace}_${metricName}`, {
@@ -105,6 +107,8 @@ export abstract class LogGroupRef extends cdk.Construct {
105107
filterPattern: FilterPattern.exists(jsonField),
106108
metricValue: jsonField
107109
});
110+
111+
return new cloudwatch.Metric({ metricName, namespace: metricNamespace });
108112
}
109113
}
110114

packages/@aws-cdk/aws-logs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"pkglint": "^0.12.0"
6060
},
6161
"dependencies": {
62+
"@aws-cdk/aws-cloudwatch": "^0.12.0",
6263
"@aws-cdk/aws-iam": "^0.12.0",
6364
"@aws-cdk/cdk": "^0.12.0"
6465
},

packages/@aws-cdk/aws-logs/test/test.loggroup.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export = {
100100
const lg = new LogGroup(stack, 'LogGroup');
101101

102102
// WHEN
103-
lg.extractMetric('$.myField', 'MyService', 'Field');
103+
const metric = lg.extractMetric('$.myField', 'MyService', 'Field');
104104

105105
// THEN
106106
expect(stack).to(haveResource('AWS::Logs::MetricFilter', {
@@ -114,6 +114,8 @@ export = {
114114
}
115115
]
116116
}));
117+
test.equal(metric.namespace, 'MyService');
118+
test.equal(metric.metricName, 'Field');
117119

118120
test.done();
119121
}

0 commit comments

Comments
 (0)