auditry 0.2.4__tar.gz → 0.2.6__tar.gz
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.
- {auditry-0.2.4/src/auditry.egg-info → auditry-0.2.6}/PKG-INFO +78 -1
- {auditry-0.2.4 → auditry-0.2.6}/README.md +77 -0
- {auditry-0.2.4 → auditry-0.2.6}/pyproject.toml +1 -1
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/__init__.py +1 -1
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/fastapi/middleware.py +25 -6
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/models.py +10 -1
- auditry-0.2.6/src/auditry/path_matcher.py +94 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/quart/adapters.py +5 -15
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/quart/middleware.py +53 -33
- {auditry-0.2.4 → auditry-0.2.6/src/auditry.egg-info}/PKG-INFO +78 -1
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry.egg-info/SOURCES.txt +4 -2
- auditry-0.2.6/tests/test_fastapi_middleware.py +31 -0
- auditry-0.2.6/tests/test_fastapi_streaming.py +258 -0
- auditry-0.2.6/tests/test_path_exclusion.py +349 -0
- auditry-0.2.6/tests/test_quart_middleware.py +52 -0
- auditry-0.2.6/tests/test_quart_streaming.py +231 -0
- auditry-0.2.4/tests/test_body_logging.py +0 -166
- auditry-0.2.4/tests/test_correlation.py +0 -9
- auditry-0.2.4/tests/test_fastapi_middleware.py +0 -85
- auditry-0.2.4/tests/test_quart_middleware.py +0 -167
- {auditry-0.2.4 → auditry-0.2.6}/LICENSE +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/MANIFEST.in +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/setup.cfg +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/core/__init__.py +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/core/base.py +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/core/logger.py +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/correlation.py +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/fastapi/__init__.py +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/fastapi/adapters.py +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/logging_config.py +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/py.typed +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/quart/__init__.py +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry/redaction.py +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry.egg-info/dependency_links.txt +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry.egg-info/requires.txt +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/src/auditry.egg-info/top_level.txt +0 -0
- {auditry-0.2.4 → auditry-0.2.6}/tests/test_redaction.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: auditry
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.6
|
|
4
4
|
Summary: Observability middleware for FastAPI and Quart with request/response logging, correlation IDs, and sensitive data redaction
|
|
5
5
|
Author-email: Liv Stark <livstark.work@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -406,6 +406,83 @@ config = ObservabilityConfig(
|
|
|
406
406
|
```
|
|
407
407
|
|
|
408
408
|
When body logging is disabled, logs will show `[BODY_LOGGING_DISABLED]` instead of the actual content, while still logging metadata like headers, status codes, and timing information.
|
|
409
|
+
|
|
410
|
+
### Excluding Paths from Middleware
|
|
411
|
+
|
|
412
|
+
You can exclude specific paths from observability middleware processing, which is useful for:
|
|
413
|
+
- Health check endpoints that don't need logging
|
|
414
|
+
- Streaming endpoints that might be disrupted by middleware
|
|
415
|
+
- Internal/admin endpoints with different logging requirements
|
|
416
|
+
- High-frequency endpoints where logging adds too much overhead
|
|
417
|
+
|
|
418
|
+
#### Simple Path Exclusion
|
|
419
|
+
|
|
420
|
+
Exclude paths for all HTTP methods:
|
|
421
|
+
|
|
422
|
+
```python
|
|
423
|
+
config = ObservabilityConfig(
|
|
424
|
+
service_name="my-service-name",
|
|
425
|
+
excluded_paths=[
|
|
426
|
+
'/health', # Exact match
|
|
427
|
+
'/metrics', # Exact match
|
|
428
|
+
'/stream*', # Wildcard - matches /stream, /streaming, /stream/events
|
|
429
|
+
'/api/*/internal', # Wildcard - matches /api/v1/internal, /api/v2/internal
|
|
430
|
+
'/admin/', # Prefix - matches /admin/* (trailing slash indicates prefix)
|
|
431
|
+
],
|
|
432
|
+
)
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
#### Method-Specific Path Exclusion
|
|
436
|
+
|
|
437
|
+
Exclude paths for specific HTTP methods:
|
|
438
|
+
|
|
439
|
+
```python
|
|
440
|
+
config = ObservabilityConfig(
|
|
441
|
+
service_name="my-service-name",
|
|
442
|
+
excluded_paths={
|
|
443
|
+
'GET': ['/health', '/metrics'], # Only exclude GET requests
|
|
444
|
+
'POST': ['/webhook/*'], # Only exclude POST requests
|
|
445
|
+
'*': ['/admin/*'], # Exclude all methods
|
|
446
|
+
},
|
|
447
|
+
)
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
#### Example: Excluding Streaming Endpoints in Quart
|
|
451
|
+
|
|
452
|
+
For Quart applications with streaming endpoints that are disrupted by middleware:
|
|
453
|
+
|
|
454
|
+
```python
|
|
455
|
+
from quart import Quart, Response, stream_with_context
|
|
456
|
+
from auditry import ObservabilityConfig
|
|
457
|
+
from auditry.quart import create_middleware
|
|
458
|
+
|
|
459
|
+
app = Quart(__name__)
|
|
460
|
+
|
|
461
|
+
# Exclude all streaming endpoints
|
|
462
|
+
config = ObservabilityConfig(
|
|
463
|
+
service_name="streaming-service",
|
|
464
|
+
excluded_paths=['/stream/*', '/events', '/live/*'],
|
|
465
|
+
)
|
|
466
|
+
|
|
467
|
+
app = create_middleware(app, config)
|
|
468
|
+
|
|
469
|
+
@app.route('/stream/data')
|
|
470
|
+
async def stream_data():
|
|
471
|
+
"""This endpoint will bypass observability middleware."""
|
|
472
|
+
@stream_with_context
|
|
473
|
+
async def generate():
|
|
474
|
+
for i in range(100):
|
|
475
|
+
yield f"data: {i}\n\n"
|
|
476
|
+
return Response(generate(), mimetype='text/event-stream')
|
|
477
|
+
|
|
478
|
+
@app.route('/api/users')
|
|
479
|
+
async def get_users():
|
|
480
|
+
"""This endpoint will still be logged normally."""
|
|
481
|
+
return {"users": ["user1", "user2"]}
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
**Note:** Excluded paths still receive correlation IDs for request tracing, but no request/response logging occurs.
|
|
485
|
+
|
|
409
486
|
## Best Practices
|
|
410
487
|
|
|
411
488
|
### 1. Configure Logging Early
|
|
@@ -363,6 +363,83 @@ config = ObservabilityConfig(
|
|
|
363
363
|
```
|
|
364
364
|
|
|
365
365
|
When body logging is disabled, logs will show `[BODY_LOGGING_DISABLED]` instead of the actual content, while still logging metadata like headers, status codes, and timing information.
|
|
366
|
+
|
|
367
|
+
### Excluding Paths from Middleware
|
|
368
|
+
|
|
369
|
+
You can exclude specific paths from observability middleware processing, which is useful for:
|
|
370
|
+
- Health check endpoints that don't need logging
|
|
371
|
+
- Streaming endpoints that might be disrupted by middleware
|
|
372
|
+
- Internal/admin endpoints with different logging requirements
|
|
373
|
+
- High-frequency endpoints where logging adds too much overhead
|
|
374
|
+
|
|
375
|
+
#### Simple Path Exclusion
|
|
376
|
+
|
|
377
|
+
Exclude paths for all HTTP methods:
|
|
378
|
+
|
|
379
|
+
```python
|
|
380
|
+
config = ObservabilityConfig(
|
|
381
|
+
service_name="my-service-name",
|
|
382
|
+
excluded_paths=[
|
|
383
|
+
'/health', # Exact match
|
|
384
|
+
'/metrics', # Exact match
|
|
385
|
+
'/stream*', # Wildcard - matches /stream, /streaming, /stream/events
|
|
386
|
+
'/api/*/internal', # Wildcard - matches /api/v1/internal, /api/v2/internal
|
|
387
|
+
'/admin/', # Prefix - matches /admin/* (trailing slash indicates prefix)
|
|
388
|
+
],
|
|
389
|
+
)
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
#### Method-Specific Path Exclusion
|
|
393
|
+
|
|
394
|
+
Exclude paths for specific HTTP methods:
|
|
395
|
+
|
|
396
|
+
```python
|
|
397
|
+
config = ObservabilityConfig(
|
|
398
|
+
service_name="my-service-name",
|
|
399
|
+
excluded_paths={
|
|
400
|
+
'GET': ['/health', '/metrics'], # Only exclude GET requests
|
|
401
|
+
'POST': ['/webhook/*'], # Only exclude POST requests
|
|
402
|
+
'*': ['/admin/*'], # Exclude all methods
|
|
403
|
+
},
|
|
404
|
+
)
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
#### Example: Excluding Streaming Endpoints in Quart
|
|
408
|
+
|
|
409
|
+
For Quart applications with streaming endpoints that are disrupted by middleware:
|
|
410
|
+
|
|
411
|
+
```python
|
|
412
|
+
from quart import Quart, Response, stream_with_context
|
|
413
|
+
from auditry import ObservabilityConfig
|
|
414
|
+
from auditry.quart import create_middleware
|
|
415
|
+
|
|
416
|
+
app = Quart(__name__)
|
|
417
|
+
|
|
418
|
+
# Exclude all streaming endpoints
|
|
419
|
+
config = ObservabilityConfig(
|
|
420
|
+
service_name="streaming-service",
|
|
421
|
+
excluded_paths=['/stream/*', '/events', '/live/*'],
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
app = create_middleware(app, config)
|
|
425
|
+
|
|
426
|
+
@app.route('/stream/data')
|
|
427
|
+
async def stream_data():
|
|
428
|
+
"""This endpoint will bypass observability middleware."""
|
|
429
|
+
@stream_with_context
|
|
430
|
+
async def generate():
|
|
431
|
+
for i in range(100):
|
|
432
|
+
yield f"data: {i}\n\n"
|
|
433
|
+
return Response(generate(), mimetype='text/event-stream')
|
|
434
|
+
|
|
435
|
+
@app.route('/api/users')
|
|
436
|
+
async def get_users():
|
|
437
|
+
"""This endpoint will still be logged normally."""
|
|
438
|
+
return {"users": ["user1", "user2"]}
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
**Note:** Excluded paths still receive correlation IDs for request tracing, but no request/response logging occurs.
|
|
442
|
+
|
|
366
443
|
## Best Practices
|
|
367
444
|
|
|
368
445
|
### 1. Configure Logging Early
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "auditry"
|
|
3
|
-
version = "0.2.
|
|
3
|
+
version = "0.2.6"
|
|
4
4
|
description = "Observability middleware for FastAPI and Quart with request/response logging, correlation IDs, and sensitive data redaction"
|
|
5
5
|
authors = [
|
|
6
6
|
{name = "Liv Stark", email = "livstark.work@gmail.com"}
|
|
@@ -6,15 +6,16 @@ the Starlette middleware system and uses the core logging functionality.
|
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
import time
|
|
9
|
-
from typing import Optional
|
|
10
9
|
|
|
11
10
|
from fastapi import Request, Response
|
|
12
11
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
12
|
+
from starlette.responses import StreamingResponse
|
|
13
13
|
from asgi_correlation_id import CorrelationIdMiddleware
|
|
14
14
|
|
|
15
15
|
from ..core import BaseMiddleware, RequestResponseLogger
|
|
16
16
|
from ..correlation import get_correlation_id
|
|
17
17
|
from ..models import ObservabilityConfig
|
|
18
|
+
from ..path_matcher import should_exclude_path
|
|
18
19
|
from .adapters import FastAPIRequestAdapter, FastAPIResponseAdapter
|
|
19
20
|
|
|
20
21
|
|
|
@@ -79,6 +80,17 @@ class FastAPIMiddleware(BaseHTTPMiddleware, BaseMiddleware):
|
|
|
79
80
|
Returns:
|
|
80
81
|
The FastAPI response
|
|
81
82
|
"""
|
|
83
|
+
# Check if this path is excluded from observability
|
|
84
|
+
path = str(request.url.path)
|
|
85
|
+
method = request.method
|
|
86
|
+
if should_exclude_path(path, method, self.config.excluded_paths):
|
|
87
|
+
# For excluded paths, just pass through with correlation ID
|
|
88
|
+
response = await call_next(request)
|
|
89
|
+
correlation_id = get_correlation_id()
|
|
90
|
+
if correlation_id and self.config.correlation_id_header:
|
|
91
|
+
response.headers[self.config.correlation_id_header] = correlation_id
|
|
92
|
+
return response
|
|
93
|
+
|
|
82
94
|
# Get correlation ID (set by correlation middleware if present)
|
|
83
95
|
correlation_id = get_correlation_id()
|
|
84
96
|
|
|
@@ -104,11 +116,18 @@ class FastAPIMiddleware(BaseHTTPMiddleware, BaseMiddleware):
|
|
|
104
116
|
# Re-extract user_id (might have been set by auth middleware)
|
|
105
117
|
user_id = await self.request_adapter.extract_user_id(request)
|
|
106
118
|
|
|
107
|
-
#
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
119
|
+
# Handle response data extraction based on response type
|
|
120
|
+
if isinstance(response, StreamingResponse):
|
|
121
|
+
# For streaming responses, log without consuming the stream
|
|
122
|
+
response_data = {
|
|
123
|
+
"status_code": response.status_code,
|
|
124
|
+
"headers": dict(response.headers),
|
|
125
|
+
"body": None
|
|
126
|
+
}
|
|
127
|
+
else:
|
|
128
|
+
# For non-streaming responses, extract full response data
|
|
129
|
+
raw_response_data = await self.response_adapter.extract_all(response)
|
|
130
|
+
response_data = self.logger.prepare_response_data(raw_response_data)
|
|
112
131
|
|
|
113
132
|
# Add correlation ID to response headers
|
|
114
133
|
if correlation_id and self.config.correlation_id_header:
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Optional, Dict, List
|
|
1
|
+
from typing import Optional, Dict, List, Union
|
|
2
2
|
from pydantic import BaseModel, Field, field_validator
|
|
3
3
|
|
|
4
4
|
|
|
@@ -76,4 +76,13 @@ class ObservabilityConfig(BaseModel):
|
|
|
76
76
|
log_response_body: bool = Field(
|
|
77
77
|
default=True,
|
|
78
78
|
description="Whether to log response bodies for the application"
|
|
79
|
+
)
|
|
80
|
+
excluded_paths: Optional[Union[List[str], Dict[str, List[str]]]] = Field(
|
|
81
|
+
default=None,
|
|
82
|
+
description=(
|
|
83
|
+
"Paths to exclude from observability middleware. "
|
|
84
|
+
"Can be a list of path patterns (e.g., ['/health', '/metrics', '/stream*']) "
|
|
85
|
+
"or a dict mapping HTTP methods to paths (e.g., {'GET': ['/health'], 'POST': ['/stream*']}). "
|
|
86
|
+
"Supports wildcards (*) for pattern matching."
|
|
87
|
+
)
|
|
79
88
|
)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Path matching utilities for excluding paths from middleware processing.
|
|
3
|
+
|
|
4
|
+
This module provides functions to check if a request path should be excluded
|
|
5
|
+
from observability middleware based on configured patterns.
|
|
6
|
+
"""
|
|
7
|
+
import fnmatch
|
|
8
|
+
from typing import Optional, List, Dict, Union
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def should_exclude_path(
|
|
12
|
+
path: str,
|
|
13
|
+
method: str,
|
|
14
|
+
excluded_paths: Optional[Union[List[str], Dict[str, List[str]]]]
|
|
15
|
+
) -> bool:
|
|
16
|
+
"""
|
|
17
|
+
Check if a request path should be excluded from middleware processing.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
path: The request path (e.g., '/api/health', '/stream/events')
|
|
21
|
+
method: The HTTP method (e.g., 'GET', 'POST')
|
|
22
|
+
excluded_paths: Configuration for excluded paths, can be:
|
|
23
|
+
- None: No paths excluded
|
|
24
|
+
- List[str]: List of path patterns to exclude for all methods
|
|
25
|
+
- Dict[str, List[str]]: Method-specific path patterns
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
True if the path should be excluded, False otherwise
|
|
29
|
+
|
|
30
|
+
Examples:
|
|
31
|
+
>>> # Exclude all methods
|
|
32
|
+
>>> should_exclude_path('/health', 'GET', ['/health', '/metrics'])
|
|
33
|
+
True
|
|
34
|
+
|
|
35
|
+
>>> # Method-specific exclusion
|
|
36
|
+
>>> should_exclude_path('/stream', 'GET', {'GET': ['/stream*']})
|
|
37
|
+
True
|
|
38
|
+
|
|
39
|
+
>>> # Wildcard patterns
|
|
40
|
+
>>> should_exclude_path('/api/v1/stream/events', 'POST', ['/api/*/stream/*'])
|
|
41
|
+
True
|
|
42
|
+
"""
|
|
43
|
+
if excluded_paths is None:
|
|
44
|
+
return False
|
|
45
|
+
|
|
46
|
+
# Normalize path (remove query params if present)
|
|
47
|
+
if '?' in path:
|
|
48
|
+
path = path.split('?')[0]
|
|
49
|
+
|
|
50
|
+
# Handle list format - applies to all methods
|
|
51
|
+
if isinstance(excluded_paths, list):
|
|
52
|
+
return _match_path_patterns(path, excluded_paths)
|
|
53
|
+
|
|
54
|
+
# Handle dict format - method-specific patterns
|
|
55
|
+
if isinstance(excluded_paths, dict):
|
|
56
|
+
# Check method-specific patterns
|
|
57
|
+
method_patterns = excluded_paths.get(method.upper(), [])
|
|
58
|
+
if _match_path_patterns(path, method_patterns):
|
|
59
|
+
return True
|
|
60
|
+
|
|
61
|
+
# Check wildcard method patterns (applies to all methods)
|
|
62
|
+
all_patterns = excluded_paths.get('*', [])
|
|
63
|
+
if _match_path_patterns(path, all_patterns):
|
|
64
|
+
return True
|
|
65
|
+
|
|
66
|
+
return False
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _match_path_patterns(path: str, patterns: List[str]) -> bool:
|
|
70
|
+
"""
|
|
71
|
+
Check if a path matches any of the given patterns.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
path: The request path to check
|
|
75
|
+
patterns: List of patterns to match against
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
True if the path matches any pattern, False otherwise
|
|
79
|
+
"""
|
|
80
|
+
for pattern in patterns:
|
|
81
|
+
# Handle exact matches
|
|
82
|
+
if pattern == path:
|
|
83
|
+
return True
|
|
84
|
+
|
|
85
|
+
# Handle wildcard patterns using fnmatch
|
|
86
|
+
if '*' in pattern or '?' in pattern:
|
|
87
|
+
if fnmatch.fnmatch(path, pattern):
|
|
88
|
+
return True
|
|
89
|
+
|
|
90
|
+
# Handle path prefix matching (e.g., '/api/' matches '/api/anything')
|
|
91
|
+
if pattern.endswith('/') and path.startswith(pattern):
|
|
92
|
+
return True
|
|
93
|
+
|
|
94
|
+
return False
|
|
@@ -5,11 +5,11 @@ These adapters implement the abstract base classes to extract data from
|
|
|
5
5
|
Quart request and response objects.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
|
-
import inspect
|
|
9
8
|
import json
|
|
10
9
|
from typing import Dict, Optional
|
|
11
10
|
|
|
12
11
|
from quart import Request, Response, g
|
|
12
|
+
from quart.wrappers.response import IterableBody
|
|
13
13
|
|
|
14
14
|
from ..core import BaseRequestAdapter, BaseResponseAdapter
|
|
15
15
|
|
|
@@ -140,23 +140,17 @@ class QuartResponseAdapter(BaseResponseAdapter):
|
|
|
140
140
|
try:
|
|
141
141
|
# For Response objects
|
|
142
142
|
if hasattr(response, "response"):
|
|
143
|
-
# Check if the response is an
|
|
144
|
-
if
|
|
143
|
+
# Check if the response is an IterableBody (streaming response)
|
|
144
|
+
if isinstance(response.response, IterableBody):
|
|
145
145
|
# This is a streaming response, return None to avoid consuming it
|
|
146
146
|
return None
|
|
147
147
|
|
|
148
|
-
# Check if it's a regular generator (also streaming)
|
|
149
|
-
if inspect.isgenerator(response.response):
|
|
150
|
-
return None
|
|
151
|
-
|
|
152
148
|
# For Response objects with get_data method (non-streaming)
|
|
153
149
|
if hasattr(response, "get_data"):
|
|
154
150
|
# Only call get_data if we're sure it's not streaming
|
|
155
151
|
# Double-check for streaming response attribute
|
|
156
|
-
if hasattr(response, "response"):
|
|
157
|
-
|
|
158
|
-
if inspect.isasyncgen(resp_attr) or inspect.isgenerator(resp_attr):
|
|
159
|
-
return None
|
|
152
|
+
if hasattr(response, "response") and isinstance(response.response, IterableBody):
|
|
153
|
+
return None
|
|
160
154
|
|
|
161
155
|
# Safe to get data for non-streaming responses
|
|
162
156
|
data = await response.get_data()
|
|
@@ -168,10 +162,6 @@ class QuartResponseAdapter(BaseResponseAdapter):
|
|
|
168
162
|
if isinstance(response, tuple) and len(response) >= 1:
|
|
169
163
|
body = response[0]
|
|
170
164
|
|
|
171
|
-
# Check if body is a generator (streaming)
|
|
172
|
-
if inspect.isasyncgen(body) or inspect.isgenerator(body):
|
|
173
|
-
return None
|
|
174
|
-
|
|
175
165
|
# Convert various body types to bytes
|
|
176
166
|
if isinstance(body, bytes):
|
|
177
167
|
return body
|
|
@@ -4,15 +4,16 @@ Quart middleware implementation for observability.
|
|
|
4
4
|
This module provides the Quart-specific middleware that integrates with
|
|
5
5
|
Quart's request/response hooks and uses the core logging functionality.
|
|
6
6
|
"""
|
|
7
|
-
import inspect
|
|
8
7
|
import time
|
|
9
8
|
|
|
10
9
|
from quart import Quart, Request, Response, request
|
|
10
|
+
from quart.wrappers.response import IterableBody
|
|
11
11
|
from asgi_correlation_id import CorrelationIdMiddleware
|
|
12
12
|
|
|
13
13
|
from ..core import BaseMiddleware, RequestResponseLogger
|
|
14
14
|
from ..correlation import get_correlation_id
|
|
15
15
|
from ..models import ObservabilityConfig
|
|
16
|
+
from ..path_matcher import should_exclude_path
|
|
16
17
|
from .adapters import QuartRequestAdapter, QuartResponseAdapter
|
|
17
18
|
|
|
18
19
|
|
|
@@ -67,9 +68,17 @@ class QuartMiddleware(BaseMiddleware):
|
|
|
67
68
|
@self.app.before_request
|
|
68
69
|
async def capture_request_start():
|
|
69
70
|
"""Capture request start time and initial data."""
|
|
71
|
+
# Check if this path is excluded from observability
|
|
72
|
+
if should_exclude_path(request.path, request.method, self.config.excluded_paths):
|
|
73
|
+
request.observability_excluded = True
|
|
74
|
+
# Still set correlation ID for excluded paths (useful for tracing)
|
|
75
|
+
request.observability_correlation_id = get_correlation_id()
|
|
76
|
+
return
|
|
77
|
+
|
|
70
78
|
# Store timing and correlation data
|
|
71
79
|
request.observability_start_time = time.time()
|
|
72
80
|
request.observability_correlation_id = get_correlation_id()
|
|
81
|
+
request.observability_excluded = False
|
|
73
82
|
|
|
74
83
|
# Pre-extract request data (especially body which needs caching)
|
|
75
84
|
raw_request_data = await self.request_adapter.extract_all(request)
|
|
@@ -84,43 +93,50 @@ class QuartMiddleware(BaseMiddleware):
|
|
|
84
93
|
@self.app.after_request
|
|
85
94
|
async def log_request_response(response: Response) -> Response:
|
|
86
95
|
"""Log the request/response after processing."""
|
|
96
|
+
# Check if this path was excluded
|
|
97
|
+
if getattr(request, "observability_excluded", False):
|
|
98
|
+
# Still add correlation ID to response headers for excluded paths
|
|
99
|
+
correlation_id = getattr(request, "observability_correlation_id", None)
|
|
100
|
+
if correlation_id and self.config.correlation_id_header:
|
|
101
|
+
response.headers[self.config.correlation_id_header] = correlation_id
|
|
102
|
+
return response
|
|
103
|
+
|
|
87
104
|
# Check if we have request data (might not if before_request wasn't called)
|
|
88
105
|
if not hasattr(request, "observability_start_time"):
|
|
89
106
|
return response
|
|
90
107
|
|
|
91
108
|
# Check if this is a streaming response - if so, handle it separately
|
|
92
|
-
if hasattr(response, "response"):
|
|
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
|
-
return response
|
|
109
|
+
if hasattr(response, "response") and isinstance(response.response, IterableBody):
|
|
110
|
+
# For streaming responses, log without consuming the stream
|
|
111
|
+
duration_ms = (time.time() - request.observability_start_time) * 1000
|
|
112
|
+
|
|
113
|
+
# Get stored correlation ID from before_request
|
|
114
|
+
correlation_id = getattr(request, "observability_correlation_id", None)
|
|
115
|
+
request_data = getattr(request, "observability_request_data", {})
|
|
116
|
+
|
|
117
|
+
# Re-extract user_id (might have been set during request processing)
|
|
118
|
+
user_id = await self.request_adapter.extract_user_id(request)
|
|
119
|
+
|
|
120
|
+
# Log with minimal response data (no body for streaming)
|
|
121
|
+
self.logger.log_success(
|
|
122
|
+
request_data=request_data,
|
|
123
|
+
response_data={
|
|
124
|
+
"status_code": response.status_code if hasattr(response, "status_code") else 200,
|
|
125
|
+
"headers": dict(response.headers) if hasattr(response, "headers") else {},
|
|
126
|
+
"body": None # Don't try to extract body for streaming
|
|
127
|
+
},
|
|
128
|
+
duration_ms=duration_ms,
|
|
129
|
+
correlation_id=correlation_id,
|
|
130
|
+
user_id=user_id,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
# Add correlation ID to response headers (same as non-streaming)
|
|
134
|
+
if correlation_id:
|
|
135
|
+
response.headers[self.config.correlation_id_header] = correlation_id
|
|
136
|
+
|
|
137
|
+
# Clear cache and return immediately
|
|
138
|
+
self.request_adapter.clear_cache(request)
|
|
139
|
+
return response
|
|
124
140
|
|
|
125
141
|
# Non-streaming response handling
|
|
126
142
|
# Calculate duration
|
|
@@ -158,6 +174,10 @@ class QuartMiddleware(BaseMiddleware):
|
|
|
158
174
|
@self.app.errorhandler(Exception)
|
|
159
175
|
async def handle_exception(error: Exception):
|
|
160
176
|
"""Log errors with request context."""
|
|
177
|
+
# Check if this path was excluded
|
|
178
|
+
if getattr(request, "observability_excluded", False):
|
|
179
|
+
raise
|
|
180
|
+
|
|
161
181
|
# Check if we have request data
|
|
162
182
|
if hasattr(request, "observability_start_time"):
|
|
163
183
|
duration_ms = (time.time() - request.observability_start_time) * 1000
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: auditry
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.6
|
|
4
4
|
Summary: Observability middleware for FastAPI and Quart with request/response logging, correlation IDs, and sensitive data redaction
|
|
5
5
|
Author-email: Liv Stark <livstark.work@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -406,6 +406,83 @@ config = ObservabilityConfig(
|
|
|
406
406
|
```
|
|
407
407
|
|
|
408
408
|
When body logging is disabled, logs will show `[BODY_LOGGING_DISABLED]` instead of the actual content, while still logging metadata like headers, status codes, and timing information.
|
|
409
|
+
|
|
410
|
+
### Excluding Paths from Middleware
|
|
411
|
+
|
|
412
|
+
You can exclude specific paths from observability middleware processing, which is useful for:
|
|
413
|
+
- Health check endpoints that don't need logging
|
|
414
|
+
- Streaming endpoints that might be disrupted by middleware
|
|
415
|
+
- Internal/admin endpoints with different logging requirements
|
|
416
|
+
- High-frequency endpoints where logging adds too much overhead
|
|
417
|
+
|
|
418
|
+
#### Simple Path Exclusion
|
|
419
|
+
|
|
420
|
+
Exclude paths for all HTTP methods:
|
|
421
|
+
|
|
422
|
+
```python
|
|
423
|
+
config = ObservabilityConfig(
|
|
424
|
+
service_name="my-service-name",
|
|
425
|
+
excluded_paths=[
|
|
426
|
+
'/health', # Exact match
|
|
427
|
+
'/metrics', # Exact match
|
|
428
|
+
'/stream*', # Wildcard - matches /stream, /streaming, /stream/events
|
|
429
|
+
'/api/*/internal', # Wildcard - matches /api/v1/internal, /api/v2/internal
|
|
430
|
+
'/admin/', # Prefix - matches /admin/* (trailing slash indicates prefix)
|
|
431
|
+
],
|
|
432
|
+
)
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
#### Method-Specific Path Exclusion
|
|
436
|
+
|
|
437
|
+
Exclude paths for specific HTTP methods:
|
|
438
|
+
|
|
439
|
+
```python
|
|
440
|
+
config = ObservabilityConfig(
|
|
441
|
+
service_name="my-service-name",
|
|
442
|
+
excluded_paths={
|
|
443
|
+
'GET': ['/health', '/metrics'], # Only exclude GET requests
|
|
444
|
+
'POST': ['/webhook/*'], # Only exclude POST requests
|
|
445
|
+
'*': ['/admin/*'], # Exclude all methods
|
|
446
|
+
},
|
|
447
|
+
)
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
#### Example: Excluding Streaming Endpoints in Quart
|
|
451
|
+
|
|
452
|
+
For Quart applications with streaming endpoints that are disrupted by middleware:
|
|
453
|
+
|
|
454
|
+
```python
|
|
455
|
+
from quart import Quart, Response, stream_with_context
|
|
456
|
+
from auditry import ObservabilityConfig
|
|
457
|
+
from auditry.quart import create_middleware
|
|
458
|
+
|
|
459
|
+
app = Quart(__name__)
|
|
460
|
+
|
|
461
|
+
# Exclude all streaming endpoints
|
|
462
|
+
config = ObservabilityConfig(
|
|
463
|
+
service_name="streaming-service",
|
|
464
|
+
excluded_paths=['/stream/*', '/events', '/live/*'],
|
|
465
|
+
)
|
|
466
|
+
|
|
467
|
+
app = create_middleware(app, config)
|
|
468
|
+
|
|
469
|
+
@app.route('/stream/data')
|
|
470
|
+
async def stream_data():
|
|
471
|
+
"""This endpoint will bypass observability middleware."""
|
|
472
|
+
@stream_with_context
|
|
473
|
+
async def generate():
|
|
474
|
+
for i in range(100):
|
|
475
|
+
yield f"data: {i}\n\n"
|
|
476
|
+
return Response(generate(), mimetype='text/event-stream')
|
|
477
|
+
|
|
478
|
+
@app.route('/api/users')
|
|
479
|
+
async def get_users():
|
|
480
|
+
"""This endpoint will still be logged normally."""
|
|
481
|
+
return {"users": ["user1", "user2"]}
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
**Note:** Excluded paths still receive correlation IDs for request tracing, but no request/response logging occurs.
|
|
485
|
+
|
|
409
486
|
## Best Practices
|
|
410
487
|
|
|
411
488
|
### 1. Configure Logging Early
|
|
@@ -6,6 +6,7 @@ src/auditry/__init__.py
|
|
|
6
6
|
src/auditry/correlation.py
|
|
7
7
|
src/auditry/logging_config.py
|
|
8
8
|
src/auditry/models.py
|
|
9
|
+
src/auditry/path_matcher.py
|
|
9
10
|
src/auditry/py.typed
|
|
10
11
|
src/auditry/redaction.py
|
|
11
12
|
src/auditry.egg-info/PKG-INFO
|
|
@@ -22,8 +23,9 @@ src/auditry/fastapi/middleware.py
|
|
|
22
23
|
src/auditry/quart/__init__.py
|
|
23
24
|
src/auditry/quart/adapters.py
|
|
24
25
|
src/auditry/quart/middleware.py
|
|
25
|
-
tests/test_body_logging.py
|
|
26
|
-
tests/test_correlation.py
|
|
27
26
|
tests/test_fastapi_middleware.py
|
|
27
|
+
tests/test_fastapi_streaming.py
|
|
28
|
+
tests/test_path_exclusion.py
|
|
28
29
|
tests/test_quart_middleware.py
|
|
30
|
+
tests/test_quart_streaming.py
|
|
29
31
|
tests/test_redaction.py
|