-
Notifications
You must be signed in to change notification settings - Fork 113
Testing for telemetry #616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saishreeeee
wants to merge
7
commits into
telemetry
Choose a base branch
from
telemetry-testing
base: telemetry
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
67a8497
added multithreaded tests, exeception handling tests
saishreeeee 76e60fe
Merge branch 'telemetry' into telemetry-testing
saishreeeee 5a84e11
Merge branch 'telemetry' into telemetry-testing
saishreeeee 70fd810
used batch size instead of default batch size
saishreeeee 6c5d6ba
Merge branch 'telemetry' into telemetry-testing
saishreeeee 3e9b47d
tests
saishreeeee 11d41ce
test
saishreeeee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import threading | ||
from unittest.mock import patch, MagicMock | ||
|
||
from databricks.sql.client import Connection | ||
from databricks.sql.telemetry.telemetry_client import TelemetryClientFactory, TelemetryClient | ||
from databricks.sql.thrift_backend import ThriftBackend | ||
from databricks.sql.utils import ExecuteResponse | ||
from databricks.sql.thrift_api.TCLIService.ttypes import TSessionHandle, TOperationHandle, TOperationState, THandleIdentifier | ||
|
||
try: | ||
import pyarrow as pa | ||
except ImportError: | ||
pa = None | ||
|
||
|
||
def run_in_threads(target, num_threads, pass_index=False): | ||
"""Helper to run target function in multiple threads.""" | ||
threads = [ | ||
threading.Thread(target=target, args=(i,) if pass_index else ()) | ||
for i in range(num_threads) | ||
] | ||
for t in threads: | ||
t.start() | ||
for t in threads: | ||
t.join() | ||
|
||
|
||
class MockArrowQueue: | ||
"""Mock queue that behaves like ArrowQueue but returns empty results.""" | ||
|
||
def __init__(self): | ||
# Create an empty arrow table if pyarrow is available, otherwise use None | ||
if pa is not None: | ||
self.empty_table = pa.table({'column': pa.array([])}) | ||
else: | ||
# Create a simple mock table-like object | ||
self.empty_table = MagicMock() | ||
self.empty_table.num_rows = 0 | ||
self.empty_table.num_columns = 0 | ||
|
||
def next_n_rows(self, num_rows: int): | ||
"""Return empty results.""" | ||
return self.empty_table | ||
|
||
def remaining_rows(self): | ||
"""Return empty results.""" | ||
return self.empty_table | ||
|
||
|
||
def test_concurrent_queries_with_telemetry_capture(): | ||
""" | ||
Test showing concurrent threads executing queries with real telemetry capture. | ||
Uses the actual Connection and Cursor classes, mocking only the ThriftBackend. | ||
""" | ||
num_threads = 5 | ||
captured_telemetry = [] | ||
connections = [] # Store connections to close them later | ||
connections_lock = threading.Lock() # Thread safety for connections list | ||
|
||
def mock_send_telemetry(self, events): | ||
"""Capture telemetry events instead of sending them over network.""" | ||
captured_telemetry.extend(events) | ||
|
||
# Clean up any existing state | ||
if TelemetryClientFactory._executor: | ||
TelemetryClientFactory._executor.shutdown(wait=True) | ||
TelemetryClientFactory._clients.clear() | ||
TelemetryClientFactory._executor = None | ||
TelemetryClientFactory._initialized = False | ||
|
||
with patch.object(TelemetryClient, '_send_telemetry', mock_send_telemetry): | ||
# Mock the ThriftBackend to avoid actual network calls | ||
with patch.object(ThriftBackend, 'open_session') as mock_open_session, \ | ||
patch.object(ThriftBackend, 'execute_command') as mock_execute_command, \ | ||
patch.object(ThriftBackend, 'close_session') as mock_close_session, \ | ||
patch.object(ThriftBackend, 'fetch_results') as mock_fetch_results, \ | ||
patch.object(ThriftBackend, 'close_command') as mock_close_command, \ | ||
patch.object(ThriftBackend, 'handle_to_hex_id') as mock_handle_to_hex_id, \ | ||
patch('databricks.sql.auth.thrift_http_client.THttpClient.open') as mock_transport_open: | ||
|
||
# Mock transport.open() to prevent actual network connection | ||
mock_transport_open.return_value = None | ||
|
||
# Set up mock responses with proper structure | ||
mock_handle_identifier = THandleIdentifier() | ||
mock_handle_identifier.guid = b'1234567890abcdef' | ||
mock_handle_identifier.secret = b'test_secret_1234' | ||
|
||
mock_session_handle = TSessionHandle() | ||
mock_session_handle.sessionId = mock_handle_identifier | ||
mock_session_handle.serverProtocolVersion = 1 | ||
|
||
mock_open_session.return_value = MagicMock( | ||
sessionHandle=mock_session_handle, | ||
serverProtocolVersion=1 | ||
) | ||
|
||
mock_handle_to_hex_id.return_value = "test-session-id-12345678" | ||
|
||
mock_op_handle = TOperationHandle() | ||
mock_op_handle.operationId = THandleIdentifier() | ||
mock_op_handle.operationId.guid = b'abcdef1234567890' | ||
mock_op_handle.operationId.secret = b'op_secret_abcd' | ||
|
||
# Create proper mock arrow_queue with required methods | ||
mock_arrow_queue = MockArrowQueue() | ||
|
||
mock_execute_response = ExecuteResponse( | ||
arrow_queue=mock_arrow_queue, | ||
description=[], | ||
command_handle=mock_op_handle, | ||
status=TOperationState.FINISHED_STATE, | ||
has_been_closed_server_side=False, | ||
has_more_rows=False, | ||
lz4_compressed=False, | ||
arrow_schema_bytes=b'', | ||
is_staging_operation=False | ||
) | ||
mock_execute_command.return_value = mock_execute_response | ||
|
||
# Mock fetch_results to return empty results | ||
mock_fetch_results.return_value = (mock_arrow_queue, False) | ||
|
||
# Mock close_command to do nothing | ||
mock_close_command.return_value = None | ||
|
||
# Mock close_session to do nothing | ||
mock_close_session.return_value = None | ||
|
||
def execute_query_worker(thread_id): | ||
"""Each thread creates a connection and executes a query.""" | ||
|
||
# Create real Connection and Cursor objects | ||
conn = Connection( | ||
server_hostname="test-host", | ||
http_path="/test/path", | ||
access_token="test-token", | ||
enable_telemetry=True | ||
) | ||
|
||
# Thread-safe storage of connection | ||
with connections_lock: | ||
connections.append(conn) | ||
|
||
cursor = conn.cursor() | ||
# This will trigger the @log_latency decorator naturally | ||
cursor.execute(f"SELECT {thread_id} as thread_id") | ||
result = cursor.fetchall() | ||
conn.close() | ||
|
||
|
||
run_in_threads(execute_query_worker, num_threads, pass_index=True) | ||
|
||
# We expect at least 2 events per thread (one for open_session and one for execute_command) | ||
assert len(captured_telemetry) >= num_threads*2 | ||
print(f"Captured telemetry: {captured_telemetry}") | ||
|
||
# Verify the decorator was used (check some telemetry events have latency measurement) | ||
events_with_latency = [ | ||
e for e in captured_telemetry | ||
if hasattr(e, 'entry') and hasattr(e.entry, 'sql_driver_log') | ||
and e.entry.sql_driver_log.operation_latency_ms is not None | ||
] | ||
assert len(events_with_latency) >= num_threads | ||
|
||
# Verify we have events with statement IDs (indicating @log_latency decorator worked) | ||
events_with_statements = [ | ||
e for e in captured_telemetry | ||
if hasattr(e, 'entry') and hasattr(e.entry, 'sql_driver_log') | ||
and e.entry.sql_driver_log.sql_statement_id is not None | ||
] | ||
assert len(events_with_statements) >= num_threads | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add these in a separate file? @jprakash-db what's the sop in python?