loki-middleware 0.1.0__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.
@@ -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,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
+ ![Loki Middleware](assets/image.png)
37
+ [![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
38
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
39
+ [![PyPI version](https://img.shields.io/pypi/v/loki-middleware.svg)](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,215 @@
1
+ # loki-middleware
2
+
3
+ ![Loki Middleware](assets/image.png)
4
+ [![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![PyPI version](https://img.shields.io/pypi/v/loki-middleware.svg)](https://pypi.org/project/loki-middleware/)
7
+
8
+ **Structured logging middleware for FastAPI with Loki integration.**
9
+
10
+ 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).
11
+
12
+ ## ๐ŸŽฏ Features
13
+
14
+ - โœ… **Structured JSON Logging** - Machine-readable logs with consistent schema
15
+ - โœ… **Loki Integration** - Direct push to Grafana Loki for log aggregation
16
+ - โœ… **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
17
+ - โœ… **Geolocation** - Automatic IP geolocation (city, country, coordinates)
18
+ - โœ… **Request/Response Capture** - Full body logging with content-type handling
19
+ - โœ… **Multiple Frameworks** - FastAPI (current). Django integration is planned.
20
+ - โœ… **Performance Metrics** - Request execution time tracking
21
+ - โœ… **Error Handling** - Graceful degradation and error alerting
22
+ - โœ… **Customizable** - Exclude paths, configure tags, adjust formatters
23
+ - โœ… **Production Ready** - Error recovery, client disconnect handling
24
+
25
+ ## ๐Ÿ“ฆ Installation
26
+
27
+ ```bash
28
+ pip install loki-middleware
29
+ ```
30
+
31
+ ### Optional: With Django support (coming soon)
32
+ ```bash
33
+ pip install loki-middleware[django]
34
+ ```
35
+
36
+ ### Development dependencies
37
+ ```bash
38
+ pip install loki-middleware[dev]
39
+ ```
40
+
41
+ ## ๐Ÿš€ Quick Start
42
+
43
+ ### FastAPI Setup
44
+
45
+ ```python
46
+ from fastapi import FastAPI
47
+ from loki_middleware.fastapi.middleware import LokiLoggingMiddleware
48
+
49
+ app = FastAPI()
50
+
51
+ # Add middleware (current API)
52
+ app.add_middleware(
53
+ LokiLoggingMiddleware,
54
+ exclude_paths=["/health", "/metrics"]
55
+ )
56
+
57
+ @app.get("/")
58
+ async def root():
59
+ return {"message": "Hello World"}
60
+ ```
61
+
62
+ ## โš™๏ธ Configuration
63
+
64
+ ### Environment Variables
65
+
66
+ ```bash
67
+ # Loki server (full URL, e.g. http://127.0.0.1:3100/loki/api/v1/push)
68
+ LOKI_URL=http://127.0.0.1:3100/loki/api/v1/push
69
+
70
+ # Application metadata (used as static tags)
71
+ LOKI_APPLICATION=my-app
72
+ LOKI_ENVIRONMENT=production
73
+ LOKI_SERVICE=backend
74
+
75
+ # Optional request host override used by the middleware
76
+ REQUEST_HOST=api.example.com
77
+
78
+ # Optional: comma-separated sensitive fields to redact
79
+ LOKI_SENSITIVE_FIELDS=password,token,authorization
80
+ LOKI_MASK_VALUE=********
81
+ ```
82
+
83
+ ### Configuration Notes
84
+
85
+ The repository does not currently expose a `LokiConfig` class. The middleware accepts the following runtime option when added to FastAPI:
86
+
87
+ - `exclude_paths` (list): paths to ignore (default includes `/health`, `/metrics`, `/docs`)
88
+
89
+ Application / Loki connection metadata is read from environment variables (see the section above). A `LokiConfig` helper/constructor may be added in a future release.
90
+
91
+ ## ๐Ÿ“Š Logged Data
92
+
93
+ ### Request Information
94
+ ```json
95
+ {
96
+ "request_id": "550e8400-e29b-41d4-a716-446655440000",
97
+ "request_method": "POST",
98
+ "request_path": "/api/users?page=1",
99
+ "request_ip": "192.168.1.100",
100
+ "request_host": "api.example.com",
101
+ "request_origin": "https://app.example.com",
102
+ "request_user_agent": "Mozilla/5.0...",
103
+ "request_body": { "name": "John", "email": "john@example.com" },
104
+ "request_location": "Paris, รŽle-de-France, France",
105
+ "request_location_latlng": [48.8566, 2.3522]
106
+ }
107
+ ```
108
+
109
+ ### Response Information
110
+ ```json
111
+ {
112
+ "response_status": "successful",
113
+ "response_status_code": 201,
114
+ "response_time": "0.1234s",
115
+ "response_type": "JSONResponse",
116
+ "response_content_type": "application/json",
117
+ "response_body": { "id": 123, "name": "John" }
118
+ }
119
+ ```
120
+
121
+ ### Distributed Tracing
122
+
123
+ 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.
124
+
125
+ ## ๐Ÿ” Query Examples in Grafana
126
+
127
+ ### All errors in production
128
+ ```logql
129
+ {service="backend", environment="production"} | json | severity="error"
130
+ ```
131
+
132
+ ### Slow requests (>1s)
133
+ ```logql
134
+ {application="my-api"} | json | response_time > 1s
135
+ ```
136
+
137
+ ### Failed API calls
138
+ ```logql
139
+ {service="backend"} | json | response_status_code >= 400
140
+ ```
141
+
142
+ ### By specific user location
143
+ ```logql
144
+ {application="my-api"} | json | request_location=~"Paris.*"
145
+ ```
146
+
147
+ ## ๐Ÿ“ˆ Performance Considerations
148
+
149
+ - **Geolocation caching**: Consider implementing Redis caching for IP lookups
150
+ - **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)
151
+ - **Streaming responses**: Handled by consuming and rebuilding the iterator; review memory implications for very large streams
152
+ - **Client disconnections**: Gracefully handled without errors
153
+
154
+ ## ๐Ÿ›  Advanced Usage
155
+
156
+ ### Custom Exclude Paths
157
+
158
+ ```python
159
+ config = LokiConfig(
160
+ exclude_paths=[
161
+ "/health",
162
+ "/metrics",
163
+ "/static",
164
+ "/admin"
165
+ ]
166
+ )
167
+ ```
168
+
169
+ ### OpenTelemetry Integration
170
+
171
+ 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.
172
+
173
+ ## ๐Ÿ“‹ Planned Features
174
+
175
+ - [ ] Django middleware
176
+ - [ ] FastAPI dependency injection helper
177
+ - [ ] Response time percentile tracking
178
+ - [ ] `LokiConfig` helper/constructor and public API for runtime configuration
179
+ - [ ] Improved OpenTelemetry automatic correlation
180
+
181
+ ## ๐Ÿงช Testing
182
+
183
+ ```bash
184
+ pytest tests/
185
+ pytest tests/test_fastapi.py -v
186
+ pytest tests/test_django.py -v
187
+ ```
188
+
189
+ ## ๐Ÿค Contributing
190
+
191
+ Contributions welcome! Please:
192
+ 1. Fork the repository
193
+ 2. Create a feature branch
194
+ 3. Add tests
195
+ 4. Submit a pull request
196
+
197
+ ## ๐Ÿ“„ License
198
+
199
+ MIT License - see [LICENSE](LICENSE) file for details
200
+
201
+ ## ๐Ÿ™ Acknowledgments
202
+
203
+ - [Loki](https://grafana.com/oss/loki/) - Log aggregation system
204
+ - [FastAPI](https://fastapi.tiangolo.com/) - Web framework
205
+ - [OpenTelemetry](https://opentelemetry.io/) - Distributed tracing
206
+
207
+ ## ๐Ÿ“ž Support
208
+
209
+ - **Issues**: [GitHub Issues](https://github.com/IlemLembo/loki-middleware/issues)
210
+ - **Email**: lemboilem@gmail.com
211
+ - **Docs**: [Full Documentation](https://github.com/IlemLembo/loki-middleware/wiki)
212
+
213
+ ---
214
+
215
+ **Made with โค๏ธ for better observability**
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
+ ![Loki Middleware](assets/image.png)
37
+ [![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
38
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
39
+ [![PyPI version](https://img.shields.io/pypi/v/loki-middleware.svg)](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,9 @@
1
+ LICENCE.md
2
+ README.md
3
+ setup.py
4
+ loki_middleware/__init__.py
5
+ loki_middleware.egg-info/PKG-INFO
6
+ loki_middleware.egg-info/SOURCES.txt
7
+ loki_middleware.egg-info/dependency_links.txt
8
+ loki_middleware.egg-info/requires.txt
9
+ loki_middleware.egg-info/top_level.txt
@@ -0,0 +1,5 @@
1
+ fastapi>=0.136.0
2
+ python-logging-loki>=0.3.1
3
+ geocoder>=1.38.1
4
+ dict_field_redacter>=0.1.3
5
+ colorama>=0.4.6
@@ -0,0 +1 @@
1
+ loki_middleware
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,33 @@
1
+ from setuptools import setup, find_packages
2
+ with open("README.md", "r", encoding="utf-8") as fh:
3
+ long_description = fh.read()
4
+
5
+
6
+ setup(
7
+ name="loki-middleware",
8
+ version="0.1.0",
9
+ description="Structured logging middleware for FastAPI with Loki integration",
10
+ long_description=long_description,
11
+ long_description_content_type="text/markdown",
12
+ author="LEMBO Ilem Nelson",
13
+ author_email="lemboilem@gmail.com",
14
+ url="https://github.com/IlemLembo/loki-middleware",
15
+ packages=find_packages(),
16
+ classifiers=[
17
+ "Programming Language :: Python :: 3",
18
+ "License :: OSI Approved :: MIT License",
19
+ "Operating System :: OS Independent",
20
+ 'Development Status :: 5 - Production/Stable',
21
+ 'Intended Audience :: Developers',
22
+ 'Intended Audience :: System Administrators',
23
+ 'Intended Audience :: Information Technology',
24
+ ],
25
+ python_requires=">=3.6",
26
+ install_requires=[
27
+ "fastapi>=0.136.0",
28
+ "python-logging-loki>=0.3.1",
29
+ "geocoder>=1.38.1",
30
+ "dict_field_redacter>=0.1.3",
31
+ "colorama>=0.4.6"
32
+ ]
33
+ )