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.