|
19 | 19 | cast, |
20 | 20 | Any, |
21 | 21 | AsyncIterable, |
| 22 | + Callable, |
22 | 23 | Optional, |
23 | 24 | Set, |
24 | 25 | Sequence, |
|
104 | 105 | else: |
105 | 106 | from typing import Iterable # noqa: F401 |
106 | 107 | from grpc import insecure_channel |
| 108 | + from grpc import intercept_channel |
107 | 109 | from google.cloud.bigtable_v2.services.bigtable.transports import BigtableGrpcTransport as TransportType # type: ignore |
108 | 110 | from google.cloud.bigtable_v2.services.bigtable import BigtableClient as GapicClient # type: ignore |
109 | 111 | from google.cloud.bigtable.data._sync_autogen.mutations_batcher import _MB_SIZE |
@@ -206,7 +208,7 @@ def __init__( |
206 | 208 | credentials = google.auth.credentials.AnonymousCredentials() |
207 | 209 | if project is None: |
208 | 210 | project = _DEFAULT_BIGTABLE_EMULATOR_CLIENT |
209 | | - |
| 211 | + self._metrics_interceptor = MetricInterceptorType() |
210 | 212 | # initialize client |
211 | 213 | ClientWithProject.__init__( |
212 | 214 | self, |
@@ -234,7 +236,6 @@ def __init__( |
234 | 236 | self._executor: concurrent.futures.ThreadPoolExecutor | None = ( |
235 | 237 | concurrent.futures.ThreadPoolExecutor() if not CrossSync.is_async else None |
236 | 238 | ) |
237 | | - self._interceptor = MetricInterceptorType() |
238 | 239 | if self._emulator_host is None: |
239 | 240 | # attempt to start background channel refresh tasks |
240 | 241 | try: |
@@ -263,12 +264,23 @@ def _build_grpc_channel(self, *args, **kwargs) -> AsyncSwappableChannel: |
263 | 264 | Returns: |
264 | 265 | a custom wrapped swappable channel |
265 | 266 | """ |
| 267 | + create_channel_fn: Callable[[], Channel] |
266 | 268 | if self._emulator_host is not None: |
267 | 269 | # emulators use insecure channel |
268 | 270 | create_channel_fn = partial(insecure_channel, self._emulator_host) |
269 | | - else: |
| 271 | + elif CrossSync.is_async: |
270 | 272 | create_channel_fn = partial(TransportType.create_channel, *args, **kwargs) |
271 | | - return AsyncSwappableChannel(create_channel_fn) |
| 273 | + else: |
| 274 | + # attach sync interceptors in create_channel_fn |
| 275 | + create_channel_fn = lambda: intercept_channel( |
| 276 | + TransportType.create_channel(*args, **kwargs), self._metrics_interceptor |
| 277 | + ) |
| 278 | + new_channel = AsyncSwappableChannel(create_channel_fn) |
| 279 | + if CrossSync.is_async: |
| 280 | + # attach async interceptors |
| 281 | + new_channel._unary_unary_interceptors.append(self._metrics_interceptor) |
| 282 | + new_channel._unary_stream_interceptors.append(self._metrics_interceptor) |
| 283 | + return new_channel |
272 | 284 |
|
273 | 285 | @staticmethod |
274 | 286 | def _client_version() -> str: |
@@ -922,24 +934,12 @@ def __init__( |
922 | 934 | ) |
923 | 935 |
|
924 | 936 | self._metrics = BigtableClientSideMetricsController( |
925 | | - client._interceptor, |
| 937 | + client._metrics_interceptor, |
926 | 938 | project_id=self.client.project, |
927 | 939 | instance_id=instance_id, |
928 | 940 | table_id=table_id, |
929 | 941 | app_profile_id=app_profile_id, |
930 | 942 | ) |
931 | | - # TODO: simplify interceptors |
932 | | - if CrossSync.is_async: |
933 | | - client.transport.grpc_channel._unary_unary_interceptors.append( |
934 | | - self._metrics.interceptor |
935 | | - ) |
936 | | - client.transport.grpc_channel._unary_stream_interceptors.append( |
937 | | - self._metrics.interceptor |
938 | | - ) |
939 | | - else: |
940 | | - client.transport.grpc_channel = intercept_channel( |
941 | | - self._metrics.interceptor, client.transport.grpc_channel |
942 | | - ) |
943 | 943 |
|
944 | 944 | try: |
945 | 945 | self._register_instance_future = CrossSync.create_task( |
|
0 commit comments