loki-middleware 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.
|
File without changes
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: loki-middleware
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Structured logging middleware for FastAPI with Loki integration
|
|
5
|
+
Home-page: https://github.com/IlemLembo/loki-middleware
|
|
6
|
+
Author: LEMBO Ilem Nelson
|
|
7
|
+
Author-email: lemboilem@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: System Administrators
|
|
14
|
+
Classifier: Intended Audience :: Information Technology
|
|
15
|
+
Requires-Python: >=3.6
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENCE.md
|
|
18
|
+
Requires-Dist: fastapi>=0.136.0
|
|
19
|
+
Requires-Dist: python-logging-loki>=0.3.1
|
|
20
|
+
Requires-Dist: geocoder>=1.38.1
|
|
21
|
+
Requires-Dist: dict_field_redacter>=0.1.3
|
|
22
|
+
Requires-Dist: colorama>=0.4.6
|
|
23
|
+
Dynamic: author
|
|
24
|
+
Dynamic: author-email
|
|
25
|
+
Dynamic: classifier
|
|
26
|
+
Dynamic: description
|
|
27
|
+
Dynamic: description-content-type
|
|
28
|
+
Dynamic: home-page
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
Dynamic: requires-dist
|
|
31
|
+
Dynamic: requires-python
|
|
32
|
+
Dynamic: summary
|
|
33
|
+
|
|
34
|
+
# loki-middleware
|
|
35
|
+
|
|
36
|
+

|
|
37
|
+
[](https://www.python.org/downloads/)
|
|
38
|
+
[](https://opensource.org/licenses/MIT)
|
|
39
|
+
[](https://pypi.org/project/loki-middleware/)
|
|
40
|
+
|
|
41
|
+
**Structured logging middleware for FastAPI with Loki integration.**
|
|
42
|
+
|
|
43
|
+
Middleware that captures detailed HTTP request/response data and geolocation, sending structured logs to Loki for visualization in Grafana. OpenTelemetry / distributed-tracing correlation is currently a placeholder and requires additional configuration in the application (see notes below).
|
|
44
|
+
|
|
45
|
+
## ๐ฏ Features
|
|
46
|
+
|
|
47
|
+
- โ
**Structured JSON Logging** - Machine-readable logs with consistent schema
|
|
48
|
+
- โ
**Loki Integration** - Direct push to Grafana Loki for log aggregation
|
|
49
|
+
- โ
**Distributed Tracing (partial)** - trace/span correlation is a placeholder; full OpenTelemetry extraction requires configuring OpenTelemetry in your app and is not yet fully implemented in the middleware
|
|
50
|
+
- โ
**Geolocation** - Automatic IP geolocation (city, country, coordinates)
|
|
51
|
+
- โ
**Request/Response Capture** - Full body logging with content-type handling
|
|
52
|
+
- โ
**Multiple Frameworks** - FastAPI (current). Django integration is planned.
|
|
53
|
+
- โ
**Performance Metrics** - Request execution time tracking
|
|
54
|
+
- โ
**Error Handling** - Graceful degradation and error alerting
|
|
55
|
+
- โ
**Customizable** - Exclude paths, configure tags, adjust formatters
|
|
56
|
+
- โ
**Production Ready** - Error recovery, client disconnect handling
|
|
57
|
+
|
|
58
|
+
## ๐ฆ Installation
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install loki-middleware
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Optional: With Django support (coming soon)
|
|
65
|
+
```bash
|
|
66
|
+
pip install loki-middleware[django]
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Development dependencies
|
|
70
|
+
```bash
|
|
71
|
+
pip install loki-middleware[dev]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## ๐ Quick Start
|
|
75
|
+
|
|
76
|
+
### FastAPI Setup
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from fastapi import FastAPI
|
|
80
|
+
from loki_middleware.fastapi.middleware import LokiLoggingMiddleware
|
|
81
|
+
|
|
82
|
+
app = FastAPI()
|
|
83
|
+
|
|
84
|
+
# Add middleware (current API)
|
|
85
|
+
app.add_middleware(
|
|
86
|
+
LokiLoggingMiddleware,
|
|
87
|
+
exclude_paths=["/health", "/metrics"]
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
@app.get("/")
|
|
91
|
+
async def root():
|
|
92
|
+
return {"message": "Hello World"}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## โ๏ธ Configuration
|
|
96
|
+
|
|
97
|
+
### Environment Variables
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
# Loki server (full URL, e.g. http://127.0.0.1:3100/loki/api/v1/push)
|
|
101
|
+
LOKI_URL=http://127.0.0.1:3100/loki/api/v1/push
|
|
102
|
+
|
|
103
|
+
# Application metadata (used as static tags)
|
|
104
|
+
LOKI_APPLICATION=my-app
|
|
105
|
+
LOKI_ENVIRONMENT=production
|
|
106
|
+
LOKI_SERVICE=backend
|
|
107
|
+
|
|
108
|
+
# Optional request host override used by the middleware
|
|
109
|
+
REQUEST_HOST=api.example.com
|
|
110
|
+
|
|
111
|
+
# Optional: comma-separated sensitive fields to redact
|
|
112
|
+
LOKI_SENSITIVE_FIELDS=password,token,authorization
|
|
113
|
+
LOKI_MASK_VALUE=********
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Configuration Notes
|
|
117
|
+
|
|
118
|
+
The repository does not currently expose a `LokiConfig` class. The middleware accepts the following runtime option when added to FastAPI:
|
|
119
|
+
|
|
120
|
+
- `exclude_paths` (list): paths to ignore (default includes `/health`, `/metrics`, `/docs`)
|
|
121
|
+
|
|
122
|
+
Application / Loki connection metadata is read from environment variables (see the section above). A `LokiConfig` helper/constructor may be added in a future release.
|
|
123
|
+
|
|
124
|
+
## ๐ Logged Data
|
|
125
|
+
|
|
126
|
+
### Request Information
|
|
127
|
+
```json
|
|
128
|
+
{
|
|
129
|
+
"request_id": "550e8400-e29b-41d4-a716-446655440000",
|
|
130
|
+
"request_method": "POST",
|
|
131
|
+
"request_path": "/api/users?page=1",
|
|
132
|
+
"request_ip": "192.168.1.100",
|
|
133
|
+
"request_host": "api.example.com",
|
|
134
|
+
"request_origin": "https://app.example.com",
|
|
135
|
+
"request_user_agent": "Mozilla/5.0...",
|
|
136
|
+
"request_body": { "name": "John", "email": "john@example.com" },
|
|
137
|
+
"request_location": "Paris, รle-de-France, France",
|
|
138
|
+
"request_location_latlng": [48.8566, 2.3522]
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Response Information
|
|
143
|
+
```json
|
|
144
|
+
{
|
|
145
|
+
"response_status": "successful",
|
|
146
|
+
"response_status_code": 201,
|
|
147
|
+
"response_time": "0.1234s",
|
|
148
|
+
"response_type": "JSONResponse",
|
|
149
|
+
"response_content_type": "application/json",
|
|
150
|
+
"response_body": { "id": 123, "name": "John" }
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Distributed Tracing
|
|
155
|
+
|
|
156
|
+
The middleware contains a placeholder for extracting OpenTelemetry trace/span context, but this is not yet active. If you configure OpenTelemetry in your app the trace/span may be available โ full automatic correlation is planned in a follow-up release.
|
|
157
|
+
|
|
158
|
+
## ๐ Query Examples in Grafana
|
|
159
|
+
|
|
160
|
+
### All errors in production
|
|
161
|
+
```logql
|
|
162
|
+
{service="backend", environment="production"} | json | severity="error"
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Slow requests (>1s)
|
|
166
|
+
```logql
|
|
167
|
+
{application="my-api"} | json | response_time > 1s
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Failed API calls
|
|
171
|
+
```logql
|
|
172
|
+
{service="backend"} | json | response_status_code >= 400
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### By specific user location
|
|
176
|
+
```logql
|
|
177
|
+
{application="my-api"} | json | request_location=~"Paris.*"
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## ๐ Performance Considerations
|
|
181
|
+
|
|
182
|
+
- **Geolocation caching**: Consider implementing Redis caching for IP lookups
|
|
183
|
+
- **Body size limits**: Large response bodies are truncated at ~1000 chars by the middleware; request bodies are currently captured fully (consider adding a request-size limit if needed)
|
|
184
|
+
- **Streaming responses**: Handled by consuming and rebuilding the iterator; review memory implications for very large streams
|
|
185
|
+
- **Client disconnections**: Gracefully handled without errors
|
|
186
|
+
|
|
187
|
+
## ๐ Advanced Usage
|
|
188
|
+
|
|
189
|
+
### Custom Exclude Paths
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
config = LokiConfig(
|
|
193
|
+
exclude_paths=[
|
|
194
|
+
"/health",
|
|
195
|
+
"/metrics",
|
|
196
|
+
"/static",
|
|
197
|
+
"/admin"
|
|
198
|
+
]
|
|
199
|
+
)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### OpenTelemetry Integration
|
|
203
|
+
|
|
204
|
+
OpenTelemetry correlation is currently a placeholder in the middleware (there are commented sections in the code). To get full trace/span correlation you should configure OpenTelemetry in your application and a follow-up release will add automatic extraction and enrichment of trace IDs.
|
|
205
|
+
|
|
206
|
+
## ๐ Planned Features
|
|
207
|
+
|
|
208
|
+
- [ ] Django middleware
|
|
209
|
+
- [ ] FastAPI dependency injection helper
|
|
210
|
+
- [ ] Response time percentile tracking
|
|
211
|
+
- [ ] `LokiConfig` helper/constructor and public API for runtime configuration
|
|
212
|
+
- [ ] Improved OpenTelemetry automatic correlation
|
|
213
|
+
|
|
214
|
+
## ๐งช Testing
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
pytest tests/
|
|
218
|
+
pytest tests/test_fastapi.py -v
|
|
219
|
+
pytest tests/test_django.py -v
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## ๐ค Contributing
|
|
223
|
+
|
|
224
|
+
Contributions welcome! Please:
|
|
225
|
+
1. Fork the repository
|
|
226
|
+
2. Create a feature branch
|
|
227
|
+
3. Add tests
|
|
228
|
+
4. Submit a pull request
|
|
229
|
+
|
|
230
|
+
## ๐ License
|
|
231
|
+
|
|
232
|
+
MIT License - see [LICENSE](LICENSE) file for details
|
|
233
|
+
|
|
234
|
+
## ๐ Acknowledgments
|
|
235
|
+
|
|
236
|
+
- [Loki](https://grafana.com/oss/loki/) - Log aggregation system
|
|
237
|
+
- [FastAPI](https://fastapi.tiangolo.com/) - Web framework
|
|
238
|
+
- [OpenTelemetry](https://opentelemetry.io/) - Distributed tracing
|
|
239
|
+
|
|
240
|
+
## ๐ Support
|
|
241
|
+
|
|
242
|
+
- **Issues**: [GitHub Issues](https://github.com/IlemLembo/loki-middleware/issues)
|
|
243
|
+
- **Email**: lemboilem@gmail.com
|
|
244
|
+
- **Docs**: [Full Documentation](https://github.com/IlemLembo/loki-middleware/wiki)
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
**Made with โค๏ธ for better observability**
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
loki_middleware/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
loki_middleware-0.1.0.dist-info/licenses/LICENCE.md,sha256=HJBWaB4XWlq58gGODlcr2t11BAg-zYkiuTeG5MmSQu0,1296
|
|
3
|
+
loki_middleware-0.1.0.dist-info/METADATA,sha256=-euZLsr-5YCQkjrgVB2fME6rN4SQKdvwhQbcEDmDpks,8102
|
|
4
|
+
loki_middleware-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
5
|
+
loki_middleware-0.1.0.dist-info/top_level.txt,sha256=x3vFNw_9p6OEEe5eA9yKDlpp68Cj5Epljz9C3xzMl7A,16
|
|
6
|
+
loki_middleware-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 [Your Name]
|
|
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.
|
|
22
|
+
|
|
23
|
+
## Contributing
|
|
24
|
+
|
|
25
|
+
We welcome contributions! Please feel free to:
|
|
26
|
+
- Report issues
|
|
27
|
+
- Submit pull requests
|
|
28
|
+
- Suggest improvements
|
|
29
|
+
|
|
30
|
+
All contributions will be reviewed and must comply with this license.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
loki_middleware
|