Skip to content

fix(otel): chat root OTel span on all early-return paths#4265

Merged
icecrasher321 merged 1 commit intostagingfrom
fix/address-otel-comment
Apr 22, 2026
Merged

fix(otel): chat root OTel span on all early-return paths#4265
icecrasher321 merged 1 commit intostagingfrom
fix/address-otel-comment

Conversation

@icecrasher321
Copy link
Copy Markdown
Collaborator

Summary

Chat root OTel span on all early-return paths.

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 22, 2026 8:49pm

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented Apr 22, 2026

PR Summary

Low Risk
Telemetry-only change that alters span outcome/status behavior on early-return HTTP errors; low risk to runtime behavior but can affect dashboards/alerting semantics.

Overview
Ensures the unified chat POST handler consistently stamps http.status_code and calls otelRoot.finish('error') on early-return paths (resolveBranch validation failures, missing chat 404, and pending-stream collision 409).

For 409 conflicts, it now finishes with a synthesized Error so the root span escalates to OTel ERROR status (to surface in actionable-error dashboards), while non-actionable 4xx rejections are marked as outcome=error without triggering error alerts.

Reviewed by Cursor Bugbot for commit 0105f75. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 22, 2026

Greptile Summary

This PR plugs three OTel span leaks in handleUnifiedChatPost where early-return paths (resolveBranch 4xx, chat-not-found 404, and stream-lock 409) exited the handler without calling activeOtelRoot.finish(), leaving root spans open indefinitely. The fix stamps the HTTP status code attribute and finishes the span on each path before returning, using a bare finish('error') for expected validation rejections and an error-carrying finish('error', new Error(...)) for the actionable 409 case — a deliberate distinction to control dashboard alert noise.

Confidence Score: 5/5

Safe to merge — targeted, well-commented fix with no logic changes outside OTel instrumentation.

All three early-return paths are correctly instrumented, double-finish is not possible (early returns exit the async callback normally rather than throwing, so the catch block's otelRoot?.finish() is unreachable on those paths), and the only remaining path before otelRoot starts (the 401 session check) correctly has no span to close. No P0/P1 findings.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/lib/copilot/chat/post.ts Adds activeOtelRoot.setAttribute + finish('error') calls on three previously-uncovered early-return paths (resolveBranch 4xx, chat-not-found 404, stream-lock 409) to prevent span leaks.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[handleUnifiedChatPost] --> B{session valid?}
    B -- no --> C[return 401\nno otelRoot yet]
    B -- yes --> D[startCopilotOtelRoot]
    D --> E[otelContextApi.with]
    E --> F[resolveBranch]
    F -- NextResponse --> G["★ NEW: setAttribute(status)\nfinish('error')\nreturn branch"]
    F -- BranchConfig --> H[resolveOrCreateChat]
    H --> I{chatId && !currentChat?}
    I -- yes --> J["★ NEW: setAttribute(404)\nfinish('error')\nreturn 404"]
    I -- no --> K[acquirePendingChatStream]
    K --> L{lock acquired?}
    L -- no --> M["★ NEW: setAttribute(409)\nfinish('error', Error)\nreturn 409"]
    L -- yes --> N[build context, SSE stream]
    N --> O[return SSE Response\nspan finished inside stream]
    E -- throws --> P[catch: otelRoot?.finish error\nreturn 500]
Loading

Reviews (1): Last reviewed commit: "fix(otel): chat root OTel span on all ea..." | Re-trigger Greptile

@icecrasher321 icecrasher321 merged commit 8c9ddef into staging Apr 22, 2026
14 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/address-otel-comment branch April 24, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant