-
Notifications
You must be signed in to change notification settings - Fork 7
batch SGP span upserts #331
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
alvinkam2001
wants to merge
5
commits into
main
Choose a base branch
from
alvinkam/sgp-batched-upsert
base: main
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
5 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
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 |
|---|---|---|
|
|
@@ -95,29 +95,40 @@ async def _drain_loop(self) -> None: | |
|
|
||
| @staticmethod | ||
| async def _process_items(items: list[_SpanQueueItem]) -> None: | ||
| """Process a list of span events concurrently.""" | ||
| """Dispatch a batch of same-event-type items to each processor in one call. | ||
|
|
||
| async def _handle(item: _SpanQueueItem) -> None: | ||
| Groups spans by processor so each processor sees its full slice of the | ||
| drain batch at once. Processors that override the batched methods can | ||
| then send a single HTTP request per drain cycle instead of N. | ||
| """ | ||
| if not items: | ||
| return | ||
|
|
||
| event_type = items[0].event_type | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are all the event_types the same?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe they should be but added an assert to check |
||
| assert all(i.event_type == event_type for i in items), ( | ||
| "_process_items requires all items to share the same event_type; " | ||
| "callers must split START and END batches before dispatching." | ||
| ) | ||
| by_processor: dict[AsyncTracingProcessor, list[Span]] = {} | ||
| for item in items: | ||
| for p in item.processors: | ||
| by_processor.setdefault(p, []).append(item.span) | ||
|
|
||
| async def _handle(p: AsyncTracingProcessor, spans: list[Span]) -> None: | ||
| try: | ||
| if item.event_type == SpanEventType.START: | ||
| coros = [p.on_span_start(item.span) for p in item.processors] | ||
| if event_type == SpanEventType.START: | ||
| await p.on_spans_start(spans) | ||
| else: | ||
| coros = [p.on_span_end(item.span) for p in item.processors] | ||
| results = await asyncio.gather(*coros, return_exceptions=True) | ||
| for result in results: | ||
| if isinstance(result, Exception): | ||
| logger.error( | ||
| "Tracing processor error during %s for span %s", | ||
| item.event_type.value, | ||
| item.span.id, | ||
| exc_info=result, | ||
| ) | ||
| await p.on_spans_end(spans) | ||
| except Exception: | ||
| logger.exception( | ||
| "Unexpected error in span queue for span %s", item.span.id | ||
| "Tracing processor %s failed handling %d spans during %s", | ||
| type(p).__name__, | ||
| len(spans), | ||
| event_type.value, | ||
| ) | ||
|
|
||
| await asyncio.gather(*[_handle(item) for item in items]) | ||
| await asyncio.gather(*[_handle(p, spans) for p, spans in by_processor.items()]) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Shutdown | ||
|
|
||
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
98 changes: 98 additions & 0 deletions
98
tests/lib/core/tracing/processors/test_tracing_processor_interface.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,98 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import uuid | ||
| import logging | ||
| from typing import override | ||
| from datetime import UTC, datetime | ||
|
|
||
| from agentex.types.span import Span | ||
| from agentex.lib.types.tracing import TracingProcessorConfig | ||
| from agentex.lib.core.tracing.processors.tracing_processor_interface import ( | ||
| AsyncTracingProcessor, | ||
| ) | ||
|
|
||
|
|
||
| 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", | ||
| ) | ||
|
|
||
|
|
||
| class _RecordingProcessor(AsyncTracingProcessor): | ||
| """Test processor that records every on_span_* call and fails on demand.""" | ||
|
|
||
| def __init__(self, fail_ids: set[str] | None = None) -> None: | ||
| self.started_ids: list[str] = [] | ||
| self.ended_ids: list[str] = [] | ||
| self._fail_ids = fail_ids or set() | ||
|
|
||
| @override | ||
| async def on_span_start(self, span: Span) -> None: | ||
| self.started_ids.append(span.id) | ||
| if span.id in self._fail_ids: | ||
| raise RuntimeError(f"boom-start-{span.id}") | ||
|
|
||
| @override | ||
| async def on_span_end(self, span: Span) -> None: | ||
| self.ended_ids.append(span.id) | ||
| if span.id in self._fail_ids: | ||
| raise RuntimeError(f"boom-end-{span.id}") | ||
|
|
||
| @override | ||
| async def shutdown(self) -> None: | ||
| pass | ||
|
|
||
|
|
||
| class TestDefaultBatchedFanout: | ||
| """The default on_spans_start / on_spans_end in AsyncTracingProcessor must: | ||
| - dispatch to the single-span method for every span | ||
| - continue after individual failures (not short-circuit) | ||
| - log each failure individually | ||
| - not propagate exceptions to the caller | ||
| """ | ||
|
|
||
| async def test_on_spans_start_runs_every_span_despite_failures(self, caplog): | ||
| proc = _RecordingProcessor(fail_ids={"span-1"}) | ||
| spans = [_make_span(f"span-{i}") for i in range(3)] | ||
|
|
||
| with caplog.at_level(logging.ERROR): | ||
| # Must not raise, even though span-1 fails. | ||
| await proc.on_spans_start(spans) | ||
|
|
||
| # Every span's on_span_start was invoked | ||
| assert proc.started_ids == ["span-0", "span-1", "span-2"] | ||
|
|
||
| async def test_on_spans_start_logs_each_failure(self, caplog): | ||
| proc = _RecordingProcessor(fail_ids={"span-0", "span-2"}) | ||
| spans = [_make_span(f"span-{i}") for i in range(3)] | ||
|
|
||
| with caplog.at_level(logging.ERROR): | ||
| await proc.on_spans_start(spans) | ||
|
|
||
| # Two distinct error log records, one per failing span | ||
| error_records = [r for r in caplog.records if r.levelno == logging.ERROR] | ||
| messages = " ".join(r.getMessage() for r in error_records) | ||
| assert "span-0" in messages | ||
| assert "span-2" in messages | ||
|
|
||
| async def test_on_spans_end_runs_every_span_despite_failures(self, caplog): | ||
| proc = _RecordingProcessor(fail_ids={"span-1"}) | ||
| spans = [_make_span(f"span-{i}") for i in range(3)] | ||
|
|
||
| with caplog.at_level(logging.ERROR): | ||
| await proc.on_spans_end(spans) | ||
|
|
||
| assert proc.ended_ids == ["span-0", "span-1", "span-2"] | ||
|
|
||
| async def test_dummy_config_construction(self): | ||
| """AsyncTracingProcessor's __init__ is abstract — verify concrete | ||
| subclass above satisfies the interface.""" | ||
| _ = TracingProcessorConfig | ||
| proc = _RecordingProcessor() | ||
| await proc.on_spans_start([]) | ||
| await proc.on_spans_end([]) | ||
| assert proc.started_ids == [] | ||
| assert proc.ended_ids == [] |
Oops, something went wrong.
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.
Is it guaranteed that all these items are the same event-type?
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.
Nevermind, see we dispatch them ourselves