-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathcloud-watch-query.js
More file actions
204 lines (183 loc) · 5.8 KB
/
cloud-watch-query.js
File metadata and controls
204 lines (183 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
StartQueryCommand,
GetQueryResultsCommand,
} from "@aws-sdk/client-cloudwatch-logs";
import { splitDateRange } from "@aws-doc-sdk-examples/lib/utils/util-date.js";
import { retry } from "@aws-doc-sdk-examples/lib/utils/util-timers.js";
class DateOutOfBoundsError extends Error {}
export class CloudWatchQuery {
/**
* Run a query for all CloudWatch Logs within a certain date range.
* CloudWatch logs return a max of 10,000 results. This class
* performs a binary search across all of the logs in the provided
* date range if a query returns the maximum number of results.
*
* @param {import('@aws-sdk/client-cloudwatch-logs').CloudWatchLogsClient} client
* @param {{ logGroupNames: string[], dateRange: [Date, Date], queryConfig: { limit: number } }} config
*/
constructor(client, { logGroupNames, dateRange, queryConfig }) {
this.client = client;
/**
* All log groups are queried.
*/
this.logGroupNames = logGroupNames;
/**
* The inclusive date range that is queried.
*/
this.dateRange = dateRange;
/**
* CloudWatch Logs never returns more than 10,000 logs.
*/
this.limit = queryConfig?.limit ?? 10000;
/**
* @type {import("@aws-sdk/client-cloudwatch-logs").ResultField[][]}
*/
this.results = [];
}
/**
* Run the query.
*/
async run() {
this.secondsElapsed = 0;
const start = new Date();
this.results = await this._largeQuery(this.dateRange);
const end = new Date();
this.secondsElapsed = (end - start) / 1000;
return this.results;
}
/**
* Recursively query for logs.
* @param {[Date, Date]} dateRange
* @returns {Promise<import("@aws-sdk/client-cloudwatch-logs").ResultField[][]>}
*/
async _largeQuery(dateRange) {
const logs = await this._query(dateRange, this.limit);
console.log(
`Query date range: ${dateRange
.map((d) => d.toISOString())
.join(" to ")}. Found ${logs.length} logs.`,
);
if (logs.length < this.limit) {
return logs;
}
const lastLogDate = this._getLastLogDate(logs);
const offsetLastLogDate = new Date(lastLogDate);
offsetLastLogDate.setMilliseconds(lastLogDate.getMilliseconds() + 1);
const subDateRange = [offsetLastLogDate, dateRange[1]];
const [r1, r2] = splitDateRange(subDateRange);
const results = await Promise.all([
this._largeQuery(r1),
this._largeQuery(r2),
]);
return [logs, ...results].flat();
}
/**
* Find the most recent log in a list of logs.
* @param {import("@aws-sdk/client-cloudwatch-logs").ResultField[][]} logs
*/
_getLastLogDate(logs) {
const timestamps = logs
.map(
(log) =>
log.find((fieldMeta) => fieldMeta.field === "@timestamp")?.value,
)
.filter((t) => !!t)
.map((t) => `${t}Z`)
.sort();
if (!timestamps.length) {
throw new Error("No timestamp found in logs.");
}
return new Date(timestamps[timestamps.length - 1]);
}
// snippet-start:[javascript.v3.cloudwatch-logs.actions.GetQueryResults]
/**
* Simple wrapper for the GetQueryResultsCommand.
* @param {string} queryId
*/
_getQueryResults(queryId) {
return this.client.send(new GetQueryResultsCommand({ queryId }));
}
// snippet-end:[javascript.v3.cloudwatch-logs.actions.GetQueryResults]
/**
* Starts a query and waits for it to complete.
* @param {[Date, Date]} dateRange
* @param {number} maxLogs
*/
async _query(dateRange, maxLogs) {
try {
const { queryId } = await this._startQuery(dateRange, maxLogs);
const { results } = await this._waitUntilQueryDone(queryId);
return results ?? [];
} catch (err) {
/**
* This error is thrown when StartQuery returns an error indicating
* that the query's start or end date occur before the log group was
* created.
*/
if (err instanceof DateOutOfBoundsError) {
return [];
}
throw err;
}
}
// snippet-start:[javascript.v3.cloudwatch-logs.actions.StartQuery]
/**
* Wrapper for the StartQueryCommand. Uses a static query string
* for consistency.
* @param {[Date, Date]} dateRange
* @param {number} maxLogs
* @returns {Promise<{ queryId: string }>}
*/
async _startQuery([startDate, endDate], maxLogs = 10000) {
try {
return await this.client.send(
new StartQueryCommand({
logGroupNames: this.logGroupNames,
queryString: "fields @timestamp, @message | sort @timestamp asc",
startTime: startDate.valueOf(),
endTime: endDate.valueOf(),
limit: maxLogs,
}),
);
} catch (err) {
/** @type {string} */
const message = err.message;
if (message.startsWith("Query's end date and time")) {
// This error indicates that the query's start or end date occur
// before the log group was created.
throw new DateOutOfBoundsError(message);
}
throw err;
}
}
// snippet-end:[javascript.v3.cloudwatch-logs.actions.StartQuery]
/**
* Call GetQueryResultsCommand until the query is done.
* @param {string} queryId
*/
_waitUntilQueryDone(queryId) {
const getResults = async () => {
const results = await this._getQueryResults(queryId);
const queryDone = [
"Complete",
"Failed",
"Cancelled",
"Timeout",
"Unknown",
].includes(results.status);
return { queryDone, results };
};
return retry(
{ intervalInMs: 1000, maxRetries: 60, quiet: true },
async () => {
const { queryDone, results } = await getResults();
if (!queryDone) {
throw new Error("Query not done.");
}
return results;
},
);
}
}