-
Notifications
You must be signed in to change notification settings - Fork 46.1k
feat(backend): Add Sentry user and tag tracking to node execution #11170
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
Conversation
Integrates Sentry SDK to set user and contextual tags during node execution for improved error tracking and user count analytics. Ensures Sentry context is properly set and restored, and exceptions are captured with relevant context before scope restoration.
✅ Deploy Preview for auto-gpt-docs-dev canceled.
|
✅ Deploy Preview for auto-gpt-docs canceled.
|
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughAdds Sentry scoping around node execution, introduces a LaunchDarkly configuration check, and conditionally augments Sentry integrations with LaunchDarkly during metrics initialization. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Exec as Executor
participant Sentry as Sentry SDK
participant Node as Node Logic
Exec->>Sentry: push_scope()
Note right of Sentry: Save original scope
Exec->>Sentry: set_user / set_tags (user, graph, node, block, user_context)
Exec->>Node: execute()
alt exception thrown
Node-->>Exec: raise Exception
Exec->>Sentry: capture_exception(e)
Exec->>Sentry: flush()
end
Exec->>Sentry: pop/restore scope
sequenceDiagram
autonumber
participant Metrics as metrics.sentry_init
participant FF as feature_flag.is_configured
participant LD as LaunchDarklyIntegration
participant Sentry as sentry_sdk.init
Metrics->>FF: is_configured()
alt LaunchDarkly configured
Metrics->>LD: new(get_client())
Metrics->>Sentry: init(integrations += [LD])
else not configured
Metrics->>Sentry: init(integrations = default)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
Here's the code health analysis summary for commits Analysis Summary
|
|
Thanks for adding Sentry user and tag tracking to node execution. This will be valuable for error tracking and analytics. Before we can merge this PR, please:
The implementation looks solid - I like how you're saving and restoring the original scope to prevent context contamination, and the flushing of errors before scope restoration ensures the context is captured correctly. |
Ensures that if user_context is None, a default UserContext with timezone set to 'UTC' is used when setting Sentry scope tags in execute_node.
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
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.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
autogpt_platform/backend/backend/executor/manager.py (1)
229-276: Fix Sentry exception capture (invalid arg) and replace manual scope restore with push_scope()
- capture_exception(scope=scope) is invalid and will raise TypeError, masking the real error.
- Directly mutating scope._user/_tags is fragile and may leak context if finally work raises.
- Use push_scope(), coerce tag values to str, and keep flush bounded/non-blocking.
Apply this diff:
@@ - output_size = 0 - - # sentry tracking nonsense to get user counts for blocks because isolation scopes don't work :( - scope = sentry_sdk.get_current_scope() - - # save the tags - original_user = scope._user - original_tags = dict(scope._tags) if scope._tags else {} - # Set user ID for error tracking - scope.set_user({"id": user_id}) - - scope.set_tag("graph_id", graph_id) - scope.set_tag("node_id", node_id) - scope.set_tag("block_name", node_block.name) - scope.set_tag("block_id", node_block.id) - for k, v in (data.user_context or UserContext(timezone="UTC")).model_dump().items(): - scope.set_tag(f"user_context.{k}", v) - - try: + output_size = 0 + try: + with sentry_sdk.push_scope() as scope: + # Sentry user and block context for this execution + scope.set_user({"id": str(user_id)}) + scope.set_tag("graph_id", str(graph_id)) + scope.set_tag("node_id", str(node_id)) + scope.set_tag("block_name", str(node_block.name)) + scope.set_tag("block_id", str(node_block.id)) + uc = (data.user_context or UserContext(timezone="UTC")).model_dump() + for k, v in uc.items(): + if v is not None: + scope.set_tag(f"user_context.{k}", str(v)) async for output_name, output_data in node_block.execute( input_data, **extra_exec_kwargs ): output_data = json.convert_pydantic_to_json(output_data) output_size += len(json.dumps(output_data)) log_metadata.debug("Node produced output", **{output_name: output_data}) yield output_name, output_data - except Exception: - # Capture exception WITH context still set before restoring scope - sentry_sdk.capture_exception(scope=scope) - sentry_sdk.flush() # Ensure it's sent before we restore scope - # Re-raise to maintain normal error flow - raise + except Exception as e: + # Capture exception with the scope active + sentry_sdk.capture_exception(e) + try: + sentry_sdk.flush(timeout=2.0) + except Exception: + pass + raise finally: # Ensure credentials are released even if execution fails if creds_lock and (await creds_lock.locked()) and (await creds_lock.owned()): try: await creds_lock.release() except Exception as e: log_metadata.error(f"Failed to release credentials lock: {e}") @@ - # Restore scope AFTER error has been captured - scope._user = original_user - scope._tags = original_tagsNote:
- Removed the unprofessional comment.
- push_scope() prevents cross-execution contamination without manual restoration.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
autogpt_platform/backend/backend/executor/manager.py(4 hunks)autogpt_platform/backend/backend/util/feature_flag.py(1 hunks)autogpt_platform/backend/backend/util/metrics.py(3 hunks)
🔇 Additional comments (1)
autogpt_platform/backend/backend/util/feature_flag.py (1)
40-43: LGTM: simple, clear config gateWorks as intended; no issues.
…1170) Integrates Sentry SDK to set user and contextual tags during node execution for improved error tracking and user count analytics. Ensures Sentry context is properly set and restored, and exceptions are captured with relevant context before scope restoration. <!-- Clearly explain the need for these changes: --> ### Changes 🏗️ Adds sentry tracking to block failures <!-- Concisely describe all of the changes made in this pull request: --> ### Checklist 📋 #### For code changes: - [x] I have clearly listed my changes in the PR description - [x] I have made a test plan - [x] I have tested my changes according to the test plan: <!-- Put your test plan here: --> - [x] Test to make sure the userid and block details show up in Sentry - [x] make sure other errors aren't contaminated <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Added conditional support for feature flags when configured, enabling targeted rollouts and experiments without impacting unconfigured environments. - Chores - Enhanced error monitoring with richer contextual data during node execution to improve stability and diagnostics. - Updated metrics initialization to dynamically include feature flag integrations when available, without altering behavior for unconfigured setups. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Integrates Sentry SDK to set user and contextual tags during node execution for improved error tracking and user count analytics. Ensures Sentry context is properly set and restored, and exceptions are captured with relevant context before scope restoration.
Changes 🏗️
Adds sentry tracking to block failures
Checklist 📋
For code changes:
Summary by CodeRabbit
New Features
Chores