Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 7982c32

Browse files
committed
fix: ensure required labels are present in metrics exporter
Silently drops metrics missing instance_id to prevent 400 InvalidArgument errors from Cloud Monitoring for project-level operations.
1 parent 2d08ad3 commit 7982c32

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

google/cloud/spanner_v1/metrics/metrics_exporter.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ def _data_point_to_timeseries_pb(
299299
)
300300
return series
301301

302-
@staticmethod
303302
def _resource_metrics_to_timeseries_pb(
303+
self,
304304
metrics_data: "MetricsData",
305305
) -> List["TimeSeries"]:
306306
"""
@@ -324,6 +324,28 @@ def _resource_metrics_to_timeseries_pb(
324324
) = CloudMonitoringMetricsExporter._extract_metric_labels(
325325
data_point
326326
)
327+
328+
# Ensure project_id is present in monitored resource labels
329+
if (
330+
MONITORED_RES_LABEL_KEY_PROJECT
331+
not in monitored_resource_labels
332+
):
333+
monitored_resource_labels[
334+
MONITORED_RES_LABEL_KEY_PROJECT
335+
] = self.project_id
336+
337+
# The OpenTelemetry exporter uses the 'spanner_instance_client' resource type,
338+
# which strictly requires both project_id and instance_id. However, some
339+
# Spanner API calls (like creating or listing instances) operate at the
340+
# project level and naturally lack an instance_id. We silently drop these
341+
# metrics here to prevent Cloud Monitoring from rejecting the entire batch
342+
# with a 400 InvalidArgument error.
343+
if (
344+
MONITORED_RES_LABEL_KEY_INSTANCE
345+
not in monitored_resource_labels
346+
):
347+
continue
348+
327349
monitored_resource = CloudMonitoringMetricsExporter._resource_to_monitored_resource_pb(
328350
resource_metric.resource, monitored_resource_labels
329351
)

0 commit comments

Comments
 (0)