apitally-serverless 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- apitally_serverless/__init__.py +0 -0
- apitally_serverless/common/__init__.py +0 -0
- apitally_serverless/common/config.py +30 -0
- apitally_serverless/common/consumers.py +24 -0
- apitally_serverless/common/headers.py +41 -0
- apitally_serverless/common/masking.py +144 -0
- apitally_serverless/common/output.py +86 -0
- apitally_serverless/fastapi.py +17 -0
- apitally_serverless/py.typed +0 -0
- apitally_serverless/starlette.py +255 -0
- apitally_serverless-0.1.0.dist-info/METADATA +129 -0
- apitally_serverless-0.1.0.dist-info/RECORD +14 -0
- apitally_serverless-0.1.0.dist-info/WHEEL +4 -0
- apitally_serverless-0.1.0.dist-info/licenses/LICENSE +21 -0
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from typing import Any, TypedDict
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ApitallyConfigKwargs(TypedDict, total=False):
|
|
6
|
+
enabled: bool
|
|
7
|
+
log_request_headers: bool
|
|
8
|
+
log_request_body: bool
|
|
9
|
+
log_response_headers: bool
|
|
10
|
+
log_response_body: bool
|
|
11
|
+
mask_headers: list[str]
|
|
12
|
+
mask_body_fields: list[str]
|
|
13
|
+
exclude_paths: list[str]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class ApitallyConfig:
|
|
18
|
+
enabled: bool = True
|
|
19
|
+
log_request_headers: bool = False
|
|
20
|
+
log_request_body: bool = False
|
|
21
|
+
log_response_headers: bool = True
|
|
22
|
+
log_response_body: bool = False
|
|
23
|
+
mask_headers: list[str] = field(default_factory=list)
|
|
24
|
+
mask_body_fields: list[str] = field(default_factory=list)
|
|
25
|
+
exclude_paths: list[str] = field(default_factory=list)
|
|
26
|
+
|
|
27
|
+
@classmethod
|
|
28
|
+
def from_kwargs(cls, kwargs: ApitallyConfigKwargs) -> "ApitallyConfig":
|
|
29
|
+
config_kwargs: dict[str, Any] = {k: v for k, v in kwargs.items() if k in cls.__dataclass_fields__}
|
|
30
|
+
return ApitallyConfig(**config_kwargs)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
_seen_consumer_hashes: set[int] = set()
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass
|
|
8
|
+
class ApitallyConsumer:
|
|
9
|
+
identifier: str
|
|
10
|
+
name: str | None = None
|
|
11
|
+
group: str | None = None
|
|
12
|
+
|
|
13
|
+
def __post_init__(self) -> None:
|
|
14
|
+
self.identifier = str(self.identifier).strip()[:128]
|
|
15
|
+
self.name = str(self.name).strip()[:64] if self.name else None
|
|
16
|
+
self.group = str(self.group).strip()[:64] if self.group else None
|
|
17
|
+
|
|
18
|
+
if self.name or self.group:
|
|
19
|
+
h = hash((self.identifier, self.name, self.group))
|
|
20
|
+
if h in _seen_consumer_hashes:
|
|
21
|
+
self.name = None
|
|
22
|
+
self.group = None
|
|
23
|
+
else:
|
|
24
|
+
_seen_consumer_hashes.add(h)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from typing import Iterable
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
SUPPORTED_CONTENT_TYPES = [
|
|
5
|
+
"application/json",
|
|
6
|
+
"application/ld+json",
|
|
7
|
+
"application/problem+json",
|
|
8
|
+
"application/vnd.api+json",
|
|
9
|
+
"application/x-ndjson",
|
|
10
|
+
"text/plain",
|
|
11
|
+
"text/html",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def convert_headers(
|
|
16
|
+
headers: Iterable[tuple[str, str]] | None,
|
|
17
|
+
) -> list[tuple[str, str]]:
|
|
18
|
+
if headers is None:
|
|
19
|
+
return []
|
|
20
|
+
return [(k.lower(), v) for k, v in headers]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def parse_content_length(
|
|
24
|
+
content_length: str | bytes | int | None,
|
|
25
|
+
) -> int | None:
|
|
26
|
+
if content_length is None:
|
|
27
|
+
return None
|
|
28
|
+
if isinstance(content_length, int):
|
|
29
|
+
return content_length
|
|
30
|
+
if isinstance(content_length, bytes):
|
|
31
|
+
content_length = content_length.decode()
|
|
32
|
+
try:
|
|
33
|
+
return int(content_length)
|
|
34
|
+
except ValueError:
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def is_supported_content_type(content_type: str | None) -> bool:
|
|
39
|
+
if not content_type:
|
|
40
|
+
return False
|
|
41
|
+
return any(content_type.startswith(t) for t in SUPPORTED_CONTENT_TYPES)
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import re
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from apitally_serverless.common.config import ApitallyConfig
|
|
6
|
+
from apitally_serverless.common.output import OutputDataDict
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
MASKED = "******"
|
|
10
|
+
|
|
11
|
+
EXCLUDE_PATH_PATTERNS = [
|
|
12
|
+
r"/_?healthz?$",
|
|
13
|
+
r"/_?health[_-]?checks?$",
|
|
14
|
+
r"/_?heart[_-]?beats?$",
|
|
15
|
+
r"/ping$",
|
|
16
|
+
r"/ready$",
|
|
17
|
+
r"/live$",
|
|
18
|
+
]
|
|
19
|
+
MASK_HEADER_PATTERNS = [
|
|
20
|
+
r"auth",
|
|
21
|
+
r"api-?key",
|
|
22
|
+
r"secret",
|
|
23
|
+
r"token",
|
|
24
|
+
r"cookie",
|
|
25
|
+
]
|
|
26
|
+
MASK_BODY_FIELD_PATTERNS = [
|
|
27
|
+
r"password",
|
|
28
|
+
r"pwd",
|
|
29
|
+
r"token",
|
|
30
|
+
r"secret",
|
|
31
|
+
r"auth",
|
|
32
|
+
r"card[-_ ]?number",
|
|
33
|
+
r"ccv",
|
|
34
|
+
r"ssn",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class DataMasker:
|
|
39
|
+
def __init__(self, config: ApitallyConfig) -> None:
|
|
40
|
+
self.config = config
|
|
41
|
+
self.exclude_path_patterns = [
|
|
42
|
+
re.compile(p, re.I) for p in dict.fromkeys(config.exclude_paths + EXCLUDE_PATH_PATTERNS)
|
|
43
|
+
]
|
|
44
|
+
self.mask_header_patterns = [
|
|
45
|
+
re.compile(p, re.I) for p in dict.fromkeys(config.mask_headers + MASK_HEADER_PATTERNS)
|
|
46
|
+
]
|
|
47
|
+
self.mask_body_field_patterns = [
|
|
48
|
+
re.compile(p, re.I) for p in dict.fromkeys(config.mask_body_fields + MASK_BODY_FIELD_PATTERNS)
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
def apply_masking(self, data: OutputDataDict) -> None:
|
|
52
|
+
request = data["request"]
|
|
53
|
+
response = data["response"]
|
|
54
|
+
|
|
55
|
+
# Check if path is excluded
|
|
56
|
+
if self._should_exclude_path(request["path"]):
|
|
57
|
+
request["headers"] = None
|
|
58
|
+
request["body"] = None
|
|
59
|
+
response["headers"] = None
|
|
60
|
+
response["body"] = None
|
|
61
|
+
data["exclude"] = True
|
|
62
|
+
return
|
|
63
|
+
|
|
64
|
+
# Drop request and response bodies if logging is disabled
|
|
65
|
+
if not self.config.log_request_body and request["body"] is not None:
|
|
66
|
+
request["body"] = None
|
|
67
|
+
if not self.config.log_response_body and response["body"] is not None:
|
|
68
|
+
response["body"] = None
|
|
69
|
+
|
|
70
|
+
# Mask request and response body fields
|
|
71
|
+
if request["body"] is not None:
|
|
72
|
+
request["body"] = self._mask_body_bytes(request["body"], request["headers"])
|
|
73
|
+
if response["body"] is not None:
|
|
74
|
+
response["body"] = self._mask_body_bytes(response["body"], response["headers"])
|
|
75
|
+
|
|
76
|
+
# Mask request and response headers
|
|
77
|
+
if self.config.log_request_headers and request["headers"] is not None:
|
|
78
|
+
request["headers"] = self._mask_headers(request["headers"])
|
|
79
|
+
else:
|
|
80
|
+
request["headers"] = None
|
|
81
|
+
|
|
82
|
+
if self.config.log_response_headers and response["headers"] is not None:
|
|
83
|
+
response["headers"] = self._mask_headers(response["headers"])
|
|
84
|
+
else:
|
|
85
|
+
response["headers"] = None
|
|
86
|
+
|
|
87
|
+
def _should_exclude_path(self, path: str | None) -> bool:
|
|
88
|
+
return path is not None and any(p.search(path) for p in self.exclude_path_patterns)
|
|
89
|
+
|
|
90
|
+
def _should_mask_header(self, name: str) -> bool:
|
|
91
|
+
return any(p.search(name) for p in self.mask_header_patterns)
|
|
92
|
+
|
|
93
|
+
def _should_mask_body_field(self, name: str) -> bool:
|
|
94
|
+
return any(p.search(name) for p in self.mask_body_field_patterns)
|
|
95
|
+
|
|
96
|
+
def _mask_headers(self, headers: list[tuple[str, str]]) -> list[tuple[str, str]]:
|
|
97
|
+
return [(k, MASKED if self._should_mask_header(k) else v) for k, v in headers]
|
|
98
|
+
|
|
99
|
+
def _mask_body_bytes(self, body: bytes, headers: list[tuple[str, str]] | None) -> bytes:
|
|
100
|
+
content_type = self._get_content_type(headers)
|
|
101
|
+
|
|
102
|
+
try:
|
|
103
|
+
if content_type is not None and "ndjson" in content_type.lower():
|
|
104
|
+
lines = body.decode("utf-8").split("\n")
|
|
105
|
+
masked_lines = []
|
|
106
|
+
for line in lines:
|
|
107
|
+
line = line.strip()
|
|
108
|
+
if line:
|
|
109
|
+
try:
|
|
110
|
+
parsed = json.loads(line)
|
|
111
|
+
masked = self._mask_body(parsed)
|
|
112
|
+
masked_lines.append(json.dumps(masked, separators=(",", ":")))
|
|
113
|
+
except (json.JSONDecodeError, UnicodeDecodeError):
|
|
114
|
+
masked_lines.append(line)
|
|
115
|
+
return "\n".join(masked_lines).encode("utf-8")
|
|
116
|
+
elif content_type is None or "json" in content_type.lower():
|
|
117
|
+
parsed = json.loads(body.decode("utf-8"))
|
|
118
|
+
masked = self._mask_body(parsed)
|
|
119
|
+
return json.dumps(masked, separators=(",", ":")).encode("utf-8")
|
|
120
|
+
except (json.JSONDecodeError, UnicodeDecodeError):
|
|
121
|
+
pass
|
|
122
|
+
|
|
123
|
+
return body
|
|
124
|
+
|
|
125
|
+
def _mask_body(self, data: Any) -> Any:
|
|
126
|
+
if isinstance(data, dict):
|
|
127
|
+
result = {}
|
|
128
|
+
for key, value in data.items():
|
|
129
|
+
if isinstance(value, str) and self._should_mask_body_field(key):
|
|
130
|
+
result[key] = MASKED
|
|
131
|
+
else:
|
|
132
|
+
result[key] = self._mask_body(value)
|
|
133
|
+
return result
|
|
134
|
+
if isinstance(data, list):
|
|
135
|
+
return [self._mask_body(item) for item in data]
|
|
136
|
+
return data
|
|
137
|
+
|
|
138
|
+
def _get_content_type(self, headers: list[tuple[str, str]] | None) -> str | None:
|
|
139
|
+
if not headers:
|
|
140
|
+
return None
|
|
141
|
+
for k, v in headers:
|
|
142
|
+
if k.lower() == "content-type":
|
|
143
|
+
return v
|
|
144
|
+
return None
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import gzip
|
|
3
|
+
import json
|
|
4
|
+
from typing import Any, TypedDict
|
|
5
|
+
|
|
6
|
+
from typing_extensions import NotRequired
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ConsumerDict(TypedDict):
|
|
10
|
+
identifier: str
|
|
11
|
+
name: str | None
|
|
12
|
+
group: str | None
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class StartupDataDict(TypedDict):
|
|
16
|
+
paths: list[dict[str, str]]
|
|
17
|
+
versions: dict[str, str]
|
|
18
|
+
client: str
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class RequestDataDict(TypedDict):
|
|
22
|
+
path: str | None
|
|
23
|
+
headers: list[tuple[str, str]] | None
|
|
24
|
+
size: int | None
|
|
25
|
+
consumer: str | None
|
|
26
|
+
body: bytes | None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ResponseDataDict(TypedDict):
|
|
30
|
+
response_time: float
|
|
31
|
+
status_code: int
|
|
32
|
+
headers: list[tuple[str, str]] | None
|
|
33
|
+
size: int | None
|
|
34
|
+
body: bytes | None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ValidationErrorDict(TypedDict):
|
|
38
|
+
loc: list[str]
|
|
39
|
+
msg: str
|
|
40
|
+
type: str
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class OutputDataDict(TypedDict):
|
|
44
|
+
instance_uuid: str
|
|
45
|
+
request_uuid: str
|
|
46
|
+
startup: StartupDataDict | None
|
|
47
|
+
consumer: ConsumerDict | None
|
|
48
|
+
request: RequestDataDict
|
|
49
|
+
response: ResponseDataDict
|
|
50
|
+
validation_errors: list[ValidationErrorDict] | None
|
|
51
|
+
exclude: NotRequired[bool]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _json_default(obj: Any) -> Any:
|
|
55
|
+
if isinstance(obj, bytes):
|
|
56
|
+
return base64.b64encode(obj).decode("ascii")
|
|
57
|
+
raise TypeError(f"Object of type {type(obj).__name__} is not JSON serializable")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _skip_empty_values(data: dict[str, Any]) -> dict[str, Any]:
|
|
61
|
+
return {
|
|
62
|
+
k: _skip_empty_values(v) if isinstance(v, dict) else v
|
|
63
|
+
for k, v in data.items()
|
|
64
|
+
if v is not None and not (isinstance(v, (list, dict, bytes, str)) and len(v) == 0)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _create_log_message(data: OutputDataDict) -> str:
|
|
69
|
+
cleaned = _skip_empty_values(dict(data))
|
|
70
|
+
serialized = json.dumps(cleaned, separators=(",", ":"), default=_json_default)
|
|
71
|
+
compressed = gzip.compress(serialized.encode("utf-8"))
|
|
72
|
+
encoded = base64.b64encode(compressed).decode("ascii")
|
|
73
|
+
return f"apitally:{encoded}"
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def log_data(data: OutputDataDict) -> None:
|
|
77
|
+
msg = _create_log_message(data)
|
|
78
|
+
|
|
79
|
+
if len(msg) > 15_000:
|
|
80
|
+
# Cloudflare Workers Logpush limits the total length of all exception and log messages to 16,384 characters,
|
|
81
|
+
# so we need to keep the logged message well below that limit.
|
|
82
|
+
data["request"]["body"] = None
|
|
83
|
+
data["response"]["body"] = None
|
|
84
|
+
msg = _create_log_message(data)
|
|
85
|
+
|
|
86
|
+
print(msg)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from apitally_serverless.starlette import ApitallyMiddleware as _ApitallyMiddlewareForStarlette
|
|
2
|
+
from apitally_serverless.starlette import set_consumer
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
__all__ = ["ApitallyMiddleware", "set_consumer"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ApitallyMiddleware(_ApitallyMiddlewareForStarlette):
|
|
9
|
+
"""
|
|
10
|
+
Apitally middleware for FastAPI applications in serverless environments.
|
|
11
|
+
|
|
12
|
+
For more information, see:
|
|
13
|
+
- Setup guide: https://docs.apitally.io/frameworks/fastapi-serverless
|
|
14
|
+
- Reference: https://docs.apitally.io/reference/python-serverless
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
import time
|
|
4
|
+
from contextlib import suppress
|
|
5
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
6
|
+
from uuid import uuid4
|
|
7
|
+
|
|
8
|
+
from starlette.datastructures import Headers
|
|
9
|
+
from starlette.requests import Request
|
|
10
|
+
from starlette.routing import BaseRoute, Match, Router
|
|
11
|
+
from starlette.schemas import SchemaGenerator
|
|
12
|
+
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
|
13
|
+
from typing_extensions import Unpack
|
|
14
|
+
|
|
15
|
+
from apitally_serverless.common.config import ApitallyConfig, ApitallyConfigKwargs
|
|
16
|
+
from apitally_serverless.common.consumers import ApitallyConsumer
|
|
17
|
+
from apitally_serverless.common.headers import convert_headers, is_supported_content_type, parse_content_length
|
|
18
|
+
from apitally_serverless.common.masking import DataMasker
|
|
19
|
+
from apitally_serverless.common.output import (
|
|
20
|
+
OutputDataDict,
|
|
21
|
+
StartupDataDict,
|
|
22
|
+
ValidationErrorDict,
|
|
23
|
+
log_data,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
__all__ = ["ApitallyMiddleware", "set_consumer"]
|
|
28
|
+
|
|
29
|
+
MAX_BODY_SIZE = 10_000
|
|
30
|
+
BODY_TOO_LARGE = b"<body too large>"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ApitallyMiddleware:
|
|
34
|
+
"""
|
|
35
|
+
Apitally middleware for Starlette applications in serverless environments.
|
|
36
|
+
|
|
37
|
+
For more information, see:
|
|
38
|
+
- Setup guide: https://docs.apitally.io/frameworks/starlette-serverless
|
|
39
|
+
- Reference: https://docs.apitally.io/reference/python-serverless
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __init__(
|
|
43
|
+
self,
|
|
44
|
+
app: ASGIApp,
|
|
45
|
+
**kwargs: Unpack[ApitallyConfigKwargs],
|
|
46
|
+
) -> None:
|
|
47
|
+
self.app = app
|
|
48
|
+
self.config = ApitallyConfig.from_kwargs(kwargs)
|
|
49
|
+
self.masker = DataMasker(self.config)
|
|
50
|
+
self.instance_uuid = str(uuid4())
|
|
51
|
+
self.is_first_request = True
|
|
52
|
+
|
|
53
|
+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
54
|
+
if not self.config.enabled or scope["type"] != "http" or scope["method"] == "OPTIONS": # pragma: no cover
|
|
55
|
+
await self.app(scope, receive, send)
|
|
56
|
+
return
|
|
57
|
+
|
|
58
|
+
start_time = time.perf_counter()
|
|
59
|
+
request = Request(scope, receive, send)
|
|
60
|
+
request_size = parse_content_length(request.headers.get("Content-Length"))
|
|
61
|
+
request_content_type = request.headers.get("Content-Type")
|
|
62
|
+
request_body = b""
|
|
63
|
+
request_body_too_large = request_size is not None and request_size > MAX_BODY_SIZE
|
|
64
|
+
|
|
65
|
+
response_status = 0
|
|
66
|
+
response_time: float | None = None
|
|
67
|
+
response_headers = Headers()
|
|
68
|
+
response_body = b""
|
|
69
|
+
response_body_too_large = False
|
|
70
|
+
response_size: int | None = None
|
|
71
|
+
response_chunked = False
|
|
72
|
+
response_content_type: str | None = None
|
|
73
|
+
|
|
74
|
+
async def receive_wrapper() -> Message:
|
|
75
|
+
nonlocal request_body, request_body_too_large
|
|
76
|
+
|
|
77
|
+
message = await receive()
|
|
78
|
+
if message["type"] == "http.request":
|
|
79
|
+
if (
|
|
80
|
+
self.config.log_request_body
|
|
81
|
+
and not request_body_too_large
|
|
82
|
+
and is_supported_content_type(request_content_type)
|
|
83
|
+
):
|
|
84
|
+
request_body += message.get("body", b"")
|
|
85
|
+
if len(request_body) > MAX_BODY_SIZE:
|
|
86
|
+
request_body_too_large = True
|
|
87
|
+
request_body = b""
|
|
88
|
+
return message
|
|
89
|
+
|
|
90
|
+
async def send_wrapper(message: Message) -> None:
|
|
91
|
+
nonlocal response_time, response_status, response_headers, response_body
|
|
92
|
+
nonlocal response_body_too_large, response_chunked, response_content_type, response_size
|
|
93
|
+
|
|
94
|
+
if message["type"] == "http.response.start":
|
|
95
|
+
response_time = time.perf_counter() - start_time
|
|
96
|
+
response_status = message["status"]
|
|
97
|
+
response_headers = Headers(scope=message)
|
|
98
|
+
response_chunked = (
|
|
99
|
+
response_headers.get("Transfer-Encoding") == "chunked" or "Content-Length" not in response_headers
|
|
100
|
+
)
|
|
101
|
+
response_content_type = response_headers.get("Content-Type")
|
|
102
|
+
response_size = (
|
|
103
|
+
parse_content_length(response_headers.get("Content-Length")) if not response_chunked else 0
|
|
104
|
+
)
|
|
105
|
+
response_body_too_large = response_size is not None and response_size > MAX_BODY_SIZE
|
|
106
|
+
|
|
107
|
+
elif message["type"] == "http.response.body":
|
|
108
|
+
if response_chunked and response_size is not None:
|
|
109
|
+
response_size += len(message.get("body", b""))
|
|
110
|
+
|
|
111
|
+
should_capture = (
|
|
112
|
+
(self.config.log_response_body or response_status == 422)
|
|
113
|
+
and is_supported_content_type(response_content_type)
|
|
114
|
+
and not response_body_too_large
|
|
115
|
+
)
|
|
116
|
+
if should_capture:
|
|
117
|
+
response_body += message.get("body", b"")
|
|
118
|
+
if len(response_body) > MAX_BODY_SIZE:
|
|
119
|
+
response_body_too_large = True
|
|
120
|
+
response_body = b""
|
|
121
|
+
|
|
122
|
+
await send(message)
|
|
123
|
+
|
|
124
|
+
try:
|
|
125
|
+
await self.app(scope, receive_wrapper, send_wrapper)
|
|
126
|
+
finally:
|
|
127
|
+
if response_time is None:
|
|
128
|
+
response_time = time.perf_counter() - start_time
|
|
129
|
+
|
|
130
|
+
if request_body_too_large:
|
|
131
|
+
request_body = BODY_TOO_LARGE
|
|
132
|
+
if response_body_too_large:
|
|
133
|
+
response_body = BODY_TOO_LARGE
|
|
134
|
+
|
|
135
|
+
# Build startup data on first request
|
|
136
|
+
startup_data: StartupDataDict | None = None
|
|
137
|
+
if self.is_first_request:
|
|
138
|
+
self.is_first_request = False
|
|
139
|
+
startup_data = {
|
|
140
|
+
"paths": _get_endpoints(self.app),
|
|
141
|
+
"versions": _get_versions(),
|
|
142
|
+
"client": "python-serverless:starlette",
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
consumer = _get_consumer(request)
|
|
146
|
+
validation_errors = (
|
|
147
|
+
_extract_validation_errors(response_body) if response_status == 422 and response_body else None
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
data: OutputDataDict = {
|
|
151
|
+
"instance_uuid": self.instance_uuid,
|
|
152
|
+
"request_uuid": str(uuid4()),
|
|
153
|
+
"startup": startup_data,
|
|
154
|
+
"consumer": {
|
|
155
|
+
"identifier": consumer.identifier,
|
|
156
|
+
"name": consumer.name,
|
|
157
|
+
"group": consumer.group,
|
|
158
|
+
}
|
|
159
|
+
if consumer and (consumer.name or consumer.group)
|
|
160
|
+
else None,
|
|
161
|
+
"request": {
|
|
162
|
+
"path": _get_path(request),
|
|
163
|
+
"headers": convert_headers(request.headers.items()),
|
|
164
|
+
"size": request_size,
|
|
165
|
+
"consumer": consumer.identifier if consumer else None,
|
|
166
|
+
"body": request_body or None,
|
|
167
|
+
},
|
|
168
|
+
"response": {
|
|
169
|
+
"response_time": response_time,
|
|
170
|
+
"status_code": response_status,
|
|
171
|
+
"headers": convert_headers(response_headers.items()),
|
|
172
|
+
"size": response_size,
|
|
173
|
+
"body": response_body or None,
|
|
174
|
+
},
|
|
175
|
+
"validation_errors": validation_errors,
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
self.masker.apply_masking(data)
|
|
179
|
+
log_data(data)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def set_consumer(request: Request, identifier: str, name: str | None = None, group: str | None = None) -> None:
|
|
183
|
+
"""Set the consumer for the current request."""
|
|
184
|
+
request.state.apitally_consumer = ApitallyConsumer(identifier, name=name, group=group)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _get_consumer(request: Request) -> ApitallyConsumer | None:
|
|
188
|
+
if hasattr(request.state, "apitally_consumer") and isinstance(request.state.apitally_consumer, ApitallyConsumer):
|
|
189
|
+
return request.state.apitally_consumer
|
|
190
|
+
return None
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _get_path(request: Request, routes: list[BaseRoute] | None = None) -> str | None:
|
|
194
|
+
if routes is None:
|
|
195
|
+
routes = request.app.routes
|
|
196
|
+
for route in routes:
|
|
197
|
+
if hasattr(route, "routes"):
|
|
198
|
+
path = _get_path(request, routes=route.routes)
|
|
199
|
+
if path is not None:
|
|
200
|
+
return path
|
|
201
|
+
elif hasattr(route, "path"):
|
|
202
|
+
match, _ = route.matches(request.scope)
|
|
203
|
+
if match == Match.FULL:
|
|
204
|
+
return request.scope.get("root_path", "") + route.path
|
|
205
|
+
return None
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _get_endpoints(app: ASGIApp) -> list[dict[str, str]]:
|
|
209
|
+
routes = _get_routes(app)
|
|
210
|
+
schemas = SchemaGenerator({})
|
|
211
|
+
endpoints = schemas.get_endpoints(routes)
|
|
212
|
+
return [{"method": e.http_method, "path": e.path} for e in endpoints]
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _get_routes(app: ASGIApp | Router) -> list[BaseRoute]:
|
|
216
|
+
if isinstance(app, Router):
|
|
217
|
+
return app.routes
|
|
218
|
+
elif hasattr(app, "app"):
|
|
219
|
+
return _get_routes(app.app)
|
|
220
|
+
return [] # pragma: no cover
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _get_versions() -> dict[str, str]:
|
|
224
|
+
versions = {
|
|
225
|
+
"python": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
|
|
226
|
+
}
|
|
227
|
+
for package in ["apitally-serverless", "fastapi", "starlette"]:
|
|
228
|
+
with suppress(PackageNotFoundError):
|
|
229
|
+
versions[package] = version(package)
|
|
230
|
+
return versions
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _extract_validation_errors(response_body: bytes) -> list[ValidationErrorDict] | None:
|
|
234
|
+
"""Extract Pydantic validation errors from a 422 response body."""
|
|
235
|
+
try:
|
|
236
|
+
body = json.loads(response_body.decode("utf-8"))
|
|
237
|
+
if isinstance(body, dict) and "detail" in body and isinstance(body["detail"], list):
|
|
238
|
+
errors: list[ValidationErrorDict] = []
|
|
239
|
+
for detail in body["detail"]:
|
|
240
|
+
if isinstance(detail, dict):
|
|
241
|
+
loc = detail.get("loc", [])
|
|
242
|
+
msg = detail.get("msg", "")
|
|
243
|
+
error_type = detail.get("type", "")
|
|
244
|
+
errors.append(
|
|
245
|
+
{
|
|
246
|
+
"loc": [str(item) for item in loc],
|
|
247
|
+
"msg": str(msg),
|
|
248
|
+
"type": str(error_type),
|
|
249
|
+
}
|
|
250
|
+
)
|
|
251
|
+
return errors
|
|
252
|
+
except (json.JSONDecodeError, UnicodeDecodeError): # pragma: no cover
|
|
253
|
+
pass
|
|
254
|
+
|
|
255
|
+
return None
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: apitally-serverless
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Simple API monitoring & analytics for REST APIs running on serverless platforms (e.g. Cloudflare Workers).
|
|
5
|
+
Project-URL: Homepage, https://apitally.io
|
|
6
|
+
Project-URL: Documentation, https://docs.apitally.io
|
|
7
|
+
Project-URL: Repository, https://github.com/apitally/apitally-py-serverless
|
|
8
|
+
Author-email: Apitally <hello@apitally.io>
|
|
9
|
+
License: MIT License
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Environment :: Web Environment
|
|
13
|
+
Classifier: Framework :: FastAPI
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Information Technology
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Programming Language :: Python
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Topic :: Internet
|
|
26
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
27
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware
|
|
28
|
+
Classifier: Topic :: Software Development
|
|
29
|
+
Classifier: Topic :: System :: Monitoring
|
|
30
|
+
Classifier: Typing :: Typed
|
|
31
|
+
Requires-Python: <4.0,>=3.10
|
|
32
|
+
Requires-Dist: typing-extensions>=4.0.0; python_version < '3.11'
|
|
33
|
+
Provides-Extra: fastapi
|
|
34
|
+
Requires-Dist: fastapi>=0.116.1; extra == 'fastapi'
|
|
35
|
+
Requires-Dist: starlette<1.0.0,>=0.47.2; extra == 'fastapi'
|
|
36
|
+
Provides-Extra: starlette
|
|
37
|
+
Requires-Dist: starlette<1.0.0,>=0.47.2; extra == 'starlette'
|
|
38
|
+
Description-Content-Type: text/markdown
|
|
39
|
+
|
|
40
|
+
<p align="center">
|
|
41
|
+
<a href="https://apitally.io" target="_blank">
|
|
42
|
+
<picture>
|
|
43
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://assets.apitally.io/logos/logo-horizontal-new-dark.png">
|
|
44
|
+
<source media="(prefers-color-scheme: light)" srcset="https://assets.apitally.io/logos/logo-horizontal-new-light.png">
|
|
45
|
+
<img alt="Apitally logo" src="https://assets.apitally.io/logos/logo-horizontal-new-light.png" width="220">
|
|
46
|
+
</picture>
|
|
47
|
+
</a>
|
|
48
|
+
</p>
|
|
49
|
+
<p align="center"><b>API monitoring & analytics made simple</b></p>
|
|
50
|
+
<p align="center" style="color: #ccc;">Real-time metrics, request logs, and alerts for your APIs — with just a few lines of code.</p>
|
|
51
|
+
<br>
|
|
52
|
+
<img alt="Apitally screenshots" src="https://assets.apitally.io/screenshots/overview.png">
|
|
53
|
+
<br>
|
|
54
|
+
|
|
55
|
+
# Apitally SDK for serverless Python runtimes
|
|
56
|
+
|
|
57
|
+
[](https://github.com/apitally/apitally-py-serverless/actions)
|
|
58
|
+
[](https://codecov.io/gh/apitally/apitally-py-serverless)
|
|
59
|
+
[](https://pypi.org/project/apitally-serverless/)
|
|
60
|
+
|
|
61
|
+
This SDK for Apitally currently supports the following web frameworks:
|
|
62
|
+
|
|
63
|
+
- [FastAPI](https://docs.apitally.io/setup-guides/fastapi-serverless)
|
|
64
|
+
|
|
65
|
+
The following serverless platforms are supported:
|
|
66
|
+
|
|
67
|
+
- [Cloudflare Workers](https://docs.apitally.io/setup-guides/cloudflare-workers) (via Logpush)
|
|
68
|
+
|
|
69
|
+
Learn more about Apitally on our 🌎 [website](https://apitally.io) or check out
|
|
70
|
+
the 📚 [documentation](https://docs.apitally.io).
|
|
71
|
+
|
|
72
|
+
## Key features
|
|
73
|
+
|
|
74
|
+
### API analytics
|
|
75
|
+
|
|
76
|
+
Track traffic, error and performance metrics for your API, each endpoint and individual API consumers, allowing you to make informed, data-driven engineering and product decisions.
|
|
77
|
+
|
|
78
|
+
### Error tracking
|
|
79
|
+
|
|
80
|
+
Understand which validation rules in your endpoints cause client errors. Capture error details and stack traces for 500 error responses, and have them linked to Sentry issues automatically.
|
|
81
|
+
|
|
82
|
+
### Request logging
|
|
83
|
+
|
|
84
|
+
Drill down from insights to individual requests or use powerful filtering to understand how consumers have interacted with your API. Configure exactly what is included in the logs to meet your requirements.
|
|
85
|
+
|
|
86
|
+
### API monitoring & alerting
|
|
87
|
+
|
|
88
|
+
Get notified immediately if something isn't right using custom alerts, synthetic uptime checks and heartbeat monitoring. Notifications can be delivered via email, Slack or Microsoft Teams.
|
|
89
|
+
|
|
90
|
+
## Installation
|
|
91
|
+
|
|
92
|
+
Use `uv` to add this SDK to your project's dependencies:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
uv add apitally-serverless
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Usage
|
|
99
|
+
|
|
100
|
+
Our comprehensive [setup guides](https://docs.apitally.io/setup-guides) include
|
|
101
|
+
all the details you need to get started.
|
|
102
|
+
|
|
103
|
+
### FastAPI
|
|
104
|
+
|
|
105
|
+
This is an example of how to add the Apitally middleware to a FastAPI
|
|
106
|
+
application running on a supported serverless platform. For further instructions, see our
|
|
107
|
+
[setup guide for FastAPI](https://docs.apitally.io/frameworks/fastapi-serverless).
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from fastapi import FastAPI
|
|
111
|
+
from apitally_serverless.fastapi import ApitallyMiddleware
|
|
112
|
+
|
|
113
|
+
app = FastAPI()
|
|
114
|
+
app.add_middleware(
|
|
115
|
+
ApitallyMiddleware,
|
|
116
|
+
log_request_headers=True,
|
|
117
|
+
log_request_body=True,
|
|
118
|
+
log_response_body=True,
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Getting help
|
|
123
|
+
|
|
124
|
+
If you need help please [create a new discussion](https://github.com/orgs/apitally/discussions/categories/q-a) on GitHub
|
|
125
|
+
or [join our Slack workspace](https://join.slack.com/t/apitally-community/shared_invite/zt-2b3xxqhdu-9RMq2HyZbR79wtzNLoGHrg).
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
This library is licensed under the terms of the MIT license.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
apitally_serverless/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
apitally_serverless/fastapi.py,sha256=5CZ0KvpWSBTvbxOBtDrIYxAqlyzJ8YX_I37Rge3cn58,542
|
|
3
|
+
apitally_serverless/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
apitally_serverless/starlette.py,sha256=XFa6jfkqOd62fDKUS-Y9dD9bbe8aib5mmueWxeqGgyE,10047
|
|
5
|
+
apitally_serverless/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
apitally_serverless/common/config.py,sha256=jUIjwKg-vv6SGxQd1yh-pyjnY3U3qUi5yK5ypt0z9Ic,986
|
|
7
|
+
apitally_serverless/common/consumers.py,sha256=tTzzwtUfDIEO1537-VikLDAGCD84wWKbGuqgnEN88hI,707
|
|
8
|
+
apitally_serverless/common/headers.py,sha256=LdN2zIJMyixpXapD1H4yjli8IH61VZk-UiaGR_dQKiQ,1005
|
|
9
|
+
apitally_serverless/common/masking.py,sha256=xL8ZCUvzfIW8ovsDrf_E3YXHhHFjFicnStPP27IjDNI,5194
|
|
10
|
+
apitally_serverless/common/output.py,sha256=s7QFE2GKOE4KAO_qM7YoUVDRrng_3Hw8yiz-oT1PBsM,2290
|
|
11
|
+
apitally_serverless-0.1.0.dist-info/METADATA,sha256=csYku0V916vM1pud9w6WUQsmT8kZ3f1vCc44eqWQqD0,5428
|
|
12
|
+
apitally_serverless-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
13
|
+
apitally_serverless-0.1.0.dist-info/licenses/LICENSE,sha256=gHcos93adDs96NZDFXkMZaHuMXmP8z-xlrR_TKqsaRQ,1065
|
|
14
|
+
apitally_serverless-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Apitally
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|