Google Cloud Monitoring
The `google-cloud-monitoring` Python client library provides programmatic access to Google Cloud Monitoring, allowing users to collect metrics, events, and metadata from Google Cloud, AWS, and other sources. It enables the creation of custom metrics, reading of time series data, and integration with Google Cloud Observability for dashboards, charts, and alerts. The library is part of the larger `google-cloud-python` ecosystem and is actively maintained, with frequent updates.
Warnings
- breaking The `google-cloud-monitoring` library version 2.0.0 and later requires Python 3.9 or higher. Older Python versions (3.8 and below) are no longer supported.
- gotcha Authentication is crucial for interacting with Google Cloud APIs. The client library uses Application Default Credentials (ADC) to find credentials. For local development, you often need to run `gcloud auth application-default login` or set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to a service account key file (though the former is generally preferred). Additionally, the project ID must be provided to the client, either explicitly via `client(project='your-project-id')` or implicitly via the `GOOGLE_CLOUD_PROJECT` environment variable.
- gotcha When writing custom metrics, it is critical to correctly specify the `MonitoredResource` type and its associated labels. Omitting or providing incorrect resource information can lead to metrics not appearing as expected or performance issues. The `global` resource type is a common fallback for metrics not tied to a specific compute resource, but more specific types (e.g., `gce_instance`, `cloud_run_revision`) are often better.
- deprecated Older versions of the `google-cloud-monitoring` library (prior to the 2.x releases) often used `from google.cloud import monitoring` and instantiated `monitoring.Client()`. The current, generated client libraries use `from google.cloud import monitoring_v3` and `monitoring_v3.MetricServiceClient()` (or other specific clients for different Monitoring API services). The older client interface may lack newer API features and is not the recommended approach.
Install
-
pip install google-cloud-monitoring
Imports
- MetricServiceClient
from google.cloud import monitoring_v3
- ServiceMonitoringServiceClient
from google.cloud import monitoring_v3
- TimeSeries
from google.cloud.monitoring_v3 import TimeSeries
Quickstart
import os
from google.cloud import monitoring_v3
from google.protobuf import timestamp_pb2
# Set your Google Cloud Project ID using environment variable or replace 'your-project-id'
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-project-id')
if project_id == 'your-project-id':
print("WARNING: Please set the GOOGLE_CLOUD_PROJECT environment variable or replace 'your-project-id' with your actual project ID.")
client = monitoring_v3.MetricServiceClient()
project_name = f"projects/{project_id}"
# Define a custom metric descriptor if it doesn't exist
metric_type = 'custom.googleapis.com/my_app/request_count'
metric_descriptor_name = f"{project_name}/metricDescriptors/{metric_type}"
try:
client.get_metric_descriptor(name=metric_descriptor_name)
print(f"Metric descriptor '{metric_type}' already exists.")
except Exception:
print(f"Creating metric descriptor '{metric_type}'...")
descriptor = monitoring_v3.MetricDescriptor()
descriptor.type = metric_type
descriptor.metric_kind = monitoring_v3.MetricDescriptor.MetricKind.GAUGE
descriptor.value_type = monitoring_v3.MetricDescriptor.ValueType.DOUBLE
descriptor.description = "Count of requests to my application."
client.create_metric_descriptor(name=project_name, metric_descriptor=descriptor)
print("Metric descriptor created.")
# Create a time series
series = monitoring_v3.TimeSeries()
series.metric.type = metric_type
# Assign to a monitored resource (e.g., 'global' for metrics not tied to a specific resource)
series.resource.type = 'global'
# For other resource types (e.g., 'gce_instance', 'cloud_run_revision'), add relevant labels:
# series.resource.labels['instance_id'] = '1234567890'
# series.resource.labels['zone'] = 'us-central1-a'
now = timestamp_pb2.Timestamp()
now.FromDatetime(timestamp_pb2.datetime_helper.utcnow())
point = monitoring_v3.Point()
point.interval.end_time = now
point.value.double_value = 42.0 # Your metric value
series.points.append(point)
# Write the time series data
try:
client.create_time_series(name=project_name, time_series=[series])
print(f"Successfully wrote a custom metric '{metric_type}' to Cloud Monitoring for project '{project_id}'.")
print("You can view this metric in the Google Cloud Console under Monitoring -> Metrics Explorer.")
except Exception as e:
print(f"Error writing time series: {e}")