From 3e403131d3e173bd2239df38aa85676486b8deb0 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Tue, 16 Oct 2018 15:33:08 +0200 Subject: [PATCH] feat(aws-logs): extractMetric() returns Metric object This makes it convenient to immediately use that Metric object to define Alarms and Dashboards. Fixes #850. --- packages/@aws-cdk/aws-logs/lib/log-group.ts | 4 ++++ packages/@aws-cdk/aws-logs/package.json | 1 + packages/@aws-cdk/aws-logs/test/test.loggroup.ts | 4 +++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-logs/lib/log-group.ts b/packages/@aws-cdk/aws-logs/lib/log-group.ts index 2d2c26c1a94e7..09e36a893cb53 100644 --- a/packages/@aws-cdk/aws-logs/lib/log-group.ts +++ b/packages/@aws-cdk/aws-logs/lib/log-group.ts @@ -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'; @@ -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}`, { @@ -105,6 +107,8 @@ export abstract class LogGroupRef extends cdk.Construct { filterPattern: FilterPattern.exists(jsonField), metricValue: jsonField }); + + return new cloudwatch.Metric({ metricName, namespace: metricNamespace }); } } diff --git a/packages/@aws-cdk/aws-logs/package.json b/packages/@aws-cdk/aws-logs/package.json index ea89db95ebd85..d81cad96630e3 100644 --- a/packages/@aws-cdk/aws-logs/package.json +++ b/packages/@aws-cdk/aws-logs/package.json @@ -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" }, diff --git a/packages/@aws-cdk/aws-logs/test/test.loggroup.ts b/packages/@aws-cdk/aws-logs/test/test.loggroup.ts index b101d0f145f52..217b20e4a4d88 100644 --- a/packages/@aws-cdk/aws-logs/test/test.loggroup.ts +++ b/packages/@aws-cdk/aws-logs/test/test.loggroup.ts @@ -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', { @@ -114,6 +114,8 @@ export = { } ] })); + test.equal(metric.namespace, 'MyService'); + test.equal(metric.metricName, 'Field'); test.done(); }