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

Commit 01e6b36

Browse files
committed
refactored interceptor
1 parent 6d585ec commit 01e6b36

2 files changed

Lines changed: 53 additions & 71 deletions

File tree

google/cloud/bigtable/data/_async/metrics_interceptor.py

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import time
1717
from functools import wraps
18-
from grpc import RpcError
1918
from google.cloud.bigtable.data._metrics.data_model import (
2019
OPERATION_INTERCEPTOR_METADATA_KEY,
2120
)
@@ -66,6 +65,26 @@ def wrapper(self, continuation, client_call_details, request):
6665
return wrapper
6766

6867

68+
def _end_attempt(operation, exc, metadata):
69+
"""Helper to add metadata and exception to an operation"""
70+
if metadata is not None:
71+
operation.add_response_metadata(metadata)
72+
if exc is not None:
73+
# end attempt. If it succeeded, let higher levels decide when to end operation
74+
operation.end_attempt_with_status(exc)
75+
76+
77+
@CrossSync.convert
78+
async def _get_metadata(source):
79+
"""Helper to extract metadata from a call or RpcError"""
80+
try:
81+
return (await source.trailing_metadata() or []) + (
82+
await source.initial_metadata() or []
83+
)
84+
except Exception:
85+
# ignore errors while fetching metadata
86+
return None
87+
6988
@CrossSync.convert_class(sync_name="BigtableMetricsInterceptor")
7089
class AsyncBigtableMetricsInterceptor(
7190
UnaryUnaryClientInterceptor, UnaryStreamClientInterceptor, MetricsHandler
@@ -109,25 +128,14 @@ async def intercept_unary_unary(
109128
metadata = None
110129
try:
111130
call = await continuation(client_call_details, request)
112-
metadata = (await call.trailing_metadata() or []) + (await call.initial_metadata() or [])
131+
metadata = await _get_metadata(call)
113132
return call
114-
except RpcError as rpc_error:
115-
# attempt extracting metadata from error
116-
try:
117-
metadata = (await rpc_error.trailing_metadata() or []) + (await rpc_error.initial_metadata() or [])
118-
except Exception:
119-
pass
133+
except Exception as rpc_error:
134+
metadata = await _get_metadata(rpc_error)
120135
encountered_exc = rpc_error
121136
raise rpc_error
122-
except Exception as e:
123-
encountered_exc = e
124-
raise
125137
finally:
126-
if metadata is not None:
127-
operation.add_response_metadata(metadata)
128-
if encountered_exc is not None:
129-
# end attempt. If it succeeded, let higher levels decide when to end operation
130-
operation.end_attempt_with_status(encountered_exc)
138+
_end_attempt(operation, encountered_exc, metadata)
131139

132140
@CrossSync.convert
133141
@_with_operation_from_metadata
@@ -146,30 +154,17 @@ async def response_wrapper(call):
146154
)
147155
has_first_response = True
148156
yield response
149-
150-
151157
except Exception as e:
158+
# handle errors while processing stream
152159
encountered_exc = e
153160
raise
154161
finally:
155162
if call is not None:
156-
metadata = (await call.trailing_metadata() or []) + (await call.initial_metadata() or [])
157-
operation.add_response_metadata(metadata)
158-
if encountered_exc is not None:
159-
# end attempt. If it succeeded, let higher levels decide when to end operation
160-
operation.end_attempt_with_status(encountered_exc)
163+
_end_attempt(operation, encountered_exc, await _get_metadata(call))
161164

162165
try:
163166
return response_wrapper(await continuation(client_call_details, request))
164-
except RpcError as rpc_error:
165-
# attempt extracting metadata from error
166-
try:
167-
metadata = (await rpc_error.trailing_metadata() or []) + (await rpc_error.initial_metadata() or [])
168-
operation.add_response_metadata(metadata)
169-
except Exception:
170-
pass
171-
operation.end_attempt_with_status(rpc_error)
172-
raise rpc_error
173-
except Exception as e:
174-
operation.end_attempt_with_status(e)
175-
raise
167+
except Exception as rpc_error:
168+
# handle errors while intializing stream
169+
_end_attempt(operation, rpc_error, await _get_metadata(rpc_error))
170+
raise rpc_error

google/cloud/bigtable/data/_sync_autogen/metrics_interceptor.py

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from __future__ import annotations
1818
import time
1919
from functools import wraps
20-
from grpc import RpcError
2120
from google.cloud.bigtable.data._metrics.data_model import (
2221
OPERATION_INTERCEPTOR_METADATA_KEY,
2322
)
@@ -56,6 +55,22 @@ def wrapper(self, continuation, client_call_details, request):
5655
return wrapper
5756

5857

58+
def _end_attempt(operation, exc, metadata):
59+
"""Helper to add metadata and exception to an operation"""
60+
if metadata is not None:
61+
operation.add_response_metadata(metadata)
62+
if exc is not None:
63+
operation.end_attempt_with_status(exc)
64+
65+
66+
def _get_metadata(source):
67+
"""Helper to extract metadata from a call or RpcError"""
68+
try:
69+
return (source.trailing_metadata() or []) + (source.initial_metadata() or [])
70+
except Exception:
71+
return None
72+
73+
5974
class BigtableMetricsInterceptor(
6075
UnaryUnaryClientInterceptor, UnaryStreamClientInterceptor, MetricsHandler
6176
):
@@ -95,27 +110,14 @@ def intercept_unary_unary(
95110
metadata = None
96111
try:
97112
call = continuation(client_call_details, request)
98-
metadata = (call.trailing_metadata() or []) + (
99-
call.initial_metadata() or []
100-
)
113+
metadata = _get_metadata(call)
101114
return call
102-
except RpcError as rpc_error:
103-
try:
104-
metadata = (rpc_error.trailing_metadata() or []) + (
105-
rpc_error.initial_metadata() or []
106-
)
107-
except Exception:
108-
pass
115+
except Exception as rpc_error:
116+
metadata = _get_metadata(rpc_error)
109117
encountered_exc = rpc_error
110118
raise rpc_error
111-
except Exception as e:
112-
encountered_exc = e
113-
raise
114119
finally:
115-
if metadata is not None:
116-
operation.add_response_metadata(metadata)
117-
if encountered_exc is not None:
118-
operation.end_attempt_with_status(encountered_exc)
120+
_end_attempt(operation, encountered_exc, metadata)
119121

120122
@_with_operation_from_metadata
121123
def intercept_unary_stream(
@@ -137,25 +139,10 @@ def response_wrapper(call):
137139
raise
138140
finally:
139141
if call is not None:
140-
metadata = (call.trailing_metadata() or []) + (
141-
call.initial_metadata() or []
142-
)
143-
operation.add_response_metadata(metadata)
144-
if encountered_exc is not None:
145-
operation.end_attempt_with_status(encountered_exc)
142+
_end_attempt(operation, encountered_exc, _get_metadata(call))
146143

147144
try:
148145
return response_wrapper(continuation(client_call_details, request))
149-
except RpcError as rpc_error:
150-
try:
151-
metadata = (rpc_error.trailing_metadata() or []) + (
152-
rpc_error.initial_metadata() or []
153-
)
154-
operation.add_response_metadata(metadata)
155-
except Exception:
156-
pass
157-
operation.end_attempt_with_status(rpc_error)
146+
except Exception as rpc_error:
147+
_end_attempt(operation, rpc_error, _get_metadata(rpc_error))
158148
raise rpc_error
159-
except Exception as e:
160-
operation.end_attempt_with_status(e)
161-
raise

0 commit comments

Comments
 (0)