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

Commit e94e143

Browse files
committed
renamed methods
1 parent 9a33d86 commit e94e143

3 files changed

Lines changed: 24 additions & 13 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@
3737
__CROSS_SYNC_OUTPUT__ = "google.cloud.bigtable.data._sync_autogen.metrics_interceptor"
3838

3939

40-
def _with_operation_from_metadata(func):
40+
def _with_active_operation(func):
4141
"""
42-
Decorator for interceptor methods to extract the active operation
43-
from metadata and pass it to the decorated function.
42+
Decorator for interceptor methods to extract the active operation associated with the
43+
in-scope contextvars, and pass it to the decorated function.
4444
"""
4545

4646
@wraps(func)
4747
def wrapper(self, continuation, client_call_details, request):
48-
operation: "ActiveOperationMetric" | None = ActiveOperationMetric.get_active()
48+
operation: "ActiveOperationMetric" | None = ActiveOperationMetric.from_context()
4949

5050
if operation:
5151
# start a new attempt if not started
@@ -97,7 +97,7 @@ class AsyncBigtableMetricsInterceptor(
9797
"""
9898

9999
@CrossSync.convert
100-
@_with_operation_from_metadata
100+
@_with_active_operation
101101
async def intercept_unary_unary(
102102
self, operation, continuation, client_call_details, request
103103
):
@@ -120,7 +120,7 @@ async def intercept_unary_unary(
120120
operation.add_response_metadata(metadata)
121121

122122
@CrossSync.convert
123-
@_with_operation_from_metadata
123+
@_with_active_operation
124124
async def intercept_unary_stream(
125125
self, operation, continuation, client_call_details, request
126126
):

google/cloud/bigtable/data/_metrics/data_model.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,13 @@ class ActiveOperationMetric:
177177
] = contextvars.ContextVar("active_operation_context")
178178

179179
@classmethod
180-
def get_active(cls):
180+
def from_context(cls) -> ActiveOperationMetric | None:
181+
"""
182+
Reads the active operation saved to contextvars
183+
184+
This is meant to be called by grpc interceptor to grab a reference to
185+
the active attempt reference at the start of an rpc
186+
"""
181187
return cls._active_operation_context.get(None)
182188

183189
@property
@@ -193,6 +199,9 @@ def state(self) -> OperationState:
193199
return OperationState.ACTIVE_ATTEMPT
194200

195201
def __post_init__(self):
202+
"""
203+
Save new instances to contextvars on init
204+
"""
196205
self._active_operation_context.set(self)
197206

198207
def start(self) -> None:
@@ -205,6 +214,7 @@ def start(self) -> None:
205214
if self.state != OperationState.CREATED:
206215
return self._handle_error(INVALID_STATE_ERROR.format("start", self.state))
207216
self.start_time_ns = time.monotonic_ns()
217+
# set as active operation in contextvars
208218
self._active_operation_context.set(self)
209219

210220
def start_attempt(self) -> ActiveAttemptMetric | None:
@@ -220,6 +230,7 @@ def start_attempt(self) -> ActiveAttemptMetric | None:
220230
return self._handle_error(
221231
INVALID_STATE_ERROR.format("start_attempt", self.state)
222232
)
233+
# set as active operation in contextvars
223234
self._active_operation_context.set(self)
224235

225236
try:

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
from grpc import UnaryStreamClientInterceptor
2727

2828

29-
def _with_operation_from_metadata(func):
30-
"""Decorator for interceptor methods to extract the active operation
31-
from metadata and pass it to the decorated function."""
29+
def _with_active_operation(func):
30+
"""Decorator for interceptor methods to extract the active operation associated with the
31+
in-scope contextvars, and pass it to the decorated function."""
3232

3333
@wraps(func)
3434
def wrapper(self, continuation, client_call_details, request):
35-
operation: "ActiveOperationMetric" | None = ActiveOperationMetric.get_active()
35+
operation: "ActiveOperationMetric" | None = ActiveOperationMetric.from_context()
3636
if operation:
3737
if (
3838
operation.state == OperationState.CREATED
@@ -63,7 +63,7 @@ class BigtableMetricsInterceptor(
6363
An async gRPC interceptor to add client metadata and print server metadata.
6464
"""
6565

66-
@_with_operation_from_metadata
66+
@_with_active_operation
6767
def intercept_unary_unary(
6868
self, operation, continuation, client_call_details, request
6969
):
@@ -83,7 +83,7 @@ def intercept_unary_unary(
8383
if metadata is not None:
8484
operation.add_response_metadata(metadata)
8585

86-
@_with_operation_from_metadata
86+
@_with_active_operation
8787
def intercept_unary_stream(
8888
self, operation, continuation, client_call_details, request
8989
):

0 commit comments

Comments
 (0)