Complete the code to create a Stackdriver Monitoring client in GCP.
from google.cloud import monitoring_v3 client = monitoring_v3.[1]()
The MetricServiceClient is used to interact with Stackdriver Monitoring for observability metrics.
Complete the code to define a metric descriptor type for a custom metric.
metric_descriptor.type = "custom.googleapis.com/[1]"
Custom metrics in GCP start with custom.googleapis.com/ followed by the metric name, here cpu_usage.
Fix the error in the code to write a time series data point for a custom metric.
series.metric.type = "custom.googleapis.com/cpu_usage" series.resource.type = "[1]"
The resource type gce_instance represents a Google Compute Engine VM, which is a common resource for CPU metrics.
Fill both blanks to create a filter string to query CPU usage metrics for a specific instance.
filter = 'metric.type="custom.googleapis.com/cpu_usage" AND resource.labels.instance_id=[1] AND resource.type=[2]'
The instance ID should be a string like "my-instance-id" and the resource type for a VM is "gce_instance".
Fill all three blanks to create a monitoring alert policy condition for high CPU usage.
condition = monitoring_v3.AlertPolicy.Condition(
display_name="High CPU Usage",
condition_threshold=monitoring_v3.AlertPolicy.Condition.MetricThreshold(
filter="metric.type=\"custom.googleapis.com/cpu_usage\" AND resource.type=\"[1]\"",
comparison=monitoring_v3.AlertPolicy.Condition.MetricThreshold.ComparisonType.[2],
threshold_value=[3],
duration=duration_pb2.Duration(seconds=300),
aggregations=[]
)
)The alert monitors CPU usage on a VM (gce_instance), triggers when usage is GREATER_THAN 80.0%, sustained for 5 minutes.