1515# This file is automatically generated by CrossSync. Do not edit manually.
1616
1717from __future__ import annotations
18+ from typing import Sequence
1819import time
1920from functools import wraps
20- from google .cloud .bigtable .data ._metrics .data_model import (
21- OPERATION_INTERCEPTOR_METADATA_KEY ,
22- )
2321from google .cloud .bigtable .data ._metrics .data_model import ActiveOperationMetric
2422from google .cloud .bigtable .data ._metrics .data_model import OperationState
23+ from google .cloud .bigtable .data ._metrics .data_model import OperationType
2524from google .cloud .bigtable .data ._metrics .handlers ._base import MetricsHandler
2625from grpc import UnaryUnaryClientInterceptor
2726from grpc import UnaryStreamClientInterceptor
@@ -33,19 +32,7 @@ def _with_operation_from_metadata(func):
3332
3433 @wraps (func )
3534 def wrapper (self , continuation , client_call_details , request ):
36- found_operation_id : str | None = None
37- try :
38- new_metadata : list [tuple [str , str ]] = []
39- if client_call_details .metadata :
40- for k , v in client_call_details .metadata :
41- if k == OPERATION_INTERCEPTOR_METADATA_KEY :
42- found_operation_id = v
43- else :
44- new_metadata .append ((k , v ))
45- client_call_details .metadata = new_metadata
46- except Exception :
47- pass
48- operation : "ActiveOperationMetric" = self .operation_map .get (found_operation_id )
35+ operation : "ActiveOperationMetric" | None = ActiveOperationMetric .get_active ()
4936 if operation :
5037 if (
5138 operation .state == OperationState .CREATED
@@ -59,18 +46,13 @@ def wrapper(self, continuation, client_call_details, request):
5946 return wrapper
6047
6148
62- def _end_attempt (operation , exc , metadata ):
63- """Helper to add metadata and exception to an operation"""
64- if metadata is not None :
65- operation .add_response_metadata (metadata )
66- if exc is not None :
67- operation .end_attempt_with_status (exc )
68-
69-
70- def _get_metadata (source ):
49+ def _get_metadata (source ) -> dict [str , str | bytes ] | None :
7150 """Helper to extract metadata from a call or RpcError"""
7251 try :
73- return (source .trailing_metadata () or []) + (source .initial_metadata () or [])
52+ metadata : Sequence [tuple [str .str | bytes ]] = (
53+ source .trailing_metadata () + source .initial_metadata ()
54+ )
55+ return {k : v for (k , v ) in metadata }
7456 except Exception :
7557 return None
7658
@@ -82,53 +64,31 @@ class BigtableMetricsInterceptor(
8264 An async gRPC interceptor to add client metadata and print server metadata.
8365 """
8466
85- def __init__ (self ):
86- super ().__init__ ()
87- self .operation_map = {}
88-
89- def register_operation (self , operation ):
90- """Register an operation object to be tracked my the interceptor
91-
92- When registered, the operation will receive metadata updates:
93- - start_attempt if attempt not started when rpc is being sent
94- - add_response_metadata after call is complete
95- - end_attempt_with_status if attempt receives an error
96-
97- The interceptor will register itself as a handeler for the operation,
98- so it can unregister the operation when it is complete"""
99- self .operation_map [operation .uuid ] = operation
100- operation .handlers .append (self )
101-
102- def on_operation_complete (self , op ):
103- if op .uuid in self .operation_map :
104- del self .operation_map [op .uuid ]
105-
106- def on_operation_cancelled (self , op ):
107- self .on_operation_complete (op )
108-
10967 @_with_operation_from_metadata
11068 def intercept_unary_unary (
11169 self , operation , continuation , client_call_details , request
11270 ):
113- encountered_exc : Exception | None = None
11471 metadata = None
11572 try :
11673 call = continuation (client_call_details , request )
11774 metadata = _get_metadata (call )
11875 return call
11976 except Exception as rpc_error :
12077 metadata = _get_metadata (rpc_error )
121- encountered_exc = rpc_error
12278 raise rpc_error
12379 finally :
124- _end_attempt (operation , encountered_exc , metadata )
80+ if metadata is not None :
81+ operation .add_response_metadata (metadata )
12582
12683 @_with_operation_from_metadata
12784 def intercept_unary_stream (
12885 self , operation , continuation , client_call_details , request
12986 ):
13087 def response_wrapper (call ):
131- has_first_response = operation .first_response_latency is not None
88+ has_first_response = (
89+ operation .first_response_latency_ns is not None
90+ or operation .op_type != OperationType .READ_ROWS
91+ )
13292 encountered_exc = None
13393 try :
13494 for response in call :
@@ -143,10 +103,14 @@ def response_wrapper(call):
143103 raise
144104 finally :
145105 if call is not None :
146- _end_attempt (operation , encountered_exc , _get_metadata (call ))
106+ metadata = _get_metadata (encountered_exc or call )
107+ if metadata is not None :
108+ operation .add_response_metadata (metadata )
147109
148110 try :
149111 return response_wrapper (continuation (client_call_details , request ))
150112 except Exception as rpc_error :
151- _end_attempt (operation , rpc_error , _get_metadata (rpc_error ))
113+ metadata = _get_metadata (rpc_error )
114+ if metadata is not None :
115+ operation .add_response_metadata (metadata )
152116 raise rpc_error
0 commit comments