Listing Cloud Logging log-based metrics using gRPC
- One minute read - 194 wordsReferring to Accessing Google Services using gRPC, I wanted to query a project’s Cloud Logging for log-based metrics using gRPC.
In summary:
ENDPOINT="logging.googleapis.com:443"
ROOT="/path/to/googleapis" # https://github.com/googleapis/googleapis
PACKAGE="google/logging/v2"
# NB Not logging.proto
PROTO="${ROOT}/${PACKAGE}/logging_metrics.proto"
TOKEN=$(gcloud auth print-access-token)
PROJECT="..."
PACKAGE="google.logging.v2"
SERVICE="MetricsServiceV2"
METHOD="${PACKAGE}.${SERVICE}/ListLogMetrics"
# ListLogMetricsRequest fields
PARENT="projects/${PROJECT}"
grpcurl \
--import-path=${ROOT} \
--proto=${PROTO} \
-H "Authorization: Bearer ${TOKEN}" \
-d "{\"parent\": \"${PARENT}\"}" \
${ENDPOINT} ${METHOD}
From APIs Explorer, Cloud Logging API v2, instead of the REST reference, browse the gRPC reference specifically the package google.logging.v2 which includes MetricsServiceV2. We’re interested in the ListLogMetrics method (which unfortunately isn’t directly hyperlinkable) but is defined to be:
rpc ListLogMetrics(ListLogMetricsRequest) returns (ListLogMetricsResponse)
And ListLogMetricsRequest includes a parent field with value projects/{PROJECT_ID}.
The above can all be corroborated using the Google APIs repo and specifically the google folder that includes the gRPC defintions. The MetricsServiceV2 is defined in logging_metrics.proto:
https://github.com/googleapis/googleapis/blob/master/google/logging/v2/logging_metrics.proto
The method’s response is ListLogMetricsResponse which includes a repeated fields metrics of type LogMetric
Because gRPCUrl uses JSON to send requests (-d {...}) and receive responses, the output of the command can be piped through e.g. jq to extract e.g. the name and create times:
grpcurl ... \
| jq -r '.metrics[]|{"name":.name,"createTime":.createTime}'
That’s all!