|
13 | 13 | from starlette.requests import Request |
14 | 14 | from starlette.types import ASGIApp, Message, Receive, Scope, Send |
15 | 15 |
|
| 16 | +from titiler.core import telemetry |
| 17 | + |
16 | 18 |
|
17 | 19 | @dataclass(frozen=True) |
18 | 20 | class CacheControlMiddleware: |
@@ -107,29 +109,31 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send): |
107 | 109 | if scope["type"] != "http": |
108 | 110 | return await self.app(scope, receive, send) |
109 | 111 |
|
110 | | - exception: Exception | None = None |
111 | | - try: |
112 | | - await self.app(scope, receive, send) |
113 | | - except Exception as e: |
114 | | - exception = e |
115 | | - |
116 | 112 | request = Request(scope, receive=receive) |
117 | 113 |
|
118 | 114 | data = { |
119 | | - "method": request.method, |
120 | | - "referer": next( |
| 115 | + "http.method": request.method, |
| 116 | + "http.referer": next( |
121 | 117 | (request.headers.get(attr) for attr in ["referer", "referrer"]), |
122 | 118 | None, |
123 | 119 | ), |
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), |
129 | 125 | } |
130 | 126 |
|
131 | 127 | 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 |
133 | 137 |
|
134 | 138 | self.logger.info( |
135 | 139 | f"Request received: {request.url.path} {request.method}", |
|
0 commit comments