-
Notifications
You must be signed in to change notification settings - Fork 23
Propagate OpenTelemetry context on inference API calls MCDB-100371 #132
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b31f571
Propagate OpenTelemetry context on inference API calls.
ajha-ss 4e6f3ea
Move helper functions below top-level imports to fix flake8 E402
ajha-ss 1d3ecf7
Default OpenAI httpx clients to 600s timeout and inherit caller timeout
ajha-ss f3a4109
Test SingleStoreChatFactory timeout defaults and inheritance
ajha-ss cbe5f25
copilot review comments
ajha-ss 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
Some comments aren't visible on the classic Files Changed page.
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,102 @@ | ||
| #!/usr/bin/env python | ||
| # type: ignore | ||
| """SingleStoreChatFactory OTel header injection tests.""" | ||
| import unittest | ||
| from types import ModuleType | ||
| from unittest.mock import patch | ||
|
|
||
| try: | ||
| import httpx | ||
| from singlestoredb.ai import chat as chat_mod | ||
| except ImportError: | ||
| httpx = None | ||
| chat_mod = None | ||
|
|
||
|
|
||
| @unittest.skipIf(chat_mod is None, 'singlestoredb.ai.chat dependencies missing') | ||
| class TestInjectOtelHeaders(unittest.TestCase): | ||
|
|
||
| def test_calls_otel_inject(self): | ||
| headers = {} | ||
|
|
||
| def fake_inject(target): | ||
| target['baggage'] = 'session=abc,turn=def' | ||
|
|
||
| fake_propagate = ModuleType('opentelemetry.propagate') | ||
| fake_propagate.inject = fake_inject | ||
| fake_otel = ModuleType('opentelemetry') | ||
| fake_otel.__path__ = [] # mark as package for submodules | ||
| with patch.dict( | ||
| 'sys.modules', | ||
| { | ||
| 'opentelemetry': fake_otel, | ||
| 'opentelemetry.propagate': fake_propagate, | ||
| }, | ||
| ): | ||
| chat_mod._inject_otel_headers(headers) | ||
| self.assertEqual(headers['baggage'], 'session=abc,turn=def') | ||
|
|
||
| def test_httpx_hook_appends_once(self): | ||
| client = httpx.Client() | ||
| try: | ||
| chat_mod._attach_otel_request_hook(client) | ||
| chat_mod._attach_otel_request_hook(client) | ||
| self.assertEqual( | ||
| client.event_hooks['request'].count(chat_mod._httpx_inject_otel), | ||
| 1, | ||
| ) | ||
| finally: | ||
| client.close() | ||
|
|
||
| @patch('singlestoredb.ai.chat.ChatOpenAI') | ||
| def test_openai_default_timeout_is_600(self, mock_chat_openai): | ||
| chat_mod.SingleStoreChatFactory( | ||
| model_name='gpt-4o', | ||
| base_url='https://example.com', | ||
| hosting_platform='OpenAI', | ||
| ) | ||
| self.assertTrue(mock_chat_openai.called) | ||
| _, kwargs = mock_chat_openai.call_args | ||
| http_client = kwargs.get('http_client') | ||
| http_async_client = kwargs.get('http_async_client') | ||
| try: | ||
| self.assertIsNotNone(http_client) | ||
| self.assertEqual(http_client.timeout.read, 600.0) | ||
| self.assertEqual(http_client.timeout.connect, 5.0) | ||
| self.assertIsNotNone(http_async_client) | ||
| self.assertEqual(http_async_client.timeout.read, 600.0) | ||
| self.assertEqual(http_async_client.timeout.connect, 5.0) | ||
| finally: | ||
| if http_client: | ||
| http_client.close() | ||
| if http_async_client: | ||
| import asyncio | ||
| asyncio.run(http_async_client.aclose()) | ||
|
|
||
| @patch('singlestoredb.ai.chat.ChatOpenAI') | ||
| def test_openai_inherits_custom_client_timeout(self, mock_chat_openai): | ||
| custom_sync = httpx.Client(timeout=httpx.Timeout(42.0)) | ||
| http_async_client = None | ||
| try: | ||
| chat_mod.SingleStoreChatFactory( | ||
| model_name='gpt-4o', | ||
| base_url='https://example.com', | ||
| hosting_platform='OpenAI', | ||
| http_client=custom_sync, | ||
| ) | ||
| self.assertTrue(mock_chat_openai.called) | ||
| _, kwargs = mock_chat_openai.call_args | ||
| http_client = kwargs.get('http_client') | ||
| http_async_client = kwargs.get('http_async_client') | ||
| self.assertIs(http_client, custom_sync) | ||
| self.assertIsNotNone(http_async_client) | ||
| self.assertEqual(http_async_client.timeout.read, 42.0) | ||
| finally: | ||
| custom_sync.close() | ||
| if http_async_client: | ||
| import asyncio | ||
| asyncio.run(http_async_client.aclose()) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
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.
Uh oh!
There was an error while loading. Please reload this page.