Skip to content

feat: Add basic lock-free metrics batcher - #8326

Open
alexander-alderman-webb wants to merge 26 commits into
webb/lock-free-basefrom
webb/sliding-window-metrics-batcher
Open

alexander-alderman-webb wants to merge 26 commits into
webb/lock-free-basefrom
webb/sliding-window-metrics-batcher

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Add a lock-free metrics batcher using a collections.deque at the core.
Consumers pop from the front of the queue, and producers add to the end of the queue.

The main trade-off is that limits are not precise anymore. The MAX_BEFORE_FLUSH limit can now be exceeded by one or more metrics per thread (conceptually, imagine all threads adding their metric simultaneously as the worst case).

There can be two consumers if sentry_sdk.flush() is called concurrently with a flush induced by reaching a limit. Consumers therefore handle IndexError.

Issues

Reminders

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 59448 passed | ❌ 1 failed | ⏭️ 3118 skipped | Total: 62567 | Pass Rate: 95.01% | Execution Time: 161m 59s

📊 Comparison with Base Branch

Metric Change
Total Tests 📉 -9
Passed Tests 📉 -9
Failed Tests 📈 +1
Skipped Tests 📉 -1

➕ New Tests (1)

View new tests
  • test_execution_outcome[start_sync_execution-FAILED-stepfunctions_execution_failed]
    • File: tests.integrations.boto3.test_stepfunctions
    • Status: ❌ Failing

❌ Failed Tests

test_execution_outcome[start_sync_execution-FAILED-stepfunctions_execution_failed]

File: tests.integrations.boto3.test_stepfunctions
Suite: py3.10-boto3-v1.44.4
Error: assert '4242' not in "{'trace_id': 'a85247f3d7c4414fb28afc16dbf95a5f', 'span_id': 'bfb8ab85cc320c24', 'name': 'SFN.StartSyncExecution', 'status': 'error', 'is_segment': False, 'start_timestamp': 1796044191.423233, 'end_timestamp': 1796044191.424294, 'parent_span_id': 'a84b478f69c0801d', 'attributes': {'sentry.origin': 'auto.aws.boto3', 'sentry.trace_lifecycle': 'stream', 'cloud.provider': 'aws', 'rpc.method': 'StartSyncExecution', 'rpc.system.name': 'aws-api', 'sentry.kind': 'client', 'rpc.servi... [+5 more lines; full report in element text]

Stack Trace
tests/integrations/boto3/test_stepfunctions.py:152: in test_execution_outcome
    assert "4242" not in str(span)
