-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget-function-logs-url.sh
More file actions
executable file
·49 lines (40 loc) · 2.22 KB
/
get-function-logs-url.sh
File metadata and controls
executable file
·49 lines (40 loc) · 2.22 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
#!/bin/bash
set -e # Fail on any error
set -o pipefail # Ensure piped commands propagate exit codes properly
set -u # Treat unset variables as an error when substituting
# Prints the log explorer URL for the Cloud Function and displays it in the terminal.
# Usage: get-function-logs-url.sh [rate_feed]
# Example with rate feed filter: get-function-logs-url.sh CELO/USD
get_function_logs_url() {
# Load the current project variables
script_dir=$(dirname "$0")
source "${script_dir}/get-project-vars.sh"
# Optional rate feed filter (first argument)
rate_feed="${1-}"
# URL-encode the rate feed (replace / with %2F)
rate_feed_encoded=$(echo "${rate_feed}" | sed 's/\//%2F/g')
# Get time 1 hour ago in UTC
start_time=$(date -u -v-1H +"%Y-%m-%dT%H:%M:%S.000Z")
# Get current time in UTC
end_time=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
# Build the query with optional rate feed filter
if [[ -n ${rate_feed} ]]; then
logs_explorer_url=$(
cat <<EOF | tr -d '\n' | sed 's/[[:space:]]//g'
https://console.cloud.google.com/logs/query;query=resource.labels.service_name%20%3D%20%22${function_name}%22%20AND%20labels.rateFeed%20%3D%20%22${rate_feed_encoded}%22;summaryFields=labels%252FrateFeed,labels%252Fchain:false:32:beginning;startTime=${start_time};endTime=${end_time}?project=${project_id}
EOF
)
printf '\n\033[1mLogs Explorer URL (filtered by %s)\033[0m\n%s\n' "${rate_feed}" "${logs_explorer_url}"
else
logs_explorer_url=$(
cat <<EOF | tr -d '\n' | sed 's/[[:space:]]//g'
https://console.cloud.google.com/logs/query;query=resource.labels.service_name%20%3D%20%22${function_name}%22;storageScope=project;summaryFields=labels%252FrateFeed,labels%252Fchain:false:32:beginning;startTime=${start_time};endTime=${end_time}?project=${project_id}
EOF
)
printf '\n\033[1mLogs Explorer URL\033[0m - Your function logs in the GCP Logs Explorer, usually this is what you want.\n%s\n' "${logs_explorer_url}"
fi
echo ""
cloud_run_logs_url="https://console.cloud.google.com/run/detail/${region}/${function_name}/logs?project=${project_id}"
printf '\n\033[1mCloud Run Logs\033[0m - The underlying Cloud Run logs, mainly for problems occurring during startup of the function.\n%s\n' "${cloud_run_logs_url}"
}
get_function_logs_url "$@"