Skip to content

Commit 84e9527

Browse files
authored
feat(LogMonitoring): Added filterExpressions to enable advanced JSON log queries (#644)
Introduces `filterExpressions: string[]` as an additional option to filter logs Fixes #643 --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent c29d3da commit 84e9527

File tree

4 files changed

+264
-4
lines changed

4 files changed

+264
-4
lines changed

API.md

Lines changed: 37 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/monitoring/aws-cloudwatch/LogMonitoring.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,20 @@ export interface LogMonitoringProps
4444
readonly title?: string;
4545

4646
/**
47-
* Pattern to search for, e.g. "ERROR"
47+
* Pattern to filter `@message` field, e.g. "ERROR"
4848
*/
4949
readonly pattern?: string;
5050

51+
/**
52+
* Filter expressions to add.
53+
* @example
54+
* filterExpressions = [`level = "ERROR"`]
55+
* // will be appended to the query as
56+
* | filter level = "ERROR"
57+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax-Filter.html
58+
*/
59+
readonly filterExpressions?: string[];
60+
5161
/**
5262
* Maximum number of log messages to search for.
5363
*
@@ -69,6 +79,7 @@ export class LogMonitoring extends Monitoring {
6979
readonly title?: string;
7080

7181
readonly pattern?: string;
82+
readonly filterExpressions?: string[];
7283
readonly limit: number;
7384

7485
readonly alarmFactory: AlarmFactory;
@@ -88,6 +99,7 @@ export class LogMonitoring extends Monitoring {
8899
this.title = props.title;
89100

90101
this.pattern = props.pattern;
102+
this.filterExpressions = props.filterExpressions;
91103
this.limit = props.limit ?? DefaultLimit;
92104

93105
const namingStrategy = new MonitoringNamingStrategy({
@@ -140,7 +152,21 @@ export class LogMonitoring extends Monitoring {
140152
}
141153

142154
widgets(): IWidget[] {
155+
const filterStatements: string[] = [];
156+
143157
if (this.pattern) {
158+
filterStatements.push(`filter @message like /${this.pattern}/`);
159+
}
160+
161+
if (this.filterExpressions) {
162+
for (const expression of this.filterExpressions) {
163+
if (expression) {
164+
filterStatements.push(`filter ${expression}`);
165+
}
166+
}
167+
}
168+
169+
if (filterStatements.length > 0) {
144170
const height = this.resolveRecommendedHeight(this.limit);
145171

146172
return [
@@ -157,7 +183,7 @@ export class LogMonitoring extends Monitoring {
157183
*/
158184
queryLines: [
159185
"fields @timestamp, @logStream, @message",
160-
`filter @message like /${this.pattern}/`,
186+
...filterStatements,
161187
"sort @timestamp desc",
162188
`limit ${this.limit}`,
163189
],

test/monitoring/aws-cloudwatch/LogMonitoring.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,35 @@ test("snapshot test: no pattern", () => {
4949
expect(Template.fromStack(stack)).toMatchSnapshot();
5050
});
5151

52+
test("snapshot test: filterExpressions", () => {
53+
const stack = new Stack();
54+
55+
const scope = new TestMonitoringScope(stack, "Scope");
56+
57+
const monitoring = new LogMonitoring(scope, {
58+
logGroupName: "DummyLogGroup",
59+
filterExpressions: [`level = "ERROR"`, "sampling_rate = 0"],
60+
});
61+
62+
addMonitoringDashboardsToStack(stack, monitoring);
63+
expect(Template.fromStack(stack)).toMatchSnapshot();
64+
});
65+
66+
test("snapshot test: pattern and filterExpressions", () => {
67+
const stack = new Stack();
68+
69+
const scope = new TestMonitoringScope(stack, "Scope");
70+
71+
const monitoring = new LogMonitoring(scope, {
72+
logGroupName: "DummyLogGroup",
73+
pattern: "DummyPattern",
74+
filterExpressions: [`level = "ERROR"`],
75+
});
76+
77+
addMonitoringDashboardsToStack(stack, monitoring);
78+
expect(Template.fromStack(stack)).toMatchSnapshot();
79+
});
80+
5281
test("snapshot test: with alarms", () => {
5382
const stack = new Stack();
5483

test/monitoring/aws-cloudwatch/__snapshots__/LogMonitoring.test.ts.snap

Lines changed: 170 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)