E   assert '4242' not in "{'trace_id': 'a85247f3d7c4414fb28afc16dbf95a5f', 'span_id': 'bfb8ab85cc320c24', 'name': 'SFN.StartSyncExecution', 'status': 'error', 'is_segment': False, 'start_timestamp': 1796044191.423233, 'end_timestamp': 1796044191.424294, 'parent_span_id': 'a84b478f69c0801d', 'attributes': {'sentry.origin': 'auto.aws.boto3', 'sentry.trace_lifecycle': 'stream', 'cloud.provider': 'aws', 'rpc.method': 'StartSyncExecution', 'rpc.system.name': 'aws-api', 'sentry.kind': 'client', 'rpc.service': 'SFN', 'cloud.region': 'eu-north-1', 'server.address': 'states.eu-north-1.amazonaws.com', 'server.port': 443, 'aws.step_functions.state_machine.arn': 'arn:aws:states:eu-north-1:123456789012:stateMachine:orders', 'cloud.resource_id': 'arn:aws:states:eu-north-1:123456789012:stateMachine:orders', 'cloud.account.id': '123456789012', 'aws.step_functions.state_machine.name': 'orders', 'sentry.op': 'rpc', 'thread.id': '139709100994560', 'thread.name': 'MainThread', 'aws.step_functions.execution.arn': 'arn:aws:states:eu-north-1:123456789012:execution:orders:order-1234', 'error.type': 'stepfunctions_execution_failed', 'sentry.segment.id': 'a84b478f69c0801d', 'sentry.segment.name': 'parent', 'sentry.sdk.name': 'sentry.python', 'sentry.sdk.version': '3.0.2', 'process.runtime.name': 'CPython', 'process.runtime.version': '3.10.12', 'sentry.environment': 'production', 'sentry.release': 'c14262fc9a0af8bbfe5de9421300953c0dbb1164'}}"
E     
E     '4242' is contained here:
E     ?                                                                                                                                                                                                                             ^^^^^^^^^^^^^^^^^^^^^
E       {'trace_id': 'a85247f3d7c4414fb28afc16dbf95a5f', 'span_id': 'bfb8ab85cc320c24', 'name': 'SFN.StartSyncExecution', 'status': 'error', 'is_segment': False, 'start_timestamp': 1796044191.423233, 'end_timestamp': 1796044191.424294, 'parent_span_id': 'a84b478f69c0801d', 'attributes': {'sentry.origin': 'auto.aws.boto3', 'sentry.trace_lifecycle': 'stream', 'cloud.provider': 'aws', 'rpc.method': 'StartSyncExecution', 'rpc.system.name': 'aws-api', 'sentry.kind': 'client', 'rpc.service': 'SFN', 'cloud.region': 'eu-north-1', 'server.address': 'states.eu-north-1.amazonaws.com', 'server.port': 443, 'aws.step_functions.state_machine.arn': 'arn:aws:states:eu-north-1:123456789012:stateMachine:orders', 'cloud.resource_id': 'arn:aws:states:eu-north-1:123456789012:stateMachine:orders', 'cloud.account.id': '123456789012', 'aws.step_functions.state_machine.name': 'orders', 'sentry.op': 'rpc', 'thread.id': '139709100994560', 'thread.name': 'MainThread', 'aws.step_functions.execution.arn': 'arn:aws:states:eu-north-1:123456789012:execution:orders:order-1234', 'error.type': 'stepfunctions_execution_failed', 'sentry.segment.id': 'a84b478f69c0801d', 'sentry.segment.name': 'parent', 'sentry.sdk.name': 'sentry.python', 'sentry.sdk.version': '3.0.2', 'process.runtime.name': 'CPython', 'process.runtime.version': '3.10.12', 'sentry.environment': 'production', 'sentry.release': 'c14262fc9a0af8bbfe5de9421300953c0dbb1164'}}
E     ?                                                                                                                                                                                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^

❌ Patch coverage is 0.00% (0 of 81 changed executable lines covered; target 80%).
Project statement coverage is 89.81% (down 0.31 percentage points from base (5dee353) to head (b976ff3)).

Changed files with executable lines (1)
File Patch coverage Changed executable lines
sentry_sdk/_lock_free_metrics_batcher.py 0.00% 0/81 covered; missed: 1, 2, 3, 4, 5, 7, 8, 9, 20, 21, 22, 23, 25, 26, 27, 29, 34, 35, 37, 39, 40, 41, 42, 44, 48, 49, 51, 52, 53, 56, 57, 58, 60, 62, 63, 65, 66, 67, 70, 71, 73, 75, 77, 78, 80, 81, 82, 83, 85, 86, 87, 88, 90, 91, 92, 94, 96, 98, 99, 100, 102, 103, 104, 105, 106, 108, 109, 111, 114, 115, 116, 118, 119, 120, 130, 131, 133, 134, 136, 137, 139
Coverage diff
@@            Coverage Diff             @@
##        master     #8326       +/-##
==========================================
- Coverage    90.12%    89.81%    -0.31%
==========================================
  Files          201       202        +1
  Tracked lines     22199     22345      +146
  Branches      7784      7820       +36
==========================================
+ Hits         20004     20067       +63
- Misses        2195      2278       +83
- Partials      1283      1289        +6

Generated by Coverage Action

@alexander-alderman-webb
alexander-alderman-webb changed the base branch from webb/lock-free-base to webb/deduplicate-register-at-fork November 18, 2026 12:38
@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review November 19, 2026 09:11
@alexander-alderman-webb
alexander-alderman-webb requested a review from a team as a code owner November 19, 2026 09:11

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alexander-alderman-webb
alexander-alderman-webb changed the base branch from webb/deduplicate-register-at-fork to webb/lock-free-base November 19, 2026 12:05

@sentrivana sentrivana left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment on lines +56 to +58
# dict.setdefault is atomic, so exactly one thread gets to start the flusher.
claim = object()
if self._flusher_claim.setdefault("flusher", claim) is not claim:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL of this setdefault hack

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 99641dc. Configure here.

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit b976ff3.

Main concerns: data sent to Sentry, host performance, reach.

This label is a hint for reviewers, not an approval.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants