Skip to content

Enhancement: expose after exception hook for OTEL config #4594

@vikigenius

Description

@vikigenius

Summary

Allow setting after exception hooks for OTEL config. Would allow to change behavior when exception is raised for a span.

Basic Example

Currently I do something like this without which the exceptions in internal spans are simply swallowed

async def otel_after_exception(exc: Exception, _scope: Scope) -> None:
    span = get_current_span()
    span.record_exception(exc)
    span.set_status(Status(StatusCode.ERROR, str(exc)))


@dataclass
class OpenTelemetryConfig(BaseOpenTelemetryConfig):
    after_exception: list[Callable[[Exception, Scope], Awaitable[None]]] = field(
        default_factory=lambda: [otel_after_exception]
    )
class OpenTelemetryPlugin(BaseOpenTelemetryPlugin):
    """Opentelemetry Plugin."""

    @override
    def on_app_init(self, app_config: AppConfig) -> AppConfig:
        app_config = super().on_app_init(app_config)
        if isinstance(self.config, OpenTelemetryConfig):
            app_config.after_exception.extend(self.config.after_exception)
        return app_config

Drawbacks and Impact

Extra parameter, should not affect default behavior

Unresolved questions

Should we also include a default change in behavior as in my example?

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementThis is a new feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions