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

Commit 4369d57

Browse files
improved comments
1 parent 5f42046 commit 4369d57

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,32 @@
3333
from google.cloud.bigtable.data._metrics.handlers._base import MetricsHandler
3434

3535

36+
# by default, exceptions in the metrics system are logged,
37+
# but enabling this flag causes them to be raised instead
3638
ALLOW_METRIC_EXCEPTIONS = os.getenv("BIGTABLE_METRICS_EXCEPTIONS", False)
3739
LOGGER = (
3840
logging.getLogger(__name__) if os.getenv("BIGTABLE_METRICS_LOGS", False) else None
3941
)
4042

43+
# default values for zone and cluster data, if not captured
4144
DEFAULT_ZONE = "global"
4245
DEFAULT_CLUSTER_ID = "unspecified"
4346

47+
# keys for parsing metadata blobs
4448
BIGTABLE_METADATA_KEY = "x-goog-ext-425905942-bin"
4549
SERVER_TIMING_METADATA_KEY = "server-timing"
46-
4750
SERVER_TIMING_REGEX = re.compile(r"gfet4t7; dur=(\d+)")
4851

4952
INVALID_STATE_ERROR = "Invalid state for {}: {}"
5053

5154

52-
# create a named tuple that holds the clock time, and a more accurate monotonic timestamp
53-
# this allows us to be resistent to clock changes, eg DST
5455
@dataclass(frozen=True)
5556
class TimeTuple:
57+
"""
58+
Tuple that holds both the utc timestamp to record with the metrics, and a
59+
monotonic timestamp for calculating durations. The monotonic timestamp is
60+
preferred for calculations because it is resilient to clock changes, eg DST
61+
"""
5662
utc: datetime.datetime = field(
5763
default_factory=lambda: datetime.datetime.now(datetime.timezone.utc)
5864
)
@@ -82,7 +88,8 @@ class OperationState(Enum):
8288
@dataclass(frozen=True)
8389
class CompletedAttemptMetric:
8490
"""
85-
A dataclass representing the data associated with a completed rpc attempt.
91+
An immutable dataclass representing the data associated with a
92+
completed rpc attempt.
8693
"""
8794

8895
start_time: datetime.datetime
@@ -98,7 +105,8 @@ class CompletedAttemptMetric:
98105
@dataclass(frozen=True)
99106
class CompletedOperationMetric:
100107
"""
101-
A dataclass representing the data associated with a completed rpc operation.
108+
An immutable dataclass representing the data associated with a
109+
completed rpc operation.
102110
"""
103111

104112
op_type: OperationType
@@ -114,6 +122,10 @@ class CompletedOperationMetric:
114122

115123
@dataclass
116124
class ActiveAttemptMetric:
125+
"""
126+
A dataclass representing the data associated with an rpc attempt that is
127+
currently in progress. Fields are mutable and may be optional.
128+
"""
117129
# keep both clock time and monotonic timestamps for active attempts
118130
start_time: TimeTuple = field(default_factory=TimeTuple)
119131
# the time it takes to recieve the first response from the server
@@ -135,7 +147,7 @@ class ActiveAttemptMetric:
135147
class ActiveOperationMetric:
136148
"""
137149
A dataclass representing the data associated with an rpc operation that is
138-
currently in progress.
150+
currently in progress. Fields are mutable and may be optional.
139151
"""
140152

141153
op_type: OperationType
@@ -423,6 +435,10 @@ def _handle_error(message: str) -> None:
423435
async def __aenter__(self):
424436
"""
425437
Implements the async context manager protocol for wrapping unary calls
438+
439+
Using the operation's context manager provides assurances that the operation
440+
is always closed when complete, with the proper status code automaticallty
441+
detected when an exception is raised.
426442
"""
427443
return self._AsyncContextManager(self)
428444

0 commit comments

Comments
 (0)