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

Commit 8ff32a5

Browse files
committed
added constant instead of magic number
1 parent 17ad4e0 commit 8ff32a5

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

google/cloud/bigtable/data/_metrics/handlers/opentelemetry.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
from google.cloud.bigtable.data._metrics.data_model import CompletedAttemptMetric
2727
from google.cloud.bigtable.data._metrics.data_model import CompletedOperationMetric
2828

29+
# conversion factor for converting from nanoseconds to milliseconds
30+
NS_TO_MS= 1e6
2931

3032
class _OpenTelemetryInstruments:
3133
"""
@@ -175,14 +177,14 @@ def on_operation_complete(self, op: CompletedOperationMetric) -> None:
175177
is_streaming = str(op.is_streaming)
176178

177179
self.otel.operation_latencies.record(
178-
op.duration_ns / 1e6, {"streaming": is_streaming, **labels}
180+
op.duration_ns / NS_TO_MS, {"streaming": is_streaming, **labels}
179181
)
180182
if (
181183
op.op_type == OperationType.READ_ROWS
182184
and op.first_response_latency_ns is not None
183185
):
184186
self.otel.first_response_latencies.record(
185-
op.first_response_latency_ns / 1e6, labels
187+
op.first_response_latency_ns / NS_TO_MS, labels
186188
)
187189
# only record completed attempts if there were retries
188190
if op.completed_attempts:
@@ -209,19 +211,19 @@ def on_attempt_complete(
209211
is_streaming = str(op.is_streaming)
210212

211213
self.otel.attempt_latencies.record(
212-
attempt.duration_ns / 1e6, {"streaming": is_streaming, "status": status, **labels}
214+
attempt.duration_ns / NS_TO_MS, {"streaming": is_streaming, "status": status, **labels}
213215
)
214-
combined_throttling = attempt.grpc_throttling_time_ns / 1e6
216+
combined_throttling = attempt.grpc_throttling_time_ns / NS_TO_MS
215217
if not op.completed_attempts:
216218
# add flow control latency to first attempt's throttling latency
217-
combined_throttling += (op.flow_throttling_time_ns / 1e6 if op.flow_throttling_time_ns else 0)
219+
combined_throttling += (op.flow_throttling_time_ns / NS_TO_MS if op.flow_throttling_time_ns else 0)
218220
self.otel.throttling_latencies.record(combined_throttling, labels)
219221
self.otel.application_latencies.record(
220-
(attempt.application_blocking_time_ns + attempt.backoff_before_attempt_ns) / 1e6, labels
222+
(attempt.application_blocking_time_ns + attempt.backoff_before_attempt_ns) / NS_TO_MS, labels
221223
)
222224
if attempt.gfe_latency_ns is not None:
223225
self.otel.server_latencies.record(
224-
attempt.gfe_latency_ns / 1e6,
226+
attempt.gfe_latency_ns / NS_TO_MS,
225227
{"streaming": is_streaming, "status": status, **labels},
226228
)
227229
else:

0 commit comments

Comments
 (0)