1313# limitations under the License
1414from __future__ import annotations
1515
16- import time
17- from typing import Any , Callable
1816from functools import wraps
19- from google .cloud .bigtable .data ._metrics .data_model import OPERATION_INTERCEPTOR_METADATA_KEY
17+ from google .cloud .bigtable .data ._metrics .data_model import (
18+ OPERATION_INTERCEPTOR_METADATA_KEY ,
19+ )
2020from google .cloud .bigtable .data ._metrics .data_model import ActiveOperationMetric
2121from google .cloud .bigtable .data ._metrics .data_model import OperationState
2222
@@ -38,9 +38,17 @@ def _with_operation_from_metadata(func):
3838 Decorator for interceptor methods to extract the active operation
3939 from metadata and pass it to the decorated function.
4040 """
41+
4142 @wraps (func )
4243 def wrapper (self , continuation , client_call_details , request ):
43- key = next ((m [1 ] for m in client_call_details .metadata if m [0 ] == OPERATION_INTERCEPTOR_METADATA_KEY ), None )
44+ key = next (
45+ (
46+ m [1 ]
47+ for m in client_call_details .metadata
48+ if m [0 ] == OPERATION_INTERCEPTOR_METADATA_KEY
49+ ),
50+ None ,
51+ )
4452 operation : "ActiveOperationMetric" = self .operation_map .get (key )
4553 if operation :
4654 # start a new attempt if not started
@@ -51,13 +59,14 @@ def wrapper(self, continuation, client_call_details, request):
5159 else :
5260 # if operation not found, return unwrapped continuation
5361 return continuation (client_call_details , request )
62+
5463 return wrapper
5564
5665
57- @CrossSync .convert_class (
58- sync_name = "BigtableMetricsInterceptor"
59- )
60- class AsyncBigtableMetricsInterceptor ( UnaryUnaryClientInterceptor , UnaryStreamClientInterceptor ):
66+ @CrossSync .convert_class (sync_name = "BigtableMetricsInterceptor" )
67+ class AsyncBigtableMetricsInterceptor (
68+ UnaryUnaryClientInterceptor , UnaryStreamClientInterceptor
69+ ):
6170 """
6271 An async gRPC interceptor to add client metadata and print server metadata.
6372 """
@@ -89,7 +98,9 @@ def on_attempt_complete(self, attempt, operation):
8998
9099 @CrossSync .convert
91100 @_with_operation_from_metadata
92- async def intercept_unary_unary (self , operation , continuation , client_call_details , request ):
101+ async def intercept_unary_unary (
102+ self , operation , continuation , client_call_details , request
103+ ):
93104 encountered_exc : Exception | None = None
94105 call = None
95106 try :
@@ -101,8 +112,7 @@ async def intercept_unary_unary(self, operation, continuation, client_call_detai
101112 finally :
102113 if call is not None :
103114 metadata = (
104- await call .trailing_metadata ()
105- + await call .initial_metadata ()
115+ await call .trailing_metadata () + await call .initial_metadata ()
106116 )
107117 operation .add_response_metadata (metadata )
108118 if encountered_exc is not None :
@@ -111,7 +121,9 @@ async def intercept_unary_unary(self, operation, continuation, client_call_detai
111121
112122 @CrossSync .convert
113123 @_with_operation_from_metadata
114- async def intercept_unary_stream (self , operation , continuation , client_call_details , request ):
124+ async def intercept_unary_stream (
125+ self , operation , continuation , client_call_details , request
126+ ):
115127 async def response_wrapper (call ):
116128 encountered_exc = None
117129 try :
@@ -123,12 +135,11 @@ async def response_wrapper(call):
123135 raise
124136 finally :
125137 metadata = (
126- await call .trailing_metadata ()
127- + await call .initial_metadata ()
138+ await call .trailing_metadata () + await call .initial_metadata ()
128139 )
129140 operation .add_response_metadata (metadata )
130141 if encountered_exc is not None :
131142 # end attempt. If it succeeded, let higher levels decide when to end operation
132143 operation .end_attempt_with_status (encountered_exc )
133144
134- return response_wrapper (await continuation (client_call_details , request ))
145+ return response_wrapper (await continuation (client_call_details , request ))
0 commit comments