Skip to content

Commit 8fedb5f

Browse files
vuanhphungIsaac
andcommitted
test: don't assert the measured latency in the debug-log test
Match "execute completed in <n>ms" instead of 0ms, so a slow CI runner can't fail it, and say in the log_latency docstring and comment that nothing is timed when telemetry and DEBUG logging are both off. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 53893d3 commit 8fedb5f

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎src/databricks/sql/telemetry/latency_logger.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ def log_latency(statement_type: StatementType = StatementType.NONE):
155155
156156
This decorator measures the execution time of a method and sends telemetry
157157
data about the operation, including latency, statement information, and
158-
execution context.
158+
execution context. When the connection's telemetry is off and DEBUG logging
159+
is disabled, nothing would record the latency, so the method is called
160+
without timing it.
159161
160162
Args:
161163
statement_type (StatementType): The type of SQL statement being executed.
@@ -177,15 +179,14 @@ def execute(self, query):
177179
def decorator(func):
178180
@functools.wraps(func)
179181
def wrapper(self, *args, **kwargs):
180-
# Fast check: use cached telemetry_enabled flag from connection
181-
# Avoids dictionary lookup + instance check on every operation
182+
# Use the connection's cached telemetry_enabled flag. When it is off
183+
# and DEBUG logging is disabled (always the case on kernel
184+
# connections, which export telemetry from the kernel), skip timing.
182185
connection = getattr(self, "connection", None)
183186
telemetry_enabled = bool(connection) and getattr(
184187
connection, "telemetry_enabled", False
185188
)
186189
if not telemetry_enabled and not logger.isEnabledFor(logging.DEBUG):
187-
# Nothing would record the latency (always the case on kernel
188-
# connections, which export telemetry from the kernel).
189190
return func(self, *args, **kwargs)
190191

191192
start_time = time.monotonic()

‎tests/unit/test_latency_logger.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import re
23
from unittest.mock import MagicMock, patch
34

45
import pytest
@@ -66,7 +67,8 @@ def test_debug_logging_records_latency_without_telemetry(telemetry_client, caplo
6667
cursor = Cursor(telemetry_enabled=False)
6768
with caplog.at_level(logging.DEBUG, logger=LOGGER):
6869
assert cursor.execute(1) == 1
69-
assert [r.getMessage() for r in caplog.records] == ["execute completed in 0ms"]
70+
assert len(caplog.records) == 1
71+
assert re.fullmatch(r"execute completed in \d+ms", caplog.records[0].getMessage())
7072
telemetry_client.export_latency_log.assert_not_called()
7173

7274

0 commit comments

Comments
 (0)