I am using monitoring_v3 client in Python to create a cloud monitoring custom metric in Project_B. I have written a cloud function for this and deployed it in Project_A.
- A service account has been created for my cloud function in Project_A
(ServAccA). - A service account has been created in Project_B with all the needed permissions to create metrics (ServAccB).
- ServAccA is impersonating ServAccB and necessary permissions are provided.
So I am not passing any credentials explicitly to MetricServiceClient. But I get an error “details = “Permission monitoring.metricDescriptors.create denied (or the resource may not exist).”” when I run this cloud function.
Below is my cloud function in Project_A. Need suggestions on how to make this work – Creating custom metrics in a GCP project using a cloud function in another GCP project. Thanks.
from google.cloud import monitoring_v3
client = monitoring_v3.MetricServiceClient()
project_name = "projects/project_B"
desc_labels = ["id", "ip_address", "hostname"]
descriptor = ga_metric.MetricDescriptor()
descriptor.type = "custom.googleapis.com/my_metric"
descriptor.metric_kind = ga_metric.MetricDescriptor.MetricKind.GAUGE
descriptor.value_type = ga_metric.MetricDescriptor.ValueType.DOUBLE
print(f"Value type set to: {descriptor.value_type}")
descriptor.description = "This is my custom metric."
descriptor = client.create_metric_descriptor(
name=project_name, metric_descriptor=descriptor)