Sub topic 10
Turning log events into metrics with metric filters
A metric filter watches a log group and turns matching log events into data points for a CloudWatch metric. You can then graph that metric or put an alarm on it. This chapter assumes you know what log groups and log streams are, and that you can publish and query custom metrics.
Why use a metric filter
Your application writes errors to a log file, but CloudWatch has no metric for "errors per minute". A metric filter fills that gap without any change to the application. The agent ships the log lines, and the filter counts the ones you care about.
How a filter works
A filter belongs to one log group. It has two parts:
- A filter pattern that decides which log events match.
- A metric transformation that says which metric receives a value when an event matches.
Filters apply only to events ingested after the filter exists. They do not scan old data. For history, use Logs Insights instead.
Creating a filter
This command adds 1 to the ErrorCount metric in the MyApp namespace for every event containing the term ERROR:
aws logs put-metric-filter \
--log-group-name /app/web \
--filter-name ErrorCount \
--filter-pattern "ERROR" \
--metric-transformations metricName=ErrorCount,metricNamespace=MyApp,metricValue=1,defaultValue=0The transformation fields work as follows:
| Field | Meaning |
|---|---|
| `metricName` and `metricNamespace` | Where the data point is published |
| `metricValue` | The value added per match. `1` counts events. |
| `defaultValue` | The value reported in periods with no matches |
metricValue can also reference a field extracted by the pattern, such as a response time in a JSON log, so you can publish measurements and not only counts.
Choosing a pattern
Patterns are case-sensitive. ERROR does not match error. You can match a plain term, several terms, or fields in JSON or space-delimited logs. Test a pattern before relying on it, using the console's test feature or aws logs test-metric-filter.
Common errors
- No data appears. The filter only sees events ingested after it was created, and the metric appears only after the first match.
- Gaps in the graph. Without
defaultValue, periods with no matches have no data points. An alarm then follows its missing-data setting. SettingdefaultValue=0makes the metric report zero instead. - Wrong case or format. A pattern that never matches produces no error, only silence.
- Unbounded dimensions. Filters can add dimensions from log fields. A field with many unique values, such as a user ID, creates many separate metrics and raises cost.
Exercise
A metric filter on /app/web counts events matching "ERROR". The application logs "error: timeout" in lowercase, and the ErrorCount metric stays empty. Why, and what do you change?
Show answer
Filter patterns are case-sensitive, so "ERROR" does not match "error". Change the pattern to match the actual text, or fix the application's log format. The new pattern counts only events ingested after the filter is updated.