Question

Difficulty: MediumMonitoring and Analyzing Logs with Amazon CloudWatch

An e-commerce application deployed on Amazon ECS writes structured JSON logs to Amazon CloudWatch Logs. A developer needs to create a CloudWatch metric filter to count the occurrences of HTTP 504 Gateway Timeout errors. A sample log event is:

{
"request": {
"path": "/checkout",
"responseCode": 504
}
}

Which filter pattern must the developer use to correctly match this log event?

  1. { $.request.responseCode = 504 }Answer
  2. B
    { $.request.responseCode == 504 }
  3. C
    { request.responseCode = 504 }
  4. D
    [request.responseCode = 504]

Answer

The pattern `{ $.request.responseCode = 504 }` is the correct filter pattern.
The correct pattern is `{ .request.responseCode = 504 }`. In CloudWatch Logs filter pattern syntax, JSON log events are matched using curly braces `{ }`. The root of the JSON document is represented by ``, and nested properties are traversed using dot notation (e.g., `$.request.responseCode`). Additionally, equality comparison in CloudWatch filter patterns is performed using a single equals sign (`=`).

Step-by-Step Solution

1
Identify the log format
The log format is structured JSON, which requires curly braces `{ }` for the CloudWatch metric filter pattern.
CloudWatch Logs parses JSON objects only if the filter pattern is enclosed in curly braces.
2
Determine the path to the target field
The target field `responseCode` is nested under `request`. In CloudWatch Logs filter syntax, the root of the JSON object is represented by `,andnestedfieldsarereferencedusingdotnotation:`, and nested fields are referenced using dot notation: `.request.responseCode`.
Correct path syntax is necessary to address nested JSON properties.
3
Specify the comparison operator
CloudWatch Logs metric filter syntax uses a single equals sign `=` to evaluate equality.
Using operators like `==` will result in a syntax mismatch and zero metrics reported.

Key Concept

CloudWatch Logs Metric Filter JSON Syntax
Rate this question