-
Notifications
You must be signed in to change notification settings - Fork 7
fix(tracing): Fix memory leak in SGP tracing processors #302
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Empty file.
Empty file.
Empty file.
158 changes: 158 additions & 0 deletions
158
tests/lib/core/tracing/processors/test_sgp_tracing_processor.py
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,158 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import uuid | ||
| from datetime import UTC, datetime | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| from agentex.types.span import Span | ||
| from agentex.lib.types.tracing import SGPTracingProcessorConfig | ||
|
|
||
| MODULE = "agentex.lib.core.tracing.processors.sgp_tracing_processor" | ||
|
|
||
|
|
||
| def _make_config() -> SGPTracingProcessorConfig: | ||
| return SGPTracingProcessorConfig( | ||
| sgp_api_key="test-key", | ||
| sgp_account_id="test-account", | ||
| ) | ||
|
|
||
|
|
||
| def _make_span(span_id: str | None = None) -> Span: | ||
| return Span( | ||
| id=span_id or str(uuid.uuid4()), | ||
| name="test-span", | ||
| start_time=datetime.now(UTC), | ||
| trace_id="trace-1", | ||
| ) | ||
|
|
||
|
|
||
| def _make_mock_sgp_span() -> MagicMock: | ||
| sgp_span = MagicMock() | ||
| sgp_span.to_request_params.return_value = {"mock": "params"} | ||
| sgp_span.start_time = None | ||
| sgp_span.end_time = None | ||
| sgp_span.output = None | ||
| sgp_span.metadata = None | ||
| return sgp_span | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Sync processor tests | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class TestSGPSyncTracingProcessorMemoryLeak: | ||
| @patch(f"{MODULE}.EnvironmentVariables") | ||
| @patch(f"{MODULE}.flush_queue") | ||
| @patch(f"{MODULE}.create_span") | ||
| @patch(f"{MODULE}.SGPClient") | ||
| @patch(f"{MODULE}.tracing") | ||
| def _make_processor(self, mock_tracing, mock_sgp_client, mock_create_span, mock_flush, mock_env): | ||
| mock_env.refresh.return_value = MagicMock(ACP_TYPE=None, AGENT_NAME=None, AGENT_ID=None) | ||
| mock_create_span.side_effect = lambda **kwargs: _make_mock_sgp_span() | ||
|
|
||
| from agentex.lib.core.tracing.processors.sgp_tracing_processor import ( | ||
| SGPSyncTracingProcessor, | ||
| ) | ||
|
|
||
| return SGPSyncTracingProcessor(_make_config()), mock_create_span | ||
|
|
||
| def test_spans_not_leaked_after_completed_lifecycle(self): | ||
| processor, _ = self._make_processor() | ||
|
|
||
| for _ in range(100): | ||
| span = _make_span() | ||
| processor.on_span_start(span) | ||
| span.end_time = datetime.now(UTC) | ||
| processor.on_span_end(span) | ||
|
|
||
| assert len(processor._spans) == 0, ( | ||
| f"Expected 0 spans after 100 complete lifecycles, got {len(processor._spans)} — memory leak!" | ||
| ) | ||
|
|
||
| def test_spans_present_during_active_lifecycle(self): | ||
| processor, _ = self._make_processor() | ||
|
|
||
| span = _make_span() | ||
| processor.on_span_start(span) | ||
| assert len(processor._spans) == 1, "Span should be tracked while active" | ||
|
|
||
| span.end_time = datetime.now(UTC) | ||
| processor.on_span_end(span) | ||
| assert len(processor._spans) == 0, "Span should be removed after end" | ||
|
|
||
| def test_span_end_for_unknown_span_is_noop(self): | ||
| processor, _ = self._make_processor() | ||
|
|
||
| span = _make_span() | ||
| # End a span that was never started — should not raise | ||
| span.end_time = datetime.now(UTC) | ||
| processor.on_span_end(span) | ||
|
|
||
| assert len(processor._spans) == 0 | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Async processor tests | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class TestSGPAsyncTracingProcessorMemoryLeak: | ||
| @staticmethod | ||
| def _make_processor(): | ||
| mock_env = MagicMock() | ||
| mock_env.refresh.return_value = MagicMock(ACP_TYPE=None, AGENT_NAME=None, AGENT_ID=None) | ||
| mock_create_span = MagicMock(side_effect=lambda **kwargs: _make_mock_sgp_span()) | ||
|
|
||
| mock_async_client = MagicMock() | ||
| mock_async_client.spans.upsert_batch = AsyncMock() | ||
|
|
||
| with patch(f"{MODULE}.EnvironmentVariables", mock_env), \ | ||
| patch(f"{MODULE}.create_span", mock_create_span), \ | ||
| patch(f"{MODULE}.AsyncSGPClient", return_value=mock_async_client): | ||
| from agentex.lib.core.tracing.processors.sgp_tracing_processor import ( | ||
| SGPAsyncTracingProcessor, | ||
| ) | ||
|
|
||
| processor = SGPAsyncTracingProcessor(_make_config()) | ||
|
|
||
| # Wire up the mock client after construction (constructor stores it) | ||
| processor.sgp_async_client = mock_async_client | ||
|
|
||
| # Keep create_span mock active for on_span_start calls | ||
| return processor, mock_create_span | ||
|
|
||
| async def test_spans_not_leaked_after_completed_lifecycle(self): | ||
| processor, _ = self._make_processor() | ||
|
|
||
| with patch(f"{MODULE}.create_span", side_effect=lambda **kw: _make_mock_sgp_span()): | ||
| for _ in range(100): | ||
| span = _make_span() | ||
| await processor.on_span_start(span) | ||
| span.end_time = datetime.now(UTC) | ||
| await processor.on_span_end(span) | ||
|
|
||
| assert len(processor._spans) == 0, ( | ||
| f"Expected 0 spans after 100 complete lifecycles, got {len(processor._spans)} — memory leak!" | ||
| ) | ||
|
|
||
| async def test_spans_present_during_active_lifecycle(self): | ||
| processor, _ = self._make_processor() | ||
|
|
||
| with patch(f"{MODULE}.create_span", side_effect=lambda **kw: _make_mock_sgp_span()): | ||
| span = _make_span() | ||
| await processor.on_span_start(span) | ||
| assert len(processor._spans) == 1, "Span should be tracked while active" | ||
|
|
||
| span.end_time = datetime.now(UTC) | ||
| await processor.on_span_end(span) | ||
| assert len(processor._spans) == 0, "Span should be removed after end" | ||
|
|
||
| async def test_span_end_for_unknown_span_is_noop(self): | ||
| processor, _ = self._make_processor() | ||
|
|
||
| span = _make_span() | ||
| span.end_time = datetime.now(UTC) | ||
| await processor.on_span_end(span) | ||
|
|
||
| assert len(processor._spans) == 0 | ||
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.