|
| 1 | +import logging |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from databricks.sql.telemetry.latency_logger import log_latency |
| 7 | +from databricks.sql.telemetry.models.enums import StatementType |
| 8 | + |
| 9 | +LOGGER = "databricks.sql.telemetry.latency_logger" |
| 10 | + |
| 11 | + |
| 12 | +class Cursor: |
| 13 | + """Stand-in for databricks.sql.client.Cursor (telemetry keys on the class name).""" |
| 14 | + |
| 15 | + def __init__(self, telemetry_enabled): |
| 16 | + self.connection = MagicMock() |
| 17 | + self.connection.telemetry_enabled = telemetry_enabled |
| 18 | + self.connection.get_session_id_hex.return_value = "session-hex" |
| 19 | + self.connection.session.host = "host" |
| 20 | + self.connection.lz4_compression = False |
| 21 | + self.query_id = "statement-id" |
| 22 | + self.active_result_set = None |
| 23 | + self.backend = MagicMock(retry_policy=None) |
| 24 | + |
| 25 | + @log_latency(StatementType.QUERY) |
| 26 | + def execute(self, value): |
| 27 | + return value |
| 28 | + |
| 29 | + @log_latency(StatementType.QUERY) |
| 30 | + def fail(self): |
| 31 | + raise ValueError("boom") |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def telemetry_client(): |
| 36 | + client = MagicMock() |
| 37 | + with patch( |
| 38 | + "databricks.sql.telemetry.latency_logger.TelemetryClientFactory.get_telemetry_client", |
| 39 | + return_value=client, |
| 40 | + ): |
| 41 | + yield client |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def no_debug(): |
| 46 | + logger = logging.getLogger(LOGGER) |
| 47 | + level = logger.level |
| 48 | + logger.setLevel(logging.INFO) |
| 49 | + yield |
| 50 | + logger.setLevel(level) |
| 51 | + |
| 52 | + |
| 53 | +def test_disabled_telemetry_without_debug_only_calls_the_method( |
| 54 | + telemetry_client, no_debug, caplog |
| 55 | +): |
| 56 | + cursor = Cursor(telemetry_enabled=False) |
| 57 | + with caplog.at_level(logging.INFO, logger=LOGGER): |
| 58 | + assert cursor.execute(42) == 42 |
| 59 | + with pytest.raises(ValueError, match="boom"): |
| 60 | + cursor.fail() |
| 61 | + telemetry_client.export_latency_log.assert_not_called() |
| 62 | + assert not caplog.records |
| 63 | + |
| 64 | + |
| 65 | +def test_debug_logging_records_latency_without_telemetry(telemetry_client, caplog): |
| 66 | + cursor = Cursor(telemetry_enabled=False) |
| 67 | + with caplog.at_level(logging.DEBUG, logger=LOGGER): |
| 68 | + assert cursor.execute(1) == 1 |
| 69 | + assert [r.getMessage() for r in caplog.records] == ["execute completed in 0ms"] |
| 70 | + telemetry_client.export_latency_log.assert_not_called() |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("debug", [False, True]) |
| 74 | +def test_enabled_telemetry_exports_latency_even_on_error( |
| 75 | + telemetry_client, no_debug, debug, caplog |
| 76 | +): |
| 77 | + cursor = Cursor(telemetry_enabled=True) |
| 78 | + with caplog.at_level(logging.DEBUG if debug else logging.INFO, logger=LOGGER): |
| 79 | + assert cursor.execute("ok") == "ok" |
| 80 | + with pytest.raises(ValueError, match="boom"): |
| 81 | + cursor.fail() |
| 82 | + assert telemetry_client.export_latency_log.call_count == 2 |
| 83 | + kwargs = telemetry_client.export_latency_log.call_args.kwargs |
| 84 | + assert kwargs["session_id"] == "session-hex" |
| 85 | + assert kwargs["sql_statement_id"] == "statement-id" |
| 86 | + assert kwargs["sql_execution_event"].statement_type == StatementType.QUERY |
| 87 | + assert len(caplog.records) == (2 if debug else 0) |
| 88 | + |
| 89 | + |
| 90 | +def test_object_without_connection_is_not_exported(telemetry_client, no_debug): |
| 91 | + class Handler: |
| 92 | + @log_latency(StatementType.QUERY) |
| 93 | + def run(self): |
| 94 | + return "done" |
| 95 | + |
| 96 | + assert Handler().run() == "done" |
| 97 | + telemetry_client.export_latency_log.assert_not_called() |
| 98 | + |
| 99 | + |
| 100 | +def test_wrapper_keeps_the_method_metadata(): |
| 101 | + assert Cursor.execute.__name__ == "execute" |
| 102 | + assert Cursor.execute.__wrapped__(Cursor(False), 3) == 3 |
0 commit comments