-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstarlette.py
291 lines (261 loc) · 12.2 KB
/
starlette.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from __future__ import annotations
import asyncio
import contextlib
import json
import time
from typing import Any, Callable, Dict, List, Optional, Union
from warnings import warn
from httpx import HTTPStatusError, Proxy
from starlette.datastructures import Headers
from starlette.requests import Request
from starlette.routing import BaseRoute, Match, Router
from starlette.schemas import EndpointInfo, SchemaGenerator
from starlette.testclient import TestClient
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from apitally.client.client_asyncio import ApitallyClient
from apitally.client.consumers import Consumer as ApitallyConsumer
from apitally.client.request_logging import (
BODY_TOO_LARGE,
MAX_BODY_SIZE,
RequestLoggingConfig,
)
from apitally.common import get_versions, parse_int
__all__ = ["ApitallyMiddleware", "ApitallyConsumer", "RequestLoggingConfig"]
class ApitallyMiddleware:
def __init__(
self,
app: ASGIApp,
client_id: str,
env: str = "dev",
request_logging_config: Optional[RequestLoggingConfig] = None,
app_version: Optional[str] = None,
openapi_url: Optional[str] = "/openapi.json",
identify_consumer_callback: Optional[Callable[[Request], Union[str, ApitallyConsumer, None]]] = None,
proxy: Optional[Union[str, Proxy]] = None,
) -> None:
self.app = app
self.identify_consumer_callback = identify_consumer_callback
self.client = ApitallyClient(
client_id=client_id,
env=env,
request_logging_config=request_logging_config,
proxy=proxy,
)
self.client.start_sync_loop()
self._delayed_set_startup_data_task: Optional[asyncio.Task] = None
self.delayed_set_startup_data(app_version, openapi_url)
_register_shutdown_handler(app, self.client.handle_shutdown)
self.capture_request_body = (
self.client.request_logger.config.enabled and self.client.request_logger.config.log_request_body
)
self.capture_response_body = (
self.client.request_logger.config.enabled and self.client.request_logger.config.log_response_body
)
def delayed_set_startup_data(self, app_version: Optional[str] = None, openapi_url: Optional[str] = None) -> None:
self._delayed_set_startup_data_task = asyncio.create_task(
self._delayed_set_startup_data(app_version, openapi_url)
)
async def _delayed_set_startup_data(
self, app_version: Optional[str] = None, openapi_url: Optional[str] = None
) -> None:
await asyncio.sleep(1.0) # Short delay to allow app routes to be registered first
data = _get_startup_data(self.app, app_version, openapi_url)
self.client.set_startup_data(data)
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] == "http" and scope["method"] != "OPTIONS":
timestamp = time.time()
request = Request(scope)
request_size = parse_int(request.headers.get("Content-Length"))
request_body = b""
request_body_too_large = request_size is not None and request_size > MAX_BODY_SIZE
response_status = 0
response_time: Optional[float] = None
response_headers = Headers()
response_body = b""
response_body_too_large = False
response_size: Optional[int] = None
response_chunked = False
exception: Optional[BaseException] = None
start_time = time.perf_counter()
async def receive_wrapper() -> Message:
nonlocal request_body, request_body_too_large
message = await receive()
if message["type"] == "http.request" and self.capture_request_body and not request_body_too_large:
request_body += message.get("body", b"")
if len(request_body) > MAX_BODY_SIZE:
request_body_too_large = True
request_body = b""
return message
async def send_wrapper(message: Message) -> None:
nonlocal \
response_time, \
response_status, \
response_headers, \
response_body, \
response_body_too_large, \
response_chunked, \
response_size
if message["type"] == "http.response.start":
response_time = time.perf_counter() - start_time
response_status = message["status"]
response_headers = Headers(scope=message)
response_chunked = (
response_headers.get("Transfer-Encoding") == "chunked"
or "Content-Length" not in response_headers
)
response_size = parse_int(response_headers.get("Content-Length")) if not response_chunked else 0
response_body_too_large = response_size is not None and response_size > MAX_BODY_SIZE
elif message["type"] == "http.response.body":
if response_chunked and response_size is not None:
response_size += len(message.get("body", b""))
if (self.capture_response_body or response_status == 422) and not response_body_too_large:
response_body += message.get("body", b"")
if len(response_body) > MAX_BODY_SIZE:
response_body_too_large = True
response_body = b""
await send(message)
try:
await self.app(scope, receive_wrapper, send_wrapper)
except BaseException as e:
exception = e
raise e from None
finally:
if response_time is None:
response_time = time.perf_counter() - start_time
self.add_request(
timestamp=timestamp,
request=request,
request_body=request_body if not request_body_too_large else BODY_TOO_LARGE,
request_size=request_size,
response_status=response_status,
response_time=response_time,
response_headers=response_headers,
response_body=response_body if not response_body_too_large else BODY_TOO_LARGE,
response_size=response_size,
exception=exception,
)
else:
await self.app(scope, receive, send) # pragma: no cover
def add_request(
self,
timestamp: float,
request: Request,
request_body: bytes,
request_size: Optional[int],
response_status: int,
response_time: float,
response_headers: Headers,
response_body: bytes,
response_size: Optional[int],
exception: Optional[BaseException] = None,
) -> None:
path = self.get_path(request)
consumer = self.get_consumer(request)
consumer_identifier = consumer.identifier if consumer else None
self.client.consumer_registry.add_or_update_consumer(consumer)
if path is not None:
if response_status == 0 and exception is not None:
response_status = 500
self.client.request_counter.add_request(
consumer=consumer_identifier,
method=request.method,
path=path,
status_code=response_status,
response_time=response_time,
request_size=request_size,
response_size=response_size,
)
if response_status == 422 and response_body and response_headers.get("Content-Type") == "application/json":
with contextlib.suppress(json.JSONDecodeError):
body = json.loads(response_body)
if isinstance(body, dict) and "detail" in body and isinstance(body["detail"], list):
# Log FastAPI / Pydantic validation errors
self.client.validation_error_counter.add_validation_errors(
consumer=consumer_identifier,
method=request.method,
path=path,
detail=body["detail"],
)
if response_status == 500 and exception is not None:
self.client.server_error_counter.add_server_error(
consumer=consumer_identifier,
method=request.method,
path=path,
exception=exception,
)
if self.client.request_logger.enabled:
self.client.request_logger.log_request(
request={
"timestamp": timestamp,
"method": request.method,
"path": path,
"url": str(request.url),
"headers": request.headers.items(),
"size": request_size,
"consumer": consumer_identifier,
"body": request_body,
},
response={
"status_code": response_status,
"response_time": response_time,
"headers": response_headers.items(),
"size": response_size,
"body": response_body,
},
)
@staticmethod
def get_path(request: Request) -> Optional[str]:
for route in request.app.routes:
match, _ = route.matches(request.scope)
if match == Match.FULL:
return request.scope.get("root_path", "") + route.path
return None
def get_consumer(self, request: Request) -> Optional[ApitallyConsumer]:
if hasattr(request.state, "apitally_consumer") and request.state.apitally_consumer:
return ApitallyConsumer.from_string_or_object(request.state.apitally_consumer)
if hasattr(request.state, "consumer_identifier") and request.state.consumer_identifier:
# Keeping this for legacy support
warn(
"Providing a consumer identifier via `request.state.consumer_identifier` is deprecated, "
"use `request.state.apitally_consumer` instead.",
DeprecationWarning,
)
return ApitallyConsumer.from_string_or_object(request.state.consumer_identifier)
if self.identify_consumer_callback is not None:
consumer = self.identify_consumer_callback(request)
return ApitallyConsumer.from_string_or_object(consumer)
return None
def _get_startup_data(
app: ASGIApp, app_version: Optional[str] = None, openapi_url: Optional[str] = None
) -> Dict[str, Any]:
data: Dict[str, Any] = {}
if openapi_url and (openapi := _get_openapi(app, openapi_url)):
data["openapi"] = openapi
if endpoints := _get_endpoint_info(app):
data["paths"] = [{"path": endpoint.path, "method": endpoint.http_method} for endpoint in endpoints]
data["versions"] = get_versions("fastapi", "starlette", app_version=app_version)
data["client"] = "python:starlette"
return data
def _get_openapi(app: ASGIApp, openapi_url: str) -> Optional[str]:
try:
client = TestClient(app, raise_server_exceptions=False)
response = client.get(openapi_url)
response.raise_for_status()
return response.text
except HTTPStatusError:
return None
def _get_endpoint_info(app: ASGIApp) -> List[EndpointInfo]:
routes = _get_routes(app)
schemas = SchemaGenerator({})
return schemas.get_endpoints(routes)
def _get_routes(app: Union[ASGIApp, Router]) -> List[BaseRoute]:
if isinstance(app, Router):
return app.routes
elif hasattr(app, "app"):
return _get_routes(app.app)
return [] # pragma: no cover
def _register_shutdown_handler(app: Union[ASGIApp, Router], shutdown_handler: Callable[[], Any]) -> None:
if isinstance(app, Router):
app.add_event_handler("shutdown", shutdown_handler)
elif hasattr(app, "app"):
_register_shutdown_handler(app.app, shutdown_handler)