Metric filters

A metric filter counts log events that match a pattern and publishes the count as a CloudWatch metric. You use it to count the ERROR lines of python-app, and alarm on that metric in section 5.

  1. Open the log group /fcj/cloudwatch-workshop/var/log/messages, choose Actions (1), then Create metric filter (2).

Create metric filter

  1. In Filter pattern enter the pattern below (1). Both terms must appear in a log event for it to match.

    "python-app" "ERROR"
    

Filter pattern

  1. Under Test pattern, select the log stream of Instance A in Select log data to test (1) and choose Test pattern (2).

Test data

  1. Results shows how many sample events match (1) and lists them (2). Only ERROR lines from python-app match. Choose Next (3).

Test results

  1. Enter Filter name PythonAppErrors (1) and Metric namespace FCJ/CloudWatchWorkshop (2).

Filter name and namespace

  1. Enter the metric details:
    • Metric name: PythonAppErrorCount (1)
    • Metric value: 1 (2), each matching line adds 1
    • Default value: 0 (3), so the metric is 0 instead of missing when there is no error
    • Unit: Count (4)

Metric details

  1. Leave Dimensions empty and choose Next (1).

Next

  1. Review and choose Create metric filter (1).

Create metric filter

  1. The filter is created (1) and listed on the Metric filters tab with its namespace and metric (2). After a minute the metric appears in Metrics under FCJ/CloudWatchWorkshop.

Metric filter created

A metric filter only processes log events that arrive after it was created. It does not count older events.