Skip to content

Commit

Permalink
feat(aws-logs): extractMetric() returns Metric object (#939)
Browse files Browse the repository at this point in the history
This makes it convenient to immediately use that Metric object to define
Alarms and Dashboards.

Fixes #850.
  • Loading branch information
rix0rrr authored Oct 16, 2018
1 parent f58d98c commit 5558fff
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-logs/lib/log-group.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import cdk = require('@aws-cdk/cdk');
import { LogStream } from './log-stream';
import { cloudformation } from './logs.generated';
Expand Down Expand Up @@ -96,6 +97,7 @@ export abstract class LogGroupRef extends cdk.Construct {
* @param jsonField JSON field to extract (example: '$.myfield')
* @param metricNamespace Namespace to emit the metric under
* @param metricName Name to emit the metric under
* @returns A Metric object representing the extracted metric
*/
public extractMetric(jsonField: string, metricNamespace: string, metricName: string) {
new MetricFilter(this, `${metricNamespace}_${metricName}`, {
Expand All @@ -105,6 +107,8 @@ export abstract class LogGroupRef extends cdk.Construct {
filterPattern: FilterPattern.exists(jsonField),
metricValue: jsonField
});

return new cloudwatch.Metric({ metricName, namespace: metricNamespace });
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"pkglint": "^0.12.0"
},
"dependencies": {
"@aws-cdk/aws-cloudwatch": "^0.12.0",
"@aws-cdk/aws-iam": "^0.12.0",
"@aws-cdk/cdk": "^0.12.0"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-logs/test/test.loggroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export = {
const lg = new LogGroup(stack, 'LogGroup');

// WHEN
lg.extractMetric('$.myField', 'MyService', 'Field');
const metric = lg.extractMetric('$.myField', 'MyService', 'Field');

// THEN
expect(stack).to(haveResource('AWS::Logs::MetricFilter', {
Expand All @@ -114,6 +114,8 @@ export = {
}
]
}));
test.equal(metric.namespace, 'MyService');
test.equal(metric.metricName, 'Field');

test.done();
}
Expand Down

0 comments on commit 5558fff

Please sign in to comment.