Skip to content

Allow overriding or customizing span.RecordError() behavior globally #6754

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

Open
Bhogayata-Keval opened this issue May 7, 2025 · 3 comments
Labels
blocked:specification Waiting on clarification of the OpenTelemetry specification before progress can be made enhancement New feature or request

Comments

@Bhogayata-Keval
Copy link

Bhogayata-Keval commented May 7, 2025

Summary
I’d like to propose a mechanism to allow users to override or hook into span.RecordError() globally — for example, to enrich error-related span events with custom attributes consistently across all instrumented libraries.

Motivation
Currently, there is no clean way to globally customize the behavior of span.RecordError().

In my use case, I want to inject an additional attribute (e.g., custom.source = "internal") into every exception span event, regardless of which instrumentation library triggered it (Gin, Mux, Fiber, etc.).

While it’s technically possible to:

Manually call span.AddEvent("exception", ...) with custom attributes, or

Wrap span.RecordError() in user code,

…these solutions are not scalable across large codebases or libraries using auto-instrumentation.

What I tried
SpanProcessor: Can observe exception events in OnEnd(), but cannot mutate span data (it's read-only).

SpanExporter: Allows exporting enriched data, but this requires duplicating span data structures or intercepting and transforming OTLP payloads — which feels heavy-handed for a relatively simple customization.

Proposal
Introduce a hook or registry mechanism (e.g., otel.SetErrorRecorder(...)) where users can globally define how RecordError() enriches the span. This would preserve the default behavior while allowing teams to attach custom tags, redactions, or formatting logic.

@Bhogayata-Keval Bhogayata-Keval added the enhancement New feature or request label May 7, 2025
@dmathieu
Copy link
Member

This would need a specification change.

@dmathieu dmathieu added the blocked:specification Waiting on clarification of the OpenTelemetry specification before progress can be made label May 12, 2025
@dmathieu
Copy link
Member

While this SDK doesn't implement it yet, the (unstable) OnFinish hook for processors would be a good place to mutate spans if they include an error.
I think a custom processor using OnFinish would be better than a custom hook on RecordError (why not also on Start, AddAttributes, ...?)

@MrAlias
Copy link
Contributor

MrAlias commented May 20, 2025

I'm not in favor of global configuration to control this

+1 to custom processors or writing your own SDK to perform your desired behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked:specification Waiting on clarification of the OpenTelemetry specification before progress can be made enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants