1- """Interceptor for collecting Cloud Spanner metrics."""
1+ # Copyright 2025 Google LLC
2+ #
3+ # Licensed under the Apache License, Version 2.0 (the "License");
4+ # you may not use this file except in compliance with the License.
5+ # You may obtain a copy of the License at
6+ #
7+ # http://www.apache.org/licenses/LICENSE-2.0
8+ #
9+ # Unless required by applicable law or agreed to in writing, software
10+ # distributed under the License is distributed on an "AS IS" BASIS,
11+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ # See the License for the specific language governing permissions and
13+ # limitations under the License.
14+
15+ """Interceptor for collecting Cloud Spanner metrics."""
216
317from grpc_interceptor import ClientInterceptor
418from .constants import (
519 GOOGLE_CLOUD_RESOURCE_KEY ,
620 SPANNER_METHOD_PREFIX ,
721)
8- from .metrics_tracer import MetricsTracer
922from typing import Dict
1023from .spanner_metrics_tracer_factory import SpannerMetricsTracerFactory
1124import re
@@ -88,11 +101,17 @@ def _set_metrics_tracer_attributes(self, resources: Dict[str, str]) -> None:
88101
89102 if resources :
90103 if "project" in resources :
91- SpannerMetricsTracerFactory .current_metrics_tracer .set_project (resources ["project" ])
104+ SpannerMetricsTracerFactory .current_metrics_tracer .set_project (
105+ resources ["project" ]
106+ )
92107 if "instance" in resources :
93- SpannerMetricsTracerFactory .current_metrics_tracer .set_instance (resources ["instance" ])
108+ SpannerMetricsTracerFactory .current_metrics_tracer .set_instance (
109+ resources ["instance" ]
110+ )
94111 if "database" in resources :
95- SpannerMetricsTracerFactory .current_metrics_tracer .set_database (resources ["database" ])
112+ SpannerMetricsTracerFactory .current_metrics_tracer .set_database (
113+ resources ["database" ]
114+ )
96115
97116 def intercept (self , invoked_method , request_or_iterator , call_details ):
98117 """Intercept gRPC calls to collect metrics.
@@ -112,6 +131,7 @@ def intercept(self, invoked_method, request_or_iterator, call_details):
112131 ## Extract Project / Instance / Databse from header information
113132 resources = self ._extract_resource_from_path (call_details .metadata )
114133 self ._set_metrics_tracer_attributes (resources )
134+
115135 ## Format method to be be spanner.<method name>
116136 method_name = self ._remove_prefix (
117137 call_details .method , SPANNER_METHOD_PREFIX
0 commit comments