Skip to content

Instantly share code, notes, and snippets.

@tkuchiki
Created April 21, 2022 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkuchiki/7bb766ee73720421779329bf3f6de2f6 to your computer and use it in GitHub Desktop.
Save tkuchiki/7bb766ee73720421779329bf3f6de2f6 to your computer and use it in GitHub Desktop.
Example of calling Cloud Monitoring API

Prerequisite

  • python3
  • curl
  • gcloud

Usage

echo <filter> | bash cloud-monitoring-api-list-time-series.sh <gcp project id> <start utc without tz> <end utc without tz>

Example

$ echo 'metric.type="spanner.googleapis.com/instance/cpu/utilization_by_priority" resource.type="spanner_instance" resource.label."instance_id"="your instance id" metric.label."priority"="high"' | bash cloud-monitoring-api-list-time-series.sh <your gcp project> 2022-04-01T00:00:00 2022-04-02T00:00:00
#!/bin/bash
encode_url() {
echo -n $(echo -n "${1}" | python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.stdin.read(), safe="~"))')
}
set -u
project_id="${1}"
start="${2}"
end="${3}"
aligner="${4:-ALIGN_MEAN}"
alignment_period="${5:-60s}"
filter="$( encode_url $(cat -))"
access_token="$( gcloud auth print-access-token )"
encoded_start="$( encode_url ${start}.000000Z )"
encoded_end="$( encode_url ${end}.000000Z )"
curl -H "Authorization: Bearer ${access_token}" \
"https://monitoring.googleapis.com/v3/projects/${project_id}/timeSeries/?interval.startTime=${encoded_start}&interval.endTime=${encoded_end}&aggregation.perSeriesAligner=${aligner}&aggregation.alignmentPeriod=${alignment_period}&filter=${filter}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment