question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

aws-cdk/aws-logs: Cannot add dimensions to MetricFilter

See original GitHub issue

Description

It is possible to add dimensions to a metric filter via the console, but this functionality does not seem to be carried through into CDK.

I expected to be able to add dimensions via metric filter props, but I could only add them to the metric itself and that did not work:

    const metric = new CW.Metric({
      namespace: "demoNs",
      metricName: "demoMetric",
      dimensions: { Type: "$.type" },
    });

    new Logs.MetricFilter(this, `MetricFilter`, {
      metricName: "demoMetric",
      metricNamespace: "demoNs",
      logGroup: props.logGroup,
      filterPattern: Logs.FilterPattern.all(
        Logs.FilterPattern.stringValue("$.category", "==", "metric"),
        Logs.FilterPattern.stringValue("$.name", "==", "success")
      ),
      metricValue: "1",
    });

There is no mention of dimensions at all in the documentation: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-logs.MetricFilter.html

Use Case

Currently I need to create separate metric filters for each dimension, which is not optimal.

Proposed Solution

    const metric = new CW.Metric({
      namespace: "demoNs",
      metricName: "demoMetric"
    });

    new Logs.MetricFilter(this, `MetricFilter`, {
      metricName: "demoMetric",
      metricNamespace: "demoNs",
      logGroup: props.logGroup,
      filterPattern: Logs.FilterPattern.all(
        Logs.FilterPattern.stringValue("$.category", "==", "metric"),
        Logs.FilterPattern.stringValue("$.name", "==", "success")
      ),
      metricValue: "1",
      dimensions: { Type: "$.type" },
    });

Other information

No response

Acknowledge

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

15reactions
marcogrcrcommented, Jan 5, 2022

Until dimension support is added to CloudFormation and CDK, you can use this construct:

import { Construct, Names } from "monocdk";
import {
  ILogGroup,
  MetricFilterOptions,
  RetentionDays,
} from "monocdk/aws-logs";
import {
  AwsCustomResource,
  AwsCustomResourcePolicy,
  AwsSdkCall,
  PhysicalResourceId,
} from "monocdk/custom-resources";

export interface MetricFilterProps extends MetricFilterOptions {
  /** The dimensions of the metric. */
  readonly dimensions?: Record<string, string>;

  /** The log group to add the dimensions to. */
  readonly logGroup: ILogGroup;
}

/**
 * Creates a metric filter on a log group using an AWS custom resource.
 * This construct is temporary until CloudFormation and CDK support metric filter dimensions.
 * @see https://github.com/aws-cloudformation/cloudformation-coverage-roadmap/issues/419
 * @see https://github.com/aws/aws-cdk/issues/16999
 */
export class MetricFilter extends Construct {
  constructor(scope: Construct, id: string, props: MetricFilterProps) {
    super(scope, id);

    const uniqueId = Names.uniqueId(this);

    const onCreateOrUpdate: AwsSdkCall = {
      service: "CloudWatchLogs",
      action: "putMetricFilter",
      physicalResourceId: PhysicalResourceId.of(uniqueId),
      parameters: {
        filterName: uniqueId,
        filterPattern: props.filterPattern.logPatternString,
        logGroupName: props.logGroup.logGroupName,
        metricTransformations: [
          {
            defaultValue: props.defaultValue,
            dimensions: props.dimensions,
            metricName: props.metricName,
            metricNamespace: props.metricNamespace,
            metricValue: props.metricValue ?? "1",
          },
        ],
      },
    };

    const onDelete: AwsSdkCall = {
      service: "CloudWatchLogs",
      action: "deleteMetricFilter",
      parameters: {
        filterName: uniqueId,
        logGroupName: props.logGroup.logGroupName,
      },
    };

    new AwsCustomResource(this, "Default", {
      resourceType: "Custom::MetricFilter",
      onCreate: onCreateOrUpdate,
      onDelete,
      onUpdate: onCreateOrUpdate,
      policy: AwsCustomResourcePolicy.fromSdkCalls({
        resources: AwsCustomResourcePolicy.ANY_RESOURCE,
      }),
      logRetention: RetentionDays.THREE_MONTHS,
    });
  }
}

Example usage:

import { FilterPattern, LogGroup } from "monocdk/aws-logs";

const logGroup = new LogGroup(this, "CloudTrailEvents");

new MetricFilter(this, "MyMetricFilter", {
  dimensions: {
    EventSource: "$.detail.eventSource",
    EventName: "$.detail.eventName"
  },
  filterPattern: FilterPattern.exists("$.detail.eventName"),
  logGroup,
  metricName: "EventCount",
  metricNamespace: "CloudTrailEvents",
  metricValue: "1",
});
1reaction
iRoachiecommented, Aug 18, 2022

Thanks @seyeong, i opened a PR to implement this #21654

Read more comments on GitHub >

github_iconTop Results From Across the Web

class MetricFilter (construct) · AWS CDK
A filter that extracts information from CloudWatch Logs and emits to CloudWatch Metrics. Example. new MetricFilter(this, 'MetricFilter', { logGroup, ...
Read more >
AWS CDK 101 - Cloudwatch Metrics Filter with Dimensions ...
A Metric filter is created on top of a specific log group so that the log streams will be digested and metrics will...
Read more >
@aws-cdk/aws-logs - npm
A very simple MetricFilter can be created by using the logGroup. extractMetric() helper function: declare const logGroup: logs.
Read more >
@aws-cdk/aws-logs | Yarn - Package Manager
@aws-cdk/aws-logs. owner aws3.9mApache-2.01.182.0TS vulns 0 vulnerabilities. The CDK Construct Library for AWS::Logs. aws, cdk, constructs, logs ...
Read more >
PutMetricFilter - Amazon CloudWatch Logs - 亚马逊云科技
When you create a metric filter, you can also optionally assign a unit and dimensions to the metric that is created. Important. Metrics...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found