Certopolis

AWS Certified DevOps Engineer – Professional

CloudWatch Logs metric filters: metric value and default value

A metric filter matches a pattern in log events as they arrive in a log group and publishes a number to a CloudWatch metric for each match. The CloudWatch Logs User Guide says filters are not retroactive: they publish data points only for events that happen after the filter was created.

What is a metric filter made of?

A metric filter has a filter pattern, a metric name and namespace, a metric value and an optional default value. Filters are assigned to a log group and apply to all of its log streams. CloudWatch aggregates and reports the values every minute.

The metric value is what each matching event adds to the metric. A static value of 1 counts occurrences, for example the number of events that contain ERROR. A value taken from the event publishes a number found in the log, as in the pattern { $.latency = * } with the metric value $.latency. To total a numeric field such as bytes transferred, publish the field and read the Sum statistic.

Metric filters work only on log groups in the Standard log class. Delivery to the metric is at least once, so occasional duplicates are possible.

What does the default value change?

Without a default value, CloudWatch reports nothing for a period in which no event matched. The metric then has gaps, and every data point that exists reflects a match. With a default value of 0, a 0 is reported for each period in which logs were ingested but none matched. AWS recommends setting a default value, even if it is 0.

If no logs are ingested at all during a one-minute period, no value is reported, even with a default value. A default value cannot be set on a metric that has dimensions.

What are the limits on dimensions, units and history?

Dimensions are available only for JSON and space-delimited filter patterns, up to three per filter. Each distinct name and value pair becomes a separate metric that is billed as a custom metric, so AWS warns against high-cardinality fields such as requestID. AWS might disable a filter that generates 1000 different name and value pairs for its dimensions over a certain amount of time.

Set the unit correctly when the filter is created. Changing the unit on the filter later has no effect.

A filter publishes data points only for events that arrive after it was created. Events already stored in the log group are not counted. To analyse stored events, query the log group with CloudWatch Logs Insights.

Where the exam catches people

  • Believing a new metric filter counts events already in the log group: filters are not retroactive. Query older events with CloudWatch Logs Insights.
  • Believing a metric value of 1 can total a field: 1 counts matching events. Use the field itself, such as $.bytesSent, as the metric value.
  • Believing a metric without a default value reports 0 when nothing matches: it reports no data. Set the default value to 0.
  • Believing a default value can be combined with dimensions: it cannot.
  • Believing the unit can be corrected by editing the filter: changing the unit later has no effect.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A DevOps team released version 2.4 of an order service through AWS CodePipeline on Monday. On Thursday, the team created an Amazon CloudWatch Logs metric filter that counts log events containing OrderRejected in the service's log group, which is in the Standard log class and keeps 30 days of events. The release manager now needs hourly counts of OrderRejected for the seven days before Monday and for every day since, to compare the two versions. The metric shows data points only from Thursday onward. What should a DevOps engineer do to meet these requirements?

    Right. In the game that’s Renown for your Monitoring and Logging building.

    Not that one: C is right. In the game your Monitoring and Logging building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The events from before Thursday are still in the log group, because it keeps 30 days. A CloudWatch Logs Insights query reads stored events directly, so one query that filters for OrderRejected and counts with one-hour bins gives the release manager the whole comparison window at once.

    Written from docs.aws.amazon.com/AmazonCloudWatch/latest/logs/MonitoringLogData.html

  2. A DevOps team turns checkout failures into an Amazon CloudWatch metric with a CloudWatch Logs metric filter. The filter matches log events that contain CheckoutFailed, publishes a value of 1, and has no dimensions. The checkout service writes log events every minute. A dashboard shows the metric's Average statistic per hour as the failure trend, but it reads exactly 1 in every hour that had a failure and is blank in the hours that had none. The team needs the trend to fall toward zero as failures become rarer, without blank hours. What should a DevOps engineer do to meet these requirements?

    Right. In the game that’s Renown for your Monitoring and Logging building.

    Not that one: B is right. In the game your Monitoring and Logging building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    A default value of 0 makes CloudWatch Logs report 0 for each minute in which logs arrive but nothing matches. Because this service logs every minute, the metric then has a data point every minute, and the hourly Average becomes the share of minutes with failures, which falls as failures become rarer.

    Written from docs.aws.amazon.com/AmazonCloudWatch/latest/logs/MonitoringLogData.html

  3. A media company's download service runs on Amazon EC2 instances and writes one JSON log event for each completed download to an Amazon CloudWatch Logs log group in the Standard log class. Each event includes a bytesSent field. The operations team wants a CloudWatch metric that shows the total bytes sent each minute, so that the team can graph it and create an alarm for sudden drops. The application code must not change. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Monitoring and Logging building.

    Not that one: B is right. In the game your Monitoring and Logging building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Setting the value to $.bytesSent and graphing the Sum gives total bytes sent per minute, and the same metric can drive the alarm.

    Written from docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntaxForMetricFilters.html

Ask for something