1+ import itertools
12import logging
2- import re
33from unittest .mock import MagicMock , patch
44
55import pytest
@@ -42,6 +42,14 @@ def telemetry_client():
4242 yield client
4343
4444
45+ @pytest .fixture
46+ def monotonic ():
47+ # Each call advances one second, so every timed call measures exactly 1000 ms.
48+ with patch ("databricks.sql.telemetry.latency_logger.time" ) as fake_time :
49+ fake_time .monotonic .side_effect = itertools .count (0.0 , 1.0 )
50+ yield fake_time .monotonic
51+
52+
4553@pytest .fixture
4654def no_debug ():
4755 logger = logging .getLogger (LOGGER )
@@ -51,51 +59,59 @@ def no_debug():
5159 logger .setLevel (level )
5260
5361
54- def test_disabled_telemetry_without_debug_only_calls_the_method (
55- telemetry_client , no_debug , caplog
62+ def test_disabled_telemetry_without_debug_does_not_time_the_call (
63+ telemetry_client , monotonic , no_debug , caplog
5664):
5765 cursor = Cursor (telemetry_enabled = False )
5866 with caplog .at_level (logging .INFO , logger = LOGGER ):
5967 assert cursor .execute (42 ) == 42
6068 with pytest .raises (ValueError , match = "boom" ):
6169 cursor .fail ()
70+ monotonic .assert_not_called ()
6271 telemetry_client .export_latency_log .assert_not_called ()
6372 assert not caplog .records
6473
6574
66- def test_debug_logging_records_latency_without_telemetry (telemetry_client , caplog ):
75+ def test_debug_logging_records_latency_without_telemetry (
76+ telemetry_client , monotonic , caplog
77+ ):
6778 cursor = Cursor (telemetry_enabled = False )
6879 with caplog .at_level (logging .DEBUG , logger = LOGGER ):
6980 assert cursor .execute (1 ) == 1
70- assert len ( caplog . records ) == 1
71- assert re . fullmatch ( r "execute completed in \d+ms" , caplog . records [ 0 ]. getMessage ())
81+ assert monotonic . call_count == 2
82+ assert [ r . getMessage () for r in caplog . records ] == [ "execute completed in 1000ms" ]
7283 telemetry_client .export_latency_log .assert_not_called ()
7384
7485
7586@pytest .mark .parametrize ("debug" , [False , True ])
7687def test_enabled_telemetry_exports_latency_even_on_error (
77- telemetry_client , no_debug , debug , caplog
88+ telemetry_client , monotonic , no_debug , debug , caplog
7889):
7990 cursor = Cursor (telemetry_enabled = True )
8091 with caplog .at_level (logging .DEBUG if debug else logging .INFO , logger = LOGGER ):
8192 assert cursor .execute ("ok" ) == "ok"
8293 with pytest .raises (ValueError , match = "boom" ):
8394 cursor .fail ()
95+ assert monotonic .call_count == 4
8496 assert telemetry_client .export_latency_log .call_count == 2
8597 kwargs = telemetry_client .export_latency_log .call_args .kwargs
98+ assert kwargs ["latency_ms" ] == 1000
8699 assert kwargs ["session_id" ] == "session-hex"
87100 assert kwargs ["sql_statement_id" ] == "statement-id"
88101 assert kwargs ["sql_execution_event" ].statement_type == StatementType .QUERY
89102 assert len (caplog .records ) == (2 if debug else 0 )
90103
91104
92- def test_object_without_connection_is_not_exported (telemetry_client , no_debug ):
105+ def test_object_without_connection_is_not_exported (
106+ telemetry_client , monotonic , no_debug
107+ ):
93108 class Handler :
94109 @log_latency (StatementType .QUERY )
95110 def run (self ):
96111 return "done"
97112
98113 assert Handler ().run () == "done"
114+ monotonic .assert_not_called ()
99115 telemetry_client .export_latency_log .assert_not_called ()
100116
101117
0 commit comments