diff --git a/google/cloud/bigtable/data/_metrics/handlers/opentelemetry.py b/google/cloud/bigtable/data/_metrics/handlers/opentelemetry.py index 94e0d53aa..ea8283ba4 100644 --- a/google/cloud/bigtable/data/_metrics/handlers/opentelemetry.py +++ b/google/cloud/bigtable/data/_metrics/handlers/opentelemetry.py @@ -177,7 +177,9 @@ def on_operation_complete(self, op: CompletedOperationMetric) -> None: self.otel.operation_latencies.record( op.duration, {"streaming": is_streaming, **labels} ) - self.otel.retry_count.add(len(op.completed_attempts) - 1, labels) + # only record completed attempts if there were retries + if op.completed_attempts: + self.otel.retry_count.add(len(op.completed_attempts) - 1, labels) def on_attempt_complete( self, attempt: CompletedAttemptMetric, op: ActiveOperationMetric @@ -223,9 +225,9 @@ def on_attempt_complete( attempt.gfe_latency, {"streaming": is_streaming, "status": status, **labels}, ) - # gfe headers not attached. Record a connectivity error. - # TODO: this should not be recorded as an error when direct path is enabled - is_error = attempt.gfe_latency is None - self.otel.connectivity_error_count.add( - int(is_error), {"status": status, **labels} - ) + else: + # gfe headers not attached. Record a connectivity error. + # TODO: this should not be recorded as an error when direct path is enabled + self.otel.connectivity_error_count.add( + 1, {"status": status, **labels} + ) diff --git a/tests/unit/data/_metrics/handlers/test_handler_opentelemetry.py b/tests/unit/data/_metrics/handlers/test_handler_opentelemetry.py index e195bde96..ca3e8ce16 100644 --- a/tests/unit/data/_metrics/handlers/test_handler_opentelemetry.py +++ b/tests/unit/data/_metrics/handlers/test_handler_opentelemetry.py @@ -222,7 +222,7 @@ def test_operation_update_labels(self, metric_name, kind, optional_labels): op = CompletedOperationMetric( op_type=expected_op_type, start_time=0, - completed_attempts=[], + completed_attempts=[object()], duration=1, final_status=expected_status, cluster_id="c",