Skip to content

Conversation

@ntindle
Copy link
Member

@ntindle ntindle commented Oct 15, 2025

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:

  • I have clearly listed my changes in the PR description
  • I have made a test plan
  • I have tested my changes according to the test plan:
    • Test to make sure the userid and block details show up in Sentry
    • make sure other errors aren't contaminated

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.

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.
@ntindle ntindle requested a review from a team as a code owner October 15, 2025 03:13
@ntindle ntindle requested review from 0ubbe and majdyz and removed request for a team October 15, 2025 03:13
@github-project-automation github-project-automation bot moved this to 🆕 Needs initial review in AutoGPT development kanban Oct 15, 2025
@netlify
Copy link

netlify bot commented Oct 15, 2025

Deploy Preview for auto-gpt-docs-dev canceled.

Name Link
🔨 Latest commit 80dbcd5
🔍 Latest deploy log https://app.netlify.com/projects/auto-gpt-docs-dev/deploys/68ef194b9edd1c00086dec86

@netlify
Copy link

netlify bot commented Oct 15, 2025

Deploy Preview for auto-gpt-docs canceled.

Name Link
🔨 Latest commit 80dbcd5
🔍 Latest deploy log https://app.netlify.com/projects/auto-gpt-docs/deploys/68ef194bf3882d0009013a99

@github-actions github-actions bot added platform/backend AutoGPT Platform - Back end size/m labels Oct 15, 2025
@coderabbitai
Copy link

coderabbitai bot commented Oct 15, 2025

Note

Other AI code review bot(s) detected

CodeRabbit 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.

Walkthrough

Adds Sentry scoping around node execution, introduces a LaunchDarkly configuration check, and conditionally augments Sentry integrations with LaunchDarkly during metrics initialization.

Changes

Cohort / File(s) Summary of Changes
Executor Sentry scope wrapping
autogpt_platform/backend/backend/executor/manager.py
Wraps node execution in a Sentry scope, sets user/tags (user, graph, node, block, user_context), captures exceptions with scope, flushes, and restores original scope.
Feature flag configuration check
autogpt_platform/backend/backend/util/feature_flag.py
Adds is_configured() to report if LaunchDarkly SDK key is present via settings.secrets.launch_dark_darkly_sdk_key truthiness.
Metrics Sentry initialization updates
autogpt_platform/backend/backend/util/metrics.py
Dynamically builds Sentry integrations; when feature flags are configured, appends LaunchDarklyIntegration(get_client()) to integrations before sentry_init.

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
Loading
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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I thump my paw—alerts take flight,
Tags and scopes now snug and tight.
Flags that launch on moonlit breeze,
Metrics hum through forest trees.
If errors hop, we’ll catch their trail—
Sentry listens, never pale.
Carrots logged; all systems hale.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title clearly and succinctly describes the primary update—adding Sentry user and tag tracking to node execution in the backend—and follows conventional commit styling for clarity.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sentry-tags-blocks

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@qodo-merge-pro
Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Direct Scope Mutation

The code accesses and mutates private Sentry scope internals (_user, _tags). This is brittle across SDK versions and can lead to subtle bugs or context leaks. Prefer public APIs (scope.set_user(None), scope.set_tag, scope.set_context, with sentry_sdk.configure_scope(...)) or push_scope/pop_scope.

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)
scope.set_tag("node_exec_id", node_exec_id)
scope.set_tag("graph_exec_id", graph_exec_id)
for k, v in (data.user_context or {}).model_dump().items():
    scope.set_tag(f"user_context.{k}", v)

try:
    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
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}")

    # Update execution stats
    if execution_stats is not None:
        execution_stats += node_block.execution_stats
        execution_stats.input_size = input_size
        execution_stats.output_size = output_size

    # Restore scope AFTER error has been captured
    scope._user = original_user
    scope._tags = original_tags
Tag Value Types

Tags are set from data.user_context.model_dump() without normalization. Non-primitive values (lists, dicts) or large payloads may be dropped or cause noisy tag cardinality. Consider serializing to strings or using set_context for structured data.

for k, v in (data.user_context or {}).model_dump().items():
    scope.set_tag(f"user_context.{k}", v)
Flush in Exception Path

Calling sentry_sdk.flush() on every exception can block the event loop and degrade performance. Consider using async-safe capture or relying on transport buffering; if flush is needed, gate it behind severity or environment flags.

sentry_sdk.flush()  # Ensure it's sent before we restore scope
# Re-raise to maintain normal error flow

@deepsource-io
Copy link

deepsource-io bot commented Oct 15, 2025

Here's the code health analysis summary for commits 934cb3a..80dbcd5. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource JavaScript LogoJavaScript✅ SuccessView Check ↗
DeepSource Python LogoPython✅ Success
❗ 9 occurences introduced
🎯 4 occurences resolved
View Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

@AutoGPT-Agent
Copy link

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:

  1. Complete your test plan by checking off the remaining item ("make sure other errors aren't contaminated") or indicate if this test is no longer applicable.

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.

cursor[bot]

This comment was marked as outdated.

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.
@ntindle ntindle enabled auto-merge October 15, 2025 13:43
@Pwuts
Copy link
Member

Pwuts commented Oct 15, 2025

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Oct 15, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a 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_tags

Note:

  • 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.

📥 Commits

Reviewing files that changed from the base of the PR and between 934cb3a and 80dbcd5.

📒 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 gate

Works as intended; no issues.

@github-project-automation github-project-automation bot moved this from 🆕 Needs initial review to 👍🏼 Mergeable in AutoGPT development kanban Oct 15, 2025
@ntindle ntindle added this pull request to the merge queue Oct 15, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 15, 2025
@ntindle ntindle added this pull request to the merge queue Oct 15, 2025
Merged via the queue into dev with commit b230b1b Oct 15, 2025
59 checks passed
@ntindle ntindle deleted the sentry-tags-blocks branch October 15, 2025 14:51
@github-project-automation github-project-automation bot moved this from 👍🏼 Mergeable to ✅ Done in AutoGPT development kanban Oct 15, 2025
Abhi1992002 pushed a commit that referenced this pull request Oct 17, 2025
…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 -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants