Skip to content

Commit cb85b10

Browse files
committed
update LoggerMiddleware to work with telemetry
1 parent 30aae58 commit cb85b10

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/titiler/core/titiler/core/middleware.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from starlette.requests import Request
1414
from starlette.types import ASGIApp, Message, Receive, Scope, Send
1515

16+
from titiler.core import telemetry
17+
1618

1719
@dataclass(frozen=True)
1820
class CacheControlMiddleware:
@@ -107,29 +109,31 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
107109
if scope["type"] != "http":
108110
return await self.app(scope, receive, send)
109111

110-
exception: Exception | None = None
111-
try:
112-
await self.app(scope, receive, send)
113-
except Exception as e:
114-
exception = e
115-
116112
request = Request(scope, receive=receive)
117113

118114
data = {
119-
"method": request.method,
120-
"referer": next(
115+
"http.method": request.method,
116+
"http.referer": next(
121117
(request.headers.get(attr) for attr in ["referer", "referrer"]),
122118
None,
123119
),
124-
"origin": request.headers.get("origin"),
125-
"path": request.url.path,
126-
"path_params": request.path_params,
127-
"query_params": dict(request.query_params),
128-
"headers": dict(request.headers),
120+
"http.origin": request.headers.get("origin"),
121+
"http.path": request.url.path,
122+
"http.path_params": request.path_params,
123+
"http.query_params": dict(request.query_params),
124+
"http.headers": dict(request.headers),
129125
}
130126

131127
if route := scope.get("route"):
132-
data["route"] = route.path
128+
data["http.route"] = route.path
129+
130+
telemetry.add_span_attributes(telemetry.flatten_dict(data))
131+
132+
exception: Exception | None = None
133+
try:
134+
await self.app(scope, receive, send)
135+
except Exception as e:
136+
exception = e
133137

134138
self.logger.info(
135139
f"Request received: {request.url.path} {request.method}",

0 commit comments

Comments
 (0)