Metrics Query (API)
Retrieve time-series data for a specific metric using the Cloud Monitoring API.
Purpose
Analyze resource utilization and performance trends by fetching raw or aggregated data points.
Inputs
- •
project(optional): The Google Cloud project ID to query metrics from. - •
metric_type(required): The metric type to query (e.g.,compute.googleapis.com/instance/cpu/utilization). - •
filter(optional): Additional filters for the query (e.g.,resource.labels.instance_id="12345"). - •
start_time(optional): The start of the time interval (RFC3339). Defaults to 1 hour ago. - •
end_time(optional): The end of the time interval (RFC3339). Defaults to now. - •
page_size(optional): Maximum number of points to return.
Behavior
- •
Identify the target project ID (referred to as
[PROJECT_ID]). Ifprojectis provided as an input, use it. Otherwise, retrieve the active project ID usinggcloud config get-value project. - •
Obtain an authentication token using
gcloud auth print-access-token. - •
Construct the filter string:
metric.type="[METRIC_TYPE]"plus any additional filters. - •
Call the Monitoring API via
curl:bashcurl -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://monitoring.googleapis.com/v3/projects/[PROJECT_ID]/timeSeries?filter=[FILTER]&interval.startTime=[START_TIME]&interval.endTime=[END_TIME]&pageSize=[PAGE_SIZE]"
- •
If
start_timeorend_timeare not provided, calculate them relative to current UTC time.
Output
Returns a JSON object containing the time-series data points.