Skip to content

[APIP] Add error span to errors - #2989

Merged
Krishanx92 merged 2 commits into
wso2:mainfrom
DDH13:main.datadog.4
Jul 30, 2026
Merged

[APIP] Add error span to errors#2989
Krishanx92 merged 2 commits into
wso2:mainfrom
DDH13:main.datadog.4

Conversation

@DDH13

@DDH13 DDH13 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 76cb5ac0-ccfd-4237-877a-e17e5bea27c2

📥 Commits

Reviewing files that changed from the base of the PR and between 09a819c and 25ed8e7.

📒 Files selected for processing (8)
  • gateway/gateway-runtime/policy-engine/internal/constants/constants.go
  • gateway/gateway-runtime/policy-engine/internal/executor/chain.go
  • gateway/gateway-runtime/policy-engine/internal/executor/chain_span_status_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc_span_status_test.go
  • gateway/gateway-runtime/policy-engine/internal/tracing/span_status.go
  • gateway/gateway-runtime/policy-engine/internal/tracing/span_status_test.go
🚧 Files skipped from review as they are similar to previous changes (7)
  • gateway/gateway-runtime/policy-engine/internal/constants/constants.go
  • gateway/gateway-runtime/policy-engine/internal/executor/chain.go
  • gateway/gateway-runtime/policy-engine/internal/tracing/span_status.go
  • gateway/gateway-runtime/policy-engine/internal/tracing/span_status_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc_span_status_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go

📝 Walkthrough

Walkthrough

Adds terminal HTTP outcome classification and OpenTelemetry tracing across policy-chain execution and ext_proc phases. It standardizes failure reporting, distinguishes policy and upstream outcomes, prevents stale terminal state, and adds comprehensive span-status tests.

Changes

Terminal outcome tracing

Layer / File(s) Summary
Terminal outcome contract
gateway/gateway-runtime/policy-engine/internal/constants/constants.go, gateway/gateway-runtime/policy-engine/internal/tracing/*
Defines terminal status attributes and reasons, implements HTTP outcome recording, and tests status classification and span behavior.
Executor error instrumentation
gateway/gateway-runtime/policy-engine/internal/executor/*
Records condition-evaluation and parameter-cloning errors with standardized span descriptions across execution phases.
Execution context classification
gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go
Tracks generated responses and terminal outcomes, distinguishes policy errors, denials, upstream responses, and status overrides.
External processor outcome flow
gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go, gateway/gateway-runtime/policy-engine/internal/kernel/extproc_span_status_test.go
Records outcomes on phase and root spans, handles fatal and transport errors, prevents stale outcomes, and tests terminal-status scenarios.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ExternalProcessorServer
  participant handleProcessingPhase
  participant PolicyExecutionContext
  participant RecordHTTPOutcome
  ExternalProcessorServer->>handleProcessingPhase: process ext_proc phase
  handleProcessingPhase->>PolicyExecutionContext: resolve terminal outcome
  PolicyExecutionContext->>RecordHTTPOutcome: record status and terminal reason
  RecordHTTPOutcome->>ExternalProcessorServer: update phase/root span
Loading

Suggested reviewers: anugayan, renuka-fernando, malinthaprasan, tgtshanika, virajsalaka

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is missing entirely, so none of the required template sections are filled in. Add the template sections: Purpose, Goals, Approach, User stories, Documentation, Automation tests, Security checks, Samples, Related PRs, and Test environment.
Docstring Coverage ⚠️ Warning Docstring coverage is 35.14% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the core change: adding error/span tracing for policy-engine failures, though the phrasing is awkward.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies"


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go (1)

252-265: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Four near-identical outcome blocks; consider one helper.

The error-overwrite + success-stamp pair is copy-pasted across all four phase cases with only the comment differing. A small helper (e.g. recordPhaseOutcome(span trace.Span, ec *PolicyExecutionContext, resp *extprocv3.ProcessingResponse, err error)) would keep the four phases from drifting apart later. Behaviour as written looks correct.

Also applies to: 313-324, 367-378, 426-441

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go` around
lines 252 - 265, Extract the repeated terminal-outcome overwrite and phase-span
success recording into a shared helper, such as recordPhaseOutcome, using the
existing span, execution context, response, and error values. Replace the
duplicated blocks in all four phase cases with calls to this helper, preserving
the current behavior for fatal errors, successful responses, and root-span
stamping.
gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go (1)

335-373: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low value

Policy ImmediateResponse with an unset status silently skips the memo update.

If a policy returns an ImmediateResponse whose Status is nil/0, int(imm.GetStatus().GetCode()) yields 0, so the out.StatusCode != 0 guard at Line 369 leaves ec.terminal holding whatever an earlier phase memoized while the phase span gets a bare terminal.reason=policy_denied with no status. Defaulting to the status the translator actually emits (or 500) would keep the two spans consistent.

♻️ Possible hardening
 		} else {
 			// Every other ImmediateResponse originates from a policy returning
 			// policy.ImmediateResponse (auth denial, rate limit, guardrail, or a
 			// python-bridge fault).
+			status := int(imm.GetStatus().GetCode())
+			if status == 0 {
+				status = http.StatusInternalServerError
+			}
 			out = tracing.HTTPOutcome{
-				StatusCode: int(imm.GetStatus().GetCode()),
+				StatusCode: status,
 				Reason:     constants.TerminalReasonPolicyDenied,
 			}
 		}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go`
around lines 335 - 373, Update resolveTerminalOutcome so policy
ImmediateResponse results with a nil or zero status use the status emitted by
the translator, falling back to 500 when no emitted status is available. Ensure
the resulting nonzero StatusCode is assigned to ec.terminal, keeping the phase
span and terminal memo consistent while preserving existing handling for
explicit statuses.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go`:
- Around line 335-373: Update resolveTerminalOutcome so policy ImmediateResponse
results with a nil or zero status use the status emitted by the translator,
falling back to 500 when no emitted status is available. Ensure the resulting
nonzero StatusCode is assigned to ec.terminal, keeping the phase span and
terminal memo consistent while preserving existing handling for explicit
statuses.

In `@gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go`:
- Around line 252-265: Extract the repeated terminal-outcome overwrite and
phase-span success recording into a shared helper, such as recordPhaseOutcome,
using the existing span, execution context, response, and error values. Replace
the duplicated blocks in all four phase cases with calls to this helper,
preserving the current behavior for fatal errors, successful responses, and
root-span stamping.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: cba3b6e8-087e-4eb1-bf79-5c8f5986bdac

📥 Commits

Reviewing files that changed from the base of the PR and between 1f9b44b and 09a819c.

📒 Files selected for processing (8)
  • gateway/gateway-runtime/policy-engine/internal/constants/constants.go
  • gateway/gateway-runtime/policy-engine/internal/executor/chain.go
  • gateway/gateway-runtime/policy-engine/internal/executor/chain_span_status_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc_span_status_test.go
  • gateway/gateway-runtime/policy-engine/internal/tracing/span_status.go
  • gateway/gateway-runtime/policy-engine/internal/tracing/span_status_test.go

DDH13 added 2 commits July 30, 2026 02:18
…ngine

- Introduced new terminal reasons for better error tracking: `TerminalReasonPolicyStatusOverride` and `TerminalReasonProcessingFailed`.
- Updated `PolicyExecutionContext` to track overridden response statuses.
- Modified `RecordHTTPOutcome` to ensure correct span status classification based on terminal reasons.
@Krishanx92
Krishanx92 merged commit 7753261 into wso2:main Jul 30, 2026
10 checks passed
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.

2 participants