Skip to content

feat(tracing): skip OTLP error recording for selected errors#1317

Open
sylr wants to merge 1 commit intomainfrom
feat/discard-errors-from-traces
Open

feat(tracing): skip OTLP error recording for selected errors#1317
sylr wants to merge 1 commit intomainfrom
feat/discard-errors-from-traces

Conversation

@sylr
Copy link
Copy Markdown
Contributor

@sylr sylr commented Apr 1, 2026

Add TraceOption with SkipErrorRecordingIf and WithSpanStartOptions so Trace and TraceWithMetric can omit otlp.RecordError for expected cases (e.g. "not found" on ReadLogWithIdempotencyKey).

Migrate ImportLog span options to WithSpanStartOptions. Inline the InsertTransaction metric finalizer into the traced callback because TraceWithMetric now takes trace options as its trailing variadic.

Made-with: Cursor

Add TraceOption with SkipErrorRecordingIf and WithSpanStartOptions so
Trace and TraceWithMetric can omit otlp.RecordError for expected cases
(e.g. not found on ReadLogWithIdempotencyKey).

Migrate ImportLog span options to WithSpanStartOptions. Inline the
InsertTransaction metric finalizer into the traced callback because
TraceWithMetric now takes trace options as its trailing variadic.

Made-with: Cursor
Signed-off-by: Sylvain Rabot <sylvain@formance.com>
@sylr sylr requested a review from a team as a code owner April 1, 2026 14:08
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 1, 2026

Walkthrough

This PR refactors the tracing infrastructure to use a configurable TraceOption pattern. The Trace and TraceWithMetric functions now accept option objects instead of raw span options and finalizer callbacks, replacing the finalizer pattern with conditional error recording based on predicates.

Changes

Cohort / File(s) Summary
Tracing Infrastructure
internal/tracing/tracing.go
Introduced TraceOption interface with WithSpanStartOptions() and SkipErrorRecordingIf() constructors. Refactored Trace and TraceWithMetric to accept ...TraceOption instead of raw options/finalizers. Implemented conditional error recording that skips RecordError when a registered predicate returns true.
Tracing Usage
internal/controller/ledger/controller_default.go, internal/storage/ledger/logs.go, internal/storage/ledger/transactions.go
Updated tracing calls to use new option pattern: wrapped span options with WithSpanStartOptions(), added SkipErrorRecordingIf() for not-found errors, and removed trailing callback pattern in transaction insertion.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 Options now bloom where callbacks once stood,
A cleaner pattern, misunderstood no more!
Skip errors wisely, let traces grow good,
From chaos of finalizers to order's core. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ 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%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: introducing a mechanism to skip OTLP error recording for selected errors via TraceOption.
Description check ✅ Passed The description is directly related to the changeset, explaining the TraceOption API introduction, migration of span options, and inlining of finalizers.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/discard-errors-from-traces

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.

Copy link
Copy Markdown
Contributor

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

🧹 Nitpick comments (1)
internal/tracing/tracing.go (1)

127-163: Well-designed TraceOption API with clean extensibility.

The functional options pattern is idiomatic Go, the OR semantics for multiple predicates is clearly documented, and the implementation correctly returns errors to callers unchanged while only affecting OTLP recording.

One optional defensive improvement: consider guarding against nil predicates to prevent a panic if a caller accidentally passes nil.

🛡️ Optional: guard against nil predicate
 func SkipErrorRecordingIf(predicate func(error) bool) TraceOption {
+	if predicate == nil {
+		return traceOptionFunc(func(*traceSettings) {}) // no-op
+	}
 	return traceOptionFunc(func(s *traceSettings) {
 		s.skipRecordErrorIfs = append(s.skipRecordErrorIfs, predicate)
 	})
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/tracing/tracing.go` around lines 127 - 163, Guard against nil
predicates to avoid panics: in SkipErrorRecordingIf, ignore any nil predicate
arguments instead of appending them to traceSettings.skipRecordErrorIfs;
additionally make shouldSkipErrorRecording resilient by checking for nil before
calling predicate (i.e., skip nil entries). Update references in
SkipErrorRecordingIf and traceSettings.shouldSkipErrorRecording to perform these
nil checks so a mistakenly passed nil predicate won’t cause a runtime panic.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@internal/tracing/tracing.go`:
- Around line 127-163: Guard against nil predicates to avoid panics: in
SkipErrorRecordingIf, ignore any nil predicate arguments instead of appending
them to traceSettings.skipRecordErrorIfs; additionally make
shouldSkipErrorRecording resilient by checking for nil before calling predicate
(i.e., skip nil entries). Update references in SkipErrorRecordingIf and
traceSettings.shouldSkipErrorRecording to perform these nil checks so a
mistakenly passed nil predicate won’t cause a runtime panic.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ed6a493d-1ea4-451e-90d6-2e2a61ba2042

📥 Commits

Reviewing files that changed from the base of the PR and between 62ad458 and 229bc5e.

📒 Files selected for processing (4)
  • internal/controller/ledger/controller_default.go
  • internal/storage/ledger/logs.go
  • internal/storage/ledger/transactions.go
  • internal/tracing/tracing.go

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 65.38462% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.21%. Comparing base (62ad458) to head (229bc5e).

Files with missing lines Patch % Lines
internal/tracing/tracing.go 70.83% 4 Missing and 3 partials ⚠️
internal/controller/ledger/controller_default.go 0.00% 1 Missing ⚠️
internal/storage/ledger/transactions.go 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1317      +/-   ##
==========================================
- Coverage   81.00%   80.21%   -0.80%     
==========================================
  Files         205      205              
  Lines       11074    11103      +29     
==========================================
- Hits         8971     8906      -65     
- Misses       1558     1577      +19     
- Partials      545      620      +75     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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