-
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
11
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
11 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 50e771b
actual e2e
saishreeeee ea86fe2
temp
saishreeeee 1355283
Merge branch 'telemetry' into telemetry-testing
saishreeeee c106b95
actual send telemetry
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,85 @@ | ||
import threading | ||
from unittest.mock import patch | ||
import pytest | ||
|
||
from databricks.sql.telemetry.telemetry_client import TelemetryClient, TelemetryClientFactory | ||
from tests.e2e.test_driver import PySQLPytestTestCase | ||
|
||
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 TestE2ETelemetry(PySQLPytestTestCase): | ||
|
||
@pytest.fixture(autouse=True) | ||
def telemetry_setup_teardown(self): | ||
""" | ||
This fixture ensures the TelemetryClientFactory is in a clean state | ||
before each test and shuts it down afterward. Using a fixture makes | ||
this robust and automatic. | ||
""" | ||
# --- SETUP --- | ||
if TelemetryClientFactory._executor: | ||
TelemetryClientFactory._executor.shutdown(wait=True) | ||
TelemetryClientFactory._clients.clear() | ||
TelemetryClientFactory._executor = None | ||
TelemetryClientFactory._initialized = False | ||
|
||
yield # This is where the test runs | ||
|
||
# --- TEARDOWN --- | ||
if TelemetryClientFactory._executor: | ||
TelemetryClientFactory._executor.shutdown(wait=True) | ||
TelemetryClientFactory._executor = None | ||
TelemetryClientFactory._initialized = False | ||
|
||
def test_concurrent_queries_sends_telemetry(self): | ||
""" | ||
An E2E test where concurrent threads execute real queries against | ||
the staging endpoint, while we capture and verify the generated telemetry. | ||
""" | ||
num_threads = 5 | ||
captured_telemetry = [] | ||
captured_telemetry_lock = threading.Lock() | ||
captured_responses = [] | ||
captured_responses_lock = threading.Lock() | ||
|
||
original_send_telemetry = TelemetryClient._send_telemetry | ||
original_callback = TelemetryClient._telemetry_request_callback | ||
|
||
def send_telemetry_wrapper(self_client, events): | ||
with captured_telemetry_lock: | ||
captured_telemetry.extend(events) | ||
original_send_telemetry(self_client, events) | ||
|
||
with patch.object(TelemetryClient, "_send_telemetry", send_telemetry_wrapper): | ||
|
||
def execute_query_worker(thread_id): | ||
"""Each thread creates a connection and executes a query.""" | ||
with self.connection(extra_params={"enable_telemetry": True}) as conn: | ||
with conn.cursor() as cursor: | ||
cursor.execute(f"SELECT {thread_id}") | ||
cursor.fetchall() | ||
|
||
# Run the workers concurrently | ||
run_in_threads(execute_query_worker, num_threads, pass_index=True) | ||
|
||
if TelemetryClientFactory._executor: | ||
TelemetryClientFactory._executor.shutdown(wait=True) | ||
|
||
# --- VERIFICATION --- | ||
assert len(captured_telemetry) == num_threads * 3 # 4 events per thread (initial_telemetry_log, 2 latency_logs (execute, fetchall)) | ||
|
||
events_with_latency = [ | ||
e for e in captured_telemetry | ||
if e.entry.sql_driver_log.operation_latency_ms is not None and e.entry.sql_driver_log.sql_statement_id is not None | ||
] | ||
assert len(events_with_latency) == num_threads * 2 # 2 events per thread (execute, fetchall) |
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?