Skip to content

Commit 1084f9b

Browse files
committed
[Update] - query cloudwatch logs for multi log groups
1 parent 29babfd commit 1084f9b

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

services/logs.sh

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,27 @@ function aws_logs_tail_with_hint() {
2626

2727
# Function to start an AWS CloudWatch Logs query
2828
aws_logs_run_query() {
29-
local log_group_name="$1" # The name of the log group
30-
local start_time="$2" # The start time for the query in milliseconds
31-
local end_time="$3" # The end time for the query in milliseconds
32-
local query_string="$4" # The CloudWatch Logs Insights query string
33-
local region="$5" # The AWS region
29+
local log_group_names="$1" # Space-separated list of log group names
30+
local start_time="$2" # The start time for the query in milliseconds
31+
local end_time="$3" # The end time for the query in milliseconds
32+
local query_string="$4" # The CloudWatch Logs Insights query string
33+
local region="$5" # The AWS region
3434

3535
# Ensure required parameters are provided
36-
if [[ -z "$log_group_name" || -z "$start_time" || -z "$end_time" || -z "$query_string" || -z "$region" ]]; then
36+
if [[ -z "$log_group_names" || -z "$start_time" || -z "$end_time" || -z "$query_string" || -z "$region" ]]; then
3737
echo "Error: Missing required arguments."
38-
echo "Usage: aws_logs_run_query <log_group_name> <start_time> <end_time> <query_string> <region>"
38+
echo "Usage: aws_logs_run_query <log_group_names> <start_time> <end_time> <query_string> <region>"
3939
return 1
4040
fi
4141

42+
# Convert the space-separated list of log groups into a JSON array
43+
local log_group_json_array
44+
log_group_json_array=$(printf '%s' "$log_group_names" | awk '{printf "[\"%s\"", $1; for (i=2; i<=NF; i++) printf ",\"%s\"", $i; printf "]"}')
45+
4246
# Execute the query
4347
local query_id
4448
query_id=$(aws logs start-query \
45-
--log-group-name "$log_group_name" \
49+
--log-group-names "$log_group_json_array" \
4650
--start-time "$start_time" \
4751
--end-time "$end_time" \
4852
--query-string "$query_string" \
@@ -59,6 +63,11 @@ aws_logs_run_query() {
5963
echo "$query_id"
6064
}
6165

66+
# Function to start an AWS CloudWatch Logs query for multiple log groups
67+
aws_logs_run_query_multi_groups() {
68+
aws_logs_run_query "$@"
69+
}
70+
6271
# Function to get AWS CloudWatch Logs query results
6372
aws_logs_get_query_results() {
6473
local query_id="$1" # The query ID returned by aws_logs_run_query

0 commit comments

Comments
 (0)