-
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: OpenTelemetry doesn't capture exceptions in the outermost applic…
…ation layer (#3689) * fix(opentelemetry): wrap all middleware under Otel middleware to ensure spans are created and exceptions are logged correctly under that span --------- Co-authored-by: bella <[email protected]>
- Loading branch information
1 parent
30e14a6
commit d1ef753
Showing
6 changed files
with
267 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
from .config import OpenTelemetryConfig | ||
from .middleware import OpenTelemetryInstrumentationMiddleware | ||
from .plugin import OpenTelemetryPlugin | ||
|
||
__all__ = ("OpenTelemetryConfig", "OpenTelemetryInstrumentationMiddleware") | ||
__all__ = ( | ||
"OpenTelemetryConfig", | ||
"OpenTelemetryInstrumentationMiddleware", | ||
"OpenTelemetryPlugin", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from litestar.contrib.opentelemetry.config import OpenTelemetryConfig | ||
from litestar.contrib.opentelemetry.middleware import OpenTelemetryInstrumentationMiddleware | ||
from litestar.middleware.base import DefineMiddleware | ||
from litestar.plugins import InitPluginProtocol | ||
|
||
if TYPE_CHECKING: | ||
from litestar.config.app import AppConfig | ||
from litestar.types.composite_types import Middleware | ||
|
||
|
||
class OpenTelemetryPlugin(InitPluginProtocol): | ||
"""OpenTelemetry Plugin.""" | ||
|
||
__slots__ = ("config", "_middleware") | ||
|
||
def __init__(self, config: OpenTelemetryConfig | None = None) -> None: | ||
self.config = config or OpenTelemetryConfig() | ||
self._middleware: DefineMiddleware | None = None | ||
super().__init__() | ||
|
||
@property | ||
def middleware(self) -> DefineMiddleware: | ||
if self._middleware: | ||
return self._middleware | ||
return DefineMiddleware(OpenTelemetryInstrumentationMiddleware, config=self.config) | ||
|
||
def on_app_init(self, app_config: AppConfig) -> AppConfig: | ||
app_config.middleware, _middleware = self._pop_otel_middleware(app_config.middleware) | ||
return app_config | ||
|
||
@staticmethod | ||
def _pop_otel_middleware(middlewares: list[Middleware]) -> tuple[list[Middleware], DefineMiddleware | None]: | ||
"""Get the OpenTelemetry middleware if it is enabled in the application. | ||
Remove the middleware from the list of middlewares if it is found. | ||
""" | ||
otel_middleware: DefineMiddleware | None = None | ||
other_middlewares = [] | ||
for middleware in middlewares: | ||
if ( | ||
isinstance(middleware, DefineMiddleware) | ||
and middleware.middleware is OpenTelemetryInstrumentationMiddleware | ||
): | ||
otel_middleware = middleware | ||
else: | ||
other_middlewares.append(middleware) | ||
return other_middlewares, otel_middleware |
Oops, something went wrong.