Skip to content

Instantly share code, notes, and snippets.

@aamir814
Last active March 28, 2024 18:15
Show Gist options
  • Select an option

  • Save aamir814/726d7aeb0d6a0bd09e0e6330297fd2f8 to your computer and use it in GitHub Desktop.

Select an option

Save aamir814/726d7aeb0d6a0bd09e0e6330297fd2f8 to your computer and use it in GitHub Desktop.
Example script on how to use Cloud Monitoring Metric Service Client in python.
#####################################################################
# Purpose: Following is an example on how to use Google Cloud Monitoring
# Metric Service Client using timeseries to get CPU metric
# for specified Cloud SQL instances in a project with aggregated
# mean value over the last five minuets.
#
# The Service account must have following permissions:
# * roles/monitoring.viewer
# * roles/cloudsql.viewer
# * Service Account Token Creator
#
# Assumption:
# Cloud SQL instance is up and running.
# IAM API is enabled (gcloud services enable iam.googleapis.com)
#
# REFERENCES
# - https://cloud.google.com/python/docs/reference/monitoring/latest/google.cloud.monitoring_v3.services.metric_service.MetricServiceClient#google_cloud_monitoring_v3_services_metric_service_MetricServiceClient_list_time_series
# - https://github.com/googleapis/google-cloud-python/blob/main/packages/google-cloud-monitoring/README.rst
# - https://cloud.google.com/monitoring/custom-metrics/reading-metrics#reducing
# - https://cloud.google.com/logging/docs/view/query-library#sql-filters
#####################################################################
import time
from google.cloud import monitoring_v3
from google.protobuf.timestamp_pb2 import Timestamp
from google.auth import impersonated_credentials
from google.oauth2 import service_account
# CHANGE ME
proj_name="test-playground"
credential_file="/home/user/3b0ff3ae453d.json"
srv_account="[email protected]"
db_name='pgtest-14'
def list_time_series(project_id, service_account_key_file):
# Load the service account key file.
source_credentials = service_account.Credentials.from_service_account_file(
service_account_key_file
)
# Create the impersonated credentials.
target_principal = srv_account
target_scopes = ["https://www.googleapis.com/auth/cloud-platform"]
target_credentials = impersonated_credentials.Credentials(
source_credentials=source_credentials,
target_principal=target_principal,
target_scopes=target_scopes,
)
client = monitoring_v3.MetricServiceClient(credentials=target_credentials)
project_name = f"projects/{project_id}"
interval = monitoring_v3.types.TimeInterval()
now = time.time()
end_time = Timestamp()
end_time.seconds = int(now)
end_time.nanos = int((now - end_time.seconds) * 10**9)
interval.end_time = end_time
start_time = Timestamp()
start_time.seconds = int(now - 300)
start_time.nanos = end_time.nanos
interval.start_time = start_time
aggregation = monitoring_v3.Aggregation(
{
"alignment_period": {"seconds": 300}, # 20 minutes
"per_series_aligner": monitoring_v3.Aggregation.Aligner.ALIGN_MEAN,
"cross_series_reducer": monitoring_v3.Aggregation.Reducer.REDUCE_MEAN,
"group_by_fields": ["resource.zone"],
}
)
results = client.list_time_series(
request={
"name": project_name,
"filter": 'metric.type = "cloudsql.googleapis.com/database/cpu/utilization" AND resource.labels.database_id="' + project_id + ':' + db_name + '"',
"interval": interval,
"view": monitoring_v3.types.ListTimeSeriesRequest.TimeSeriesView.FULL,
"aggregation": aggregation,
}
)
for result in results:
print(result)
list_time_series(proj_name, credential_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment