microbootstrap 0.2.2__tar.gz → 0.2.5__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.
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/PKG-INFO +98 -105
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/README.md +97 -104
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/bootstrappers/litestar.py +10 -17
- microbootstrap-0.2.5/microbootstrap/config/litestar.py +14 -0
- microbootstrap-0.2.5/microbootstrap/py.typed +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/pyproject.toml +1 -1
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/__init__.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/bootstrappers/__init__.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/bootstrappers/base.py +0 -0
- {microbootstrap-0.2.2/microbootstrap/instruments → microbootstrap-0.2.5/microbootstrap/config}/__init__.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/console_writer.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/exceptions.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/granian_server.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/helpers.py +0 -0
- {microbootstrap-0.2.2/microbootstrap/middlewares → microbootstrap-0.2.5/microbootstrap/instruments}/__init__.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/base.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/cors_instrument.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/instrument_box.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/logging_instrument.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/opentelemetry_instrument.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/prometheus_instrument.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/sentry_instrument.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/swagger_instrument.py +0 -0
- /microbootstrap-0.2.2/microbootstrap/py.typed → /microbootstrap-0.2.5/microbootstrap/middlewares/__init__.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/middlewares/fastapi.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/middlewares/litestar.py +0 -0
- {microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/settings.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: microbootstrap
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.5
|
|
4
4
|
Summary: Package for bootstrapping new micro-services
|
|
5
5
|
Keywords: python,microservice,bootstrap,opentelemetry,logging,error-tracing,litestar,fastapi
|
|
6
6
|
Author: community-of-python
|
|
@@ -56,7 +56,7 @@ Description-Content-Type: text/markdown
|
|
|
56
56
|
<a href="https://pypistats.org/packages/microbootstrap" target="_blank"><img src="https://img.shields.io/pypi/dm/microbootstrap"></a>
|
|
57
57
|
</p>
|
|
58
58
|
|
|
59
|
-
<b>microbootstrap</b>
|
|
59
|
+
<b>microbootstrap</b> assists you in creating applications with all the necessary instruments already set up.
|
|
60
60
|
|
|
61
61
|
```python
|
|
62
62
|
# settings.py
|
|
@@ -64,7 +64,7 @@ from microbootstrap import LitestarSettings
|
|
|
64
64
|
|
|
65
65
|
|
|
66
66
|
class YourSettings(LitestarSettings):
|
|
67
|
-
# Your settings stored here
|
|
67
|
+
# Your settings are stored here
|
|
68
68
|
|
|
69
69
|
|
|
70
70
|
settings = YourSettings()
|
|
@@ -76,23 +76,23 @@ from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
|
76
76
|
|
|
77
77
|
from your_application.settings import settings
|
|
78
78
|
|
|
79
|
-
# Litestar application
|
|
79
|
+
# Use the Litestar application!
|
|
80
80
|
application: litestar.Litestar = LitestarBootstrapper(settings).bootstrap()
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
With <b>microbootstrap</b>, you
|
|
83
|
+
Currently, only `litestar` is supported.
|
|
84
|
+
With <b>microbootstrap</b>, you receive an application with lightweight built-in support for:
|
|
85
85
|
|
|
86
86
|
- `sentry`
|
|
87
87
|
- `prometheus`
|
|
88
88
|
- `opentelemetry`
|
|
89
89
|
- `logging`
|
|
90
90
|
- `cors`
|
|
91
|
-
- `swagger` - additional offline version support
|
|
91
|
+
- `swagger` - with additional offline version support
|
|
92
92
|
|
|
93
|
-
Interested? Let's
|
|
93
|
+
Interested? Let's dive right in ⚡
|
|
94
94
|
|
|
95
|
-
## Table of
|
|
95
|
+
## Table of Contents
|
|
96
96
|
|
|
97
97
|
- [Installation](#installation)
|
|
98
98
|
- [Quickstart](#quickstart)
|
|
@@ -103,7 +103,7 @@ Interested? Let's jump right into it ⚡
|
|
|
103
103
|
- [Prometheus](#prometheus)
|
|
104
104
|
- [Opentelemetry](#opentelemetry)
|
|
105
105
|
- [Logging](#logging)
|
|
106
|
-
- [
|
|
106
|
+
- [CORS](#cors)
|
|
107
107
|
- [Swagger](#swagger)
|
|
108
108
|
- [Configuration](#configuration)
|
|
109
109
|
- [Instruments configuration](#instruments-configuration)
|
|
@@ -112,15 +112,15 @@ Interested? Let's jump right into it ⚡
|
|
|
112
112
|
|
|
113
113
|
## Installation
|
|
114
114
|
|
|
115
|
-
You can install package
|
|
115
|
+
You can install the package using either `pip` or `poetry`.
|
|
116
116
|
|
|
117
|
-
poetry:
|
|
117
|
+
For poetry:
|
|
118
118
|
|
|
119
119
|
```bash
|
|
120
120
|
$ poetry add microbootstrap -E litestar
|
|
121
121
|
```
|
|
122
122
|
|
|
123
|
-
pip:
|
|
123
|
+
For pip:
|
|
124
124
|
|
|
125
125
|
```bash
|
|
126
126
|
$ pip install microbootstrap[litestar]
|
|
@@ -128,7 +128,7 @@ $ pip install microbootstrap[litestar]
|
|
|
128
128
|
|
|
129
129
|
## Quickstart
|
|
130
130
|
|
|
131
|
-
To
|
|
131
|
+
To configure your application, you can use the settings object.
|
|
132
132
|
|
|
133
133
|
```python
|
|
134
134
|
from microbootstrap import LitestarSettings
|
|
@@ -140,10 +140,10 @@ class YourSettings(LitestarSettings):
|
|
|
140
140
|
service_name: str = "my-awesome-service"
|
|
141
141
|
|
|
142
142
|
# Sentry settings
|
|
143
|
-
sentry_dsn: str = "your-
|
|
143
|
+
sentry_dsn: str = "your-sentry-dsn"
|
|
144
144
|
|
|
145
145
|
# Prometheus settings
|
|
146
|
-
prometheus_metrics_path: str "/my-path"
|
|
146
|
+
prometheus_metrics_path: str = "/my-path"
|
|
147
147
|
|
|
148
148
|
# Opentelemetry settings
|
|
149
149
|
opentelemetry_container_name: str = "your-container"
|
|
@@ -154,7 +154,7 @@ class YourSettings(LitestarSettings):
|
|
|
154
154
|
settings = YourSettings()
|
|
155
155
|
```
|
|
156
156
|
|
|
157
|
-
|
|
157
|
+
Next, use the `Bootstrapper` object to create an application based on your settings.
|
|
158
158
|
|
|
159
159
|
```python
|
|
160
160
|
import litestar
|
|
@@ -163,17 +163,17 @@ from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
|
163
163
|
application: litestar.Litestar = LitestarBootstrapper(settings).bootstrap()
|
|
164
164
|
```
|
|
165
165
|
|
|
166
|
-
This
|
|
166
|
+
This approach will provide you with an application that has all the essential instruments already set up for you.
|
|
167
167
|
|
|
168
168
|
## Settings
|
|
169
169
|
|
|
170
|
-
The settings object is
|
|
170
|
+
The settings object is the core of microbootstrap.
|
|
171
171
|
|
|
172
|
-
All framework-related settings inherit from the `BaseBootstrapSettings` object. `BaseBootstrapSettings` defines parameters for the service and
|
|
172
|
+
All framework-related settings inherit from the `BaseBootstrapSettings` object. `BaseBootstrapSettings` defines parameters for the service and various instruments.
|
|
173
173
|
|
|
174
|
-
However, the number of parameters is <b>not
|
|
174
|
+
However, the number of parameters is <b>not confined</b> to those defined in `BaseBootstrapSettings`. You can add as many as you need.
|
|
175
175
|
|
|
176
|
-
These parameters can be
|
|
176
|
+
These parameters can be sourced from your environment. By default, no prefix is added to these parameters.
|
|
177
177
|
|
|
178
178
|
Example:
|
|
179
179
|
|
|
@@ -187,9 +187,9 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
187
187
|
... # Other settings here
|
|
188
188
|
```
|
|
189
189
|
|
|
190
|
-
To
|
|
190
|
+
To source `your_awesome_parameter` from the environment, set the environment variable named `YOUR_AWESOME_PARAMETER`.
|
|
191
191
|
|
|
192
|
-
If you
|
|
192
|
+
If you prefer to use a prefix when sourcing parameters, set the `ENVIRONMENT_PREFIX` environment variable in advance.
|
|
193
193
|
|
|
194
194
|
Example:
|
|
195
195
|
|
|
@@ -197,13 +197,13 @@ Example:
|
|
|
197
197
|
$ export ENVIRONMENT_PREFIX=YOUR_PREFIX_
|
|
198
198
|
```
|
|
199
199
|
|
|
200
|
-
Then the settings object will
|
|
200
|
+
Then the settings object will attempt to source the variable named `YOUR_PREFIX_YOUR_AWESOME_PARAMETER`.
|
|
201
201
|
|
|
202
202
|
## Service settings
|
|
203
203
|
|
|
204
|
-
|
|
204
|
+
Each settings object for every framework includes service parameters that can be utilized by various instruments.
|
|
205
205
|
|
|
206
|
-
You can
|
|
206
|
+
You can configure them manually, or set the corresponding environment variables and let <b>microbootstrap</b> to source them automatically.
|
|
207
207
|
|
|
208
208
|
```python
|
|
209
209
|
from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
@@ -222,7 +222,7 @@ class ServiceSettings(BaseBootstrapSettings):
|
|
|
222
222
|
|
|
223
223
|
## Instruments
|
|
224
224
|
|
|
225
|
-
|
|
225
|
+
At present, the following instruments are supported for bootstrapping:
|
|
226
226
|
|
|
227
227
|
- `sentry`
|
|
228
228
|
- `prometheus`
|
|
@@ -231,12 +231,12 @@ Currently, these instruments are already supported for bootstrapping:
|
|
|
231
231
|
- `cors`
|
|
232
232
|
- `swagger`
|
|
233
233
|
|
|
234
|
-
Let's
|
|
234
|
+
Let's clarify the process required to bootstrap these instruments.
|
|
235
235
|
|
|
236
236
|
### Sentry
|
|
237
237
|
|
|
238
|
-
To bootstrap Sentry, you
|
|
239
|
-
|
|
238
|
+
To bootstrap Sentry, you must provide at least the `sentry_dsn`.
|
|
239
|
+
Additional parameters can also be supplied through the settings object.
|
|
240
240
|
|
|
241
241
|
```python
|
|
242
242
|
from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
@@ -256,12 +256,12 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
256
256
|
... # Other settings here
|
|
257
257
|
```
|
|
258
258
|
|
|
259
|
-
|
|
259
|
+
These settings are subsequently passed to the [sentry-sdk](https://pypi.org/project/sentry-sdk/) package, finalizing your Sentry integration.
|
|
260
260
|
|
|
261
261
|
### Prometheus
|
|
262
262
|
|
|
263
|
-
To bootstrap Prometheus, you
|
|
264
|
-
|
|
263
|
+
To bootstrap Prometheus, you must provide at least the `prometheus_metrics_path`.
|
|
264
|
+
Additional parameters can also be supplied through the settings object.
|
|
265
265
|
|
|
266
266
|
```python
|
|
267
267
|
from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
@@ -276,14 +276,14 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
276
276
|
... # Other settings here
|
|
277
277
|
```
|
|
278
278
|
|
|
279
|
-
These settings
|
|
280
|
-
|
|
279
|
+
These settings are subsequently passed to the [prometheus-client](https://pypi.org/project/prometheus-client/) package.
|
|
280
|
+
The underlying top-level Prometheus library may vary from framework to framework, but in general, a metrics handler will be available at the provided path.
|
|
281
281
|
|
|
282
|
-
By default, metrics are
|
|
282
|
+
By default, metrics are accessible at the `/metrics` path.
|
|
283
283
|
|
|
284
284
|
### Opentelemetry
|
|
285
285
|
|
|
286
|
-
Opentelemetry
|
|
286
|
+
To bootstrap Opentelemetry, you must provide several parameters:
|
|
287
287
|
|
|
288
288
|
- `service_name`
|
|
289
289
|
- `service_version`
|
|
@@ -291,7 +291,7 @@ Opentelemetry requires a lot of parameters to be bootstrapped:
|
|
|
291
291
|
- `opentelemetry_namespace`
|
|
292
292
|
- `opentelemetry_container_name`.
|
|
293
293
|
|
|
294
|
-
|
|
294
|
+
However, additional parameters can also be supplied if needed.
|
|
295
295
|
|
|
296
296
|
```python
|
|
297
297
|
from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
@@ -312,14 +312,14 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
312
312
|
... # Other settings here
|
|
313
313
|
```
|
|
314
314
|
|
|
315
|
-
|
|
315
|
+
These settings are subsequently passed to [opentelemetry](https://opentelemetry.io/), finalizing your Opentelemetry integration.
|
|
316
316
|
|
|
317
317
|
### Logging
|
|
318
318
|
|
|
319
|
-
<b>microbootstrap</b> provides in-memory
|
|
320
|
-
|
|
319
|
+
<b>microbootstrap</b> provides in-memory JSON logging through the use of [structlog](https://pypi.org/project/structlog/).
|
|
320
|
+
For more information on in-memory logging, refer to [MemoryHandler](https://docs.python.org/3/library/logging.handlers.html#memoryhandler).
|
|
321
321
|
|
|
322
|
-
To
|
|
322
|
+
To utilize this feature, your application must be in non-debug mode, meaning `service_debug` should be set to `False`.
|
|
323
323
|
|
|
324
324
|
```python
|
|
325
325
|
import logging
|
|
@@ -328,7 +328,7 @@ from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
|
328
328
|
|
|
329
329
|
|
|
330
330
|
class YourSettings(BaseBootstrapSettings):
|
|
331
|
-
service_debug: bool =
|
|
331
|
+
service_debug: bool = False
|
|
332
332
|
|
|
333
333
|
logging_log_level: int = logging.INFO
|
|
334
334
|
logging_flush_level: int = logging.ERROR
|
|
@@ -338,16 +338,16 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
338
338
|
logging_exclude_endpoints: list[str] = []
|
|
339
339
|
```
|
|
340
340
|
|
|
341
|
-
|
|
341
|
+
Parameter descriptions:
|
|
342
342
|
|
|
343
|
-
- `logging_log_level` - default log level.
|
|
344
|
-
- `logging_flush_level` -
|
|
345
|
-
- `logging_buffer_capacity` -
|
|
346
|
-
- `logging_unset_handlers` -
|
|
347
|
-
- `logging_extra_processors` -
|
|
348
|
-
- `logging_exclude_endpoints` -
|
|
343
|
+
- `logging_log_level` - The default log level.
|
|
344
|
+
- `logging_flush_level` - All messages will be flushed from the buffer when a log with this level appears.
|
|
345
|
+
- `logging_buffer_capacity` - The number of messages your buffer will store before being flushed.
|
|
346
|
+
- `logging_unset_handlers` - Unset logger handlers.
|
|
347
|
+
- `logging_extra_processors` - Set additional structlog processors if needed.
|
|
348
|
+
- `logging_exclude_endpoints` - Exclude logging on specific endpoints.
|
|
349
349
|
|
|
350
|
-
###
|
|
350
|
+
### CORS
|
|
351
351
|
|
|
352
352
|
```python
|
|
353
353
|
from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
@@ -363,15 +363,15 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
363
363
|
cors_max_age: int = 600
|
|
364
364
|
```
|
|
365
365
|
|
|
366
|
-
|
|
366
|
+
Parameter descriptions:
|
|
367
367
|
|
|
368
|
-
- `cors_allowed_origins` - list of origins that are
|
|
369
|
-
- `cors_allowed_methods` - list of
|
|
370
|
-
- `cors_allowed_headers` - list of
|
|
371
|
-
- `cors_exposed_headers` - list of headers that are exposed via the 'Access-Control-Expose-Headers' header.
|
|
372
|
-
- `cors_allowed_credentials` - boolean
|
|
373
|
-
- `cors_allowed_origin_regex` - regex to match origins
|
|
374
|
-
- `cors_max_age` - response caching TTL in seconds, defaults to 600.
|
|
368
|
+
- `cors_allowed_origins` - A list of origins that are permitted.
|
|
369
|
+
- `cors_allowed_methods` - A list of HTTP methods that are allowed.
|
|
370
|
+
- `cors_allowed_headers` - A list of headers that are permitted.
|
|
371
|
+
- `cors_exposed_headers` - A list of headers that are exposed via the 'Access-Control-Expose-Headers' header.
|
|
372
|
+
- `cors_allowed_credentials` - A boolean value that dictates whether or not to set the 'Access-Control-Allow-Credentials' header.
|
|
373
|
+
- `cors_allowed_origin_regex` - A regex used to match against origins.
|
|
374
|
+
- `cors_max_age` - The response caching Time-To-Live (TTL) in seconds, defaults to 600.
|
|
375
375
|
|
|
376
376
|
### Swagger
|
|
377
377
|
|
|
@@ -390,25 +390,25 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
390
390
|
swagger_extra_params: dict[str, Any] = {}
|
|
391
391
|
```
|
|
392
392
|
|
|
393
|
-
|
|
393
|
+
Parameter descriptions:
|
|
394
394
|
|
|
395
|
-
- `
|
|
396
|
-
- `
|
|
397
|
-
- `
|
|
398
|
-
- `service_static_path` -
|
|
399
|
-
- `swagger_path` - path
|
|
400
|
-
- `swagger_offline_docs` -
|
|
401
|
-
- `swagger_extra_params` -
|
|
395
|
+
- `service_name` - The name of the service, which will be displayed in the documentation.
|
|
396
|
+
- `service_description` - A brief description of the service, which will also be displayed in the documentation.
|
|
397
|
+
- `service_version` - The current version of the service.
|
|
398
|
+
- `service_static_path` - The path for static files in the service.
|
|
399
|
+
- `swagger_path` - The path where the documentation can be found.
|
|
400
|
+
- `swagger_offline_docs` - A boolean value that, when set to True, allows the Swagger JS bundles to be accessed offline. This is because the service starts to host via static.
|
|
401
|
+
- `swagger_extra_params` - Additional parameters to pass into the OpenAPI configuration.
|
|
402
402
|
|
|
403
403
|
## Configuration
|
|
404
404
|
|
|
405
|
-
|
|
405
|
+
While settings provide a convenient mechanism, it's not always feasible to store everything within them.
|
|
406
406
|
|
|
407
|
-
|
|
407
|
+
There may be cases where you need to configure a tool directly. Here's how it can be done.
|
|
408
408
|
|
|
409
409
|
### Instruments configuration
|
|
410
410
|
|
|
411
|
-
To configure
|
|
411
|
+
To manually configure an instrument, you need to import one of the available configurations from <b>microbootstrap</b>:
|
|
412
412
|
|
|
413
413
|
- `SentryConfig`
|
|
414
414
|
- `OpentelemetryConfig`
|
|
@@ -417,7 +417,7 @@ To configure instruemt manually, you have to import one of available configs fro
|
|
|
417
417
|
- `SwaggerConfig`
|
|
418
418
|
- `CorsConfig`
|
|
419
419
|
|
|
420
|
-
|
|
420
|
+
These configurations can then be passed into the `.configure_instrument` or `.configure_instruments` bootstrapper methods.
|
|
421
421
|
|
|
422
422
|
```python
|
|
423
423
|
import litestar
|
|
@@ -429,12 +429,12 @@ from microbootstrap import SentryConfig, OpentelemetryConfig
|
|
|
429
429
|
application: litestar.Litestar = (
|
|
430
430
|
LitestarBootstrapper(settings)
|
|
431
431
|
.configure_instrument(SentryConfig(sentry_dsn="https://new-dsn"))
|
|
432
|
-
.configure_instrument(OpentelemetryConfig(
|
|
432
|
+
.configure_instrument(OpentelemetryConfig(opentelemetry_endpoint="/new-endpoint"))
|
|
433
433
|
.bootstrap()
|
|
434
434
|
)
|
|
435
435
|
```
|
|
436
436
|
|
|
437
|
-
|
|
437
|
+
Alternatively,
|
|
438
438
|
|
|
439
439
|
```python
|
|
440
440
|
import litestar
|
|
@@ -455,7 +455,7 @@ application: litestar.Litestar = (
|
|
|
455
455
|
|
|
456
456
|
### Application configuration
|
|
457
457
|
|
|
458
|
-
|
|
458
|
+
The application can be configured in a similar manner:
|
|
459
459
|
|
|
460
460
|
```python
|
|
461
461
|
import litestar
|
|
@@ -471,17 +471,16 @@ async def my_handler() -> str:
|
|
|
471
471
|
|
|
472
472
|
application: litestar.Litestar = (
|
|
473
473
|
LitestarBootstrapper(settings)
|
|
474
|
-
.
|
|
474
|
+
.configure_application(AppConfig(route_handlers=[my_handler]))
|
|
475
475
|
.bootstrap()
|
|
476
476
|
)
|
|
477
477
|
```
|
|
478
478
|
|
|
479
479
|
> ### Important
|
|
480
480
|
>
|
|
481
|
-
> When configuring parameters with simple data types such as: `str`, `int`, `float`,
|
|
482
|
-
> Those variables are rewriting previous values.
|
|
481
|
+
> When configuring parameters with simple data types such as: `str`, `int`, `float`, etc., these variables overwrite previous values.
|
|
483
482
|
>
|
|
484
|
-
> Example
|
|
483
|
+
> Example:
|
|
485
484
|
>
|
|
486
485
|
> ```python
|
|
487
486
|
> from microbootstrap import LitestarSettings, SentryConfig
|
|
@@ -500,13 +499,11 @@ application: litestar.Litestar = (
|
|
|
500
499
|
> )
|
|
501
500
|
> ```
|
|
502
501
|
>
|
|
503
|
-
> In this example application will be bootstrapped with new `https://my-new-configured-sentry-dsn`
|
|
504
|
-
> instead of old one.
|
|
502
|
+
> In this example, the application will be bootstrapped with the new `https://my-new-configured-sentry-dsn` Sentry DSN, replacing the old one.
|
|
505
503
|
>
|
|
506
|
-
>
|
|
507
|
-
> They are being expanded or merged into each other.
|
|
504
|
+
> However, when you configure parameters with complex data types such as: `list`, `tuple`, `dict`, or `set`, they are expanded or merged.
|
|
508
505
|
>
|
|
509
|
-
> Example
|
|
506
|
+
> Example:
|
|
510
507
|
>
|
|
511
508
|
> ```python
|
|
512
509
|
> from microbootstrap import LitestarSettings, PrometheusConfig
|
|
@@ -525,14 +522,12 @@ application: litestar.Litestar = (
|
|
|
525
522
|
> )
|
|
526
523
|
> ```
|
|
527
524
|
>
|
|
528
|
-
> In this case
|
|
529
|
-
> This is also true for `list`, `tuple` and `set`
|
|
525
|
+
> In this case, Prometheus will receive `{"first_value": 1, "second_value": 2}` inside `prometheus_additional_params`. This is also true for `list`, `tuple`, and `set`.
|
|
530
526
|
|
|
531
527
|
## Advanced
|
|
532
528
|
|
|
533
|
-
If you
|
|
534
|
-
|
|
535
|
-
Every instrument uses some config, so that's first thing, you have to define.
|
|
529
|
+
If you need a specific instrument, you can create your own.
|
|
530
|
+
Essentially, an `Instrument` is just a class with some abstract methods. Each instrument uses a certain configuration, which is the first thing you need to define.
|
|
536
531
|
|
|
537
532
|
```python
|
|
538
533
|
from microbootstrap.instruments.base import BaseInstrumentConfig
|
|
@@ -543,7 +538,7 @@ class MyInstrumentConfig(BaseInstrumentConfig):
|
|
|
543
538
|
your_list_parameter: list
|
|
544
539
|
```
|
|
545
540
|
|
|
546
|
-
|
|
541
|
+
Next, you can create an instrument class that inherits from `Instrument` and accepts your configuration as a generic parameter.
|
|
547
542
|
|
|
548
543
|
```python
|
|
549
544
|
from microbootstrap.instruments.base import Instrument
|
|
@@ -567,21 +562,20 @@ class MyInstrument(Instrument[MyInstrumentConfig]):
|
|
|
567
562
|
return MyInstrumentConfig
|
|
568
563
|
```
|
|
569
564
|
|
|
570
|
-
|
|
565
|
+
Now, you can define the behavior of your instrument.
|
|
571
566
|
|
|
572
567
|
Attributes:
|
|
573
568
|
|
|
574
|
-
- `instrument_name` -
|
|
575
|
-
- `ready_condition` -
|
|
569
|
+
- `instrument_name` - This will be displayed in your console during bootstrap.
|
|
570
|
+
- `ready_condition` - This will be displayed in your console during bootstrap if the instrument is not ready.
|
|
576
571
|
|
|
577
572
|
Methods:
|
|
578
573
|
|
|
579
|
-
- `is_ready` - defines
|
|
580
|
-
- `teardown` - graceful shutdown
|
|
581
|
-
- `bootstrap` - main instrument
|
|
574
|
+
- `is_ready` - This defines the readiness of the instrument for bootstrapping, based on its configuration values. This is required.
|
|
575
|
+
- `teardown` - This allows for a graceful shutdown of the instrument during application shutdown. This is not required.
|
|
576
|
+
- `bootstrap` - This is the main logic of the instrument. This is not required.
|
|
582
577
|
|
|
583
|
-
|
|
584
|
-
Let's adapt it for litestar for example
|
|
578
|
+
Once you have the framework of the instrument, you can adapt it for any existing framework. For instance, let's adapt it for litestar.
|
|
585
579
|
|
|
586
580
|
```python
|
|
587
581
|
import litestar
|
|
@@ -595,17 +589,16 @@ class LitestarMyInstrument(MyInstrument):
|
|
|
595
589
|
|
|
596
590
|
def bootstrap_after(self, application: litestar.Litestar) -> dict[str, typing.Any]:
|
|
597
591
|
pass
|
|
598
|
-
|
|
599
592
|
```
|
|
600
593
|
|
|
601
|
-
To bind instrument to a bootstrapper,
|
|
594
|
+
To bind the instrument to a bootstrapper, use the `.use_instrument` decorator.
|
|
602
595
|
|
|
603
|
-
To add
|
|
596
|
+
To add extra parameters to the application, you can use:
|
|
604
597
|
|
|
605
|
-
- `bootstrap_before` -
|
|
606
|
-
- `bootstrap_after` -
|
|
598
|
+
- `bootstrap_before` - This adds arguments to the application configuration before creation.
|
|
599
|
+
- `bootstrap_after` - This adds arguments to the application after creation.
|
|
607
600
|
|
|
608
|
-
|
|
601
|
+
Afterwards, you can use your instrument during the bootstrap process.
|
|
609
602
|
|
|
610
603
|
```python
|
|
611
604
|
import litestar
|
|
@@ -628,7 +621,7 @@ application: litestar.Litestar = (
|
|
|
628
621
|
)
|
|
629
622
|
```
|
|
630
623
|
|
|
631
|
-
|
|
624
|
+
Alternatively, you can fill these parameters within your main settings object.
|
|
632
625
|
|
|
633
626
|
```python
|
|
634
627
|
from microbootstrap import LitestarSettings
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
<a href="https://pypistats.org/packages/microbootstrap" target="_blank"><img src="https://img.shields.io/pypi/dm/microbootstrap"></a>
|
|
10
10
|
</p>
|
|
11
11
|
|
|
12
|
-
<b>microbootstrap</b>
|
|
12
|
+
<b>microbootstrap</b> assists you in creating applications with all the necessary instruments already set up.
|
|
13
13
|
|
|
14
14
|
```python
|
|
15
15
|
# settings.py
|
|
@@ -17,7 +17,7 @@ from microbootstrap import LitestarSettings
|
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
class YourSettings(LitestarSettings):
|
|
20
|
-
# Your settings stored here
|
|
20
|
+
# Your settings are stored here
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
settings = YourSettings()
|
|
@@ -29,23 +29,23 @@ from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
|
29
29
|
|
|
30
30
|
from your_application.settings import settings
|
|
31
31
|
|
|
32
|
-
# Litestar application
|
|
32
|
+
# Use the Litestar application!
|
|
33
33
|
application: litestar.Litestar = LitestarBootstrapper(settings).bootstrap()
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
With <b>microbootstrap</b>, you
|
|
36
|
+
Currently, only `litestar` is supported.
|
|
37
|
+
With <b>microbootstrap</b>, you receive an application with lightweight built-in support for:
|
|
38
38
|
|
|
39
39
|
- `sentry`
|
|
40
40
|
- `prometheus`
|
|
41
41
|
- `opentelemetry`
|
|
42
42
|
- `logging`
|
|
43
43
|
- `cors`
|
|
44
|
-
- `swagger` - additional offline version support
|
|
44
|
+
- `swagger` - with additional offline version support
|
|
45
45
|
|
|
46
|
-
Interested? Let's
|
|
46
|
+
Interested? Let's dive right in ⚡
|
|
47
47
|
|
|
48
|
-
## Table of
|
|
48
|
+
## Table of Contents
|
|
49
49
|
|
|
50
50
|
- [Installation](#installation)
|
|
51
51
|
- [Quickstart](#quickstart)
|
|
@@ -56,7 +56,7 @@ Interested? Let's jump right into it ⚡
|
|
|
56
56
|
- [Prometheus](#prometheus)
|
|
57
57
|
- [Opentelemetry](#opentelemetry)
|
|
58
58
|
- [Logging](#logging)
|
|
59
|
-
- [
|
|
59
|
+
- [CORS](#cors)
|
|
60
60
|
- [Swagger](#swagger)
|
|
61
61
|
- [Configuration](#configuration)
|
|
62
62
|
- [Instruments configuration](#instruments-configuration)
|
|
@@ -65,15 +65,15 @@ Interested? Let's jump right into it ⚡
|
|
|
65
65
|
|
|
66
66
|
## Installation
|
|
67
67
|
|
|
68
|
-
You can install package
|
|
68
|
+
You can install the package using either `pip` or `poetry`.
|
|
69
69
|
|
|
70
|
-
poetry:
|
|
70
|
+
For poetry:
|
|
71
71
|
|
|
72
72
|
```bash
|
|
73
73
|
$ poetry add microbootstrap -E litestar
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
pip:
|
|
76
|
+
For pip:
|
|
77
77
|
|
|
78
78
|
```bash
|
|
79
79
|
$ pip install microbootstrap[litestar]
|
|
@@ -81,7 +81,7 @@ $ pip install microbootstrap[litestar]
|
|
|
81
81
|
|
|
82
82
|
## Quickstart
|
|
83
83
|
|
|
84
|
-
To
|
|
84
|
+
To configure your application, you can use the settings object.
|
|
85
85
|
|
|
86
86
|
```python
|
|
87
87
|
from microbootstrap import LitestarSettings
|
|
@@ -93,10 +93,10 @@ class YourSettings(LitestarSettings):
|
|
|
93
93
|
service_name: str = "my-awesome-service"
|
|
94
94
|
|
|
95
95
|
# Sentry settings
|
|
96
|
-
sentry_dsn: str = "your-
|
|
96
|
+
sentry_dsn: str = "your-sentry-dsn"
|
|
97
97
|
|
|
98
98
|
# Prometheus settings
|
|
99
|
-
prometheus_metrics_path: str "/my-path"
|
|
99
|
+
prometheus_metrics_path: str = "/my-path"
|
|
100
100
|
|
|
101
101
|
# Opentelemetry settings
|
|
102
102
|
opentelemetry_container_name: str = "your-container"
|
|
@@ -107,7 +107,7 @@ class YourSettings(LitestarSettings):
|
|
|
107
107
|
settings = YourSettings()
|
|
108
108
|
```
|
|
109
109
|
|
|
110
|
-
|
|
110
|
+
Next, use the `Bootstrapper` object to create an application based on your settings.
|
|
111
111
|
|
|
112
112
|
```python
|
|
113
113
|
import litestar
|
|
@@ -116,17 +116,17 @@ from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
|
116
116
|
application: litestar.Litestar = LitestarBootstrapper(settings).bootstrap()
|
|
117
117
|
```
|
|
118
118
|
|
|
119
|
-
This
|
|
119
|
+
This approach will provide you with an application that has all the essential instruments already set up for you.
|
|
120
120
|
|
|
121
121
|
## Settings
|
|
122
122
|
|
|
123
|
-
The settings object is
|
|
123
|
+
The settings object is the core of microbootstrap.
|
|
124
124
|
|
|
125
|
-
All framework-related settings inherit from the `BaseBootstrapSettings` object. `BaseBootstrapSettings` defines parameters for the service and
|
|
125
|
+
All framework-related settings inherit from the `BaseBootstrapSettings` object. `BaseBootstrapSettings` defines parameters for the service and various instruments.
|
|
126
126
|
|
|
127
|
-
However, the number of parameters is <b>not
|
|
127
|
+
However, the number of parameters is <b>not confined</b> to those defined in `BaseBootstrapSettings`. You can add as many as you need.
|
|
128
128
|
|
|
129
|
-
These parameters can be
|
|
129
|
+
These parameters can be sourced from your environment. By default, no prefix is added to these parameters.
|
|
130
130
|
|
|
131
131
|
Example:
|
|
132
132
|
|
|
@@ -140,9 +140,9 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
140
140
|
... # Other settings here
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
-
To
|
|
143
|
+
To source `your_awesome_parameter` from the environment, set the environment variable named `YOUR_AWESOME_PARAMETER`.
|
|
144
144
|
|
|
145
|
-
If you
|
|
145
|
+
If you prefer to use a prefix when sourcing parameters, set the `ENVIRONMENT_PREFIX` environment variable in advance.
|
|
146
146
|
|
|
147
147
|
Example:
|
|
148
148
|
|
|
@@ -150,13 +150,13 @@ Example:
|
|
|
150
150
|
$ export ENVIRONMENT_PREFIX=YOUR_PREFIX_
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
-
Then the settings object will
|
|
153
|
+
Then the settings object will attempt to source the variable named `YOUR_PREFIX_YOUR_AWESOME_PARAMETER`.
|
|
154
154
|
|
|
155
155
|
## Service settings
|
|
156
156
|
|
|
157
|
-
|
|
157
|
+
Each settings object for every framework includes service parameters that can be utilized by various instruments.
|
|
158
158
|
|
|
159
|
-
You can
|
|
159
|
+
You can configure them manually, or set the corresponding environment variables and let <b>microbootstrap</b> to source them automatically.
|
|
160
160
|
|
|
161
161
|
```python
|
|
162
162
|
from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
@@ -175,7 +175,7 @@ class ServiceSettings(BaseBootstrapSettings):
|
|
|
175
175
|
|
|
176
176
|
## Instruments
|
|
177
177
|
|
|
178
|
-
|
|
178
|
+
At present, the following instruments are supported for bootstrapping:
|
|
179
179
|
|
|
180
180
|
- `sentry`
|
|
181
181
|
- `prometheus`
|
|
@@ -184,12 +184,12 @@ Currently, these instruments are already supported for bootstrapping:
|
|
|
184
184
|
- `cors`
|
|
185
185
|
- `swagger`
|
|
186
186
|
|
|
187
|
-
Let's
|
|
187
|
+
Let's clarify the process required to bootstrap these instruments.
|
|
188
188
|
|
|
189
189
|
### Sentry
|
|
190
190
|
|
|
191
|
-
To bootstrap Sentry, you
|
|
192
|
-
|
|
191
|
+
To bootstrap Sentry, you must provide at least the `sentry_dsn`.
|
|
192
|
+
Additional parameters can also be supplied through the settings object.
|
|
193
193
|
|
|
194
194
|
```python
|
|
195
195
|
from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
@@ -209,12 +209,12 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
209
209
|
... # Other settings here
|
|
210
210
|
```
|
|
211
211
|
|
|
212
|
-
|
|
212
|
+
These settings are subsequently passed to the [sentry-sdk](https://pypi.org/project/sentry-sdk/) package, finalizing your Sentry integration.
|
|
213
213
|
|
|
214
214
|
### Prometheus
|
|
215
215
|
|
|
216
|
-
To bootstrap Prometheus, you
|
|
217
|
-
|
|
216
|
+
To bootstrap Prometheus, you must provide at least the `prometheus_metrics_path`.
|
|
217
|
+
Additional parameters can also be supplied through the settings object.
|
|
218
218
|
|
|
219
219
|
```python
|
|
220
220
|
from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
@@ -229,14 +229,14 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
229
229
|
... # Other settings here
|
|
230
230
|
```
|
|
231
231
|
|
|
232
|
-
These settings
|
|
233
|
-
|
|
232
|
+
These settings are subsequently passed to the [prometheus-client](https://pypi.org/project/prometheus-client/) package.
|
|
233
|
+
The underlying top-level Prometheus library may vary from framework to framework, but in general, a metrics handler will be available at the provided path.
|
|
234
234
|
|
|
235
|
-
By default, metrics are
|
|
235
|
+
By default, metrics are accessible at the `/metrics` path.
|
|
236
236
|
|
|
237
237
|
### Opentelemetry
|
|
238
238
|
|
|
239
|
-
Opentelemetry
|
|
239
|
+
To bootstrap Opentelemetry, you must provide several parameters:
|
|
240
240
|
|
|
241
241
|
- `service_name`
|
|
242
242
|
- `service_version`
|
|
@@ -244,7 +244,7 @@ Opentelemetry requires a lot of parameters to be bootstrapped:
|
|
|
244
244
|
- `opentelemetry_namespace`
|
|
245
245
|
- `opentelemetry_container_name`.
|
|
246
246
|
|
|
247
|
-
|
|
247
|
+
However, additional parameters can also be supplied if needed.
|
|
248
248
|
|
|
249
249
|
```python
|
|
250
250
|
from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
@@ -265,14 +265,14 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
265
265
|
... # Other settings here
|
|
266
266
|
```
|
|
267
267
|
|
|
268
|
-
|
|
268
|
+
These settings are subsequently passed to [opentelemetry](https://opentelemetry.io/), finalizing your Opentelemetry integration.
|
|
269
269
|
|
|
270
270
|
### Logging
|
|
271
271
|
|
|
272
|
-
<b>microbootstrap</b> provides in-memory
|
|
273
|
-
|
|
272
|
+
<b>microbootstrap</b> provides in-memory JSON logging through the use of [structlog](https://pypi.org/project/structlog/).
|
|
273
|
+
For more information on in-memory logging, refer to [MemoryHandler](https://docs.python.org/3/library/logging.handlers.html#memoryhandler).
|
|
274
274
|
|
|
275
|
-
To
|
|
275
|
+
To utilize this feature, your application must be in non-debug mode, meaning `service_debug` should be set to `False`.
|
|
276
276
|
|
|
277
277
|
```python
|
|
278
278
|
import logging
|
|
@@ -281,7 +281,7 @@ from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
|
281
281
|
|
|
282
282
|
|
|
283
283
|
class YourSettings(BaseBootstrapSettings):
|
|
284
|
-
service_debug: bool =
|
|
284
|
+
service_debug: bool = False
|
|
285
285
|
|
|
286
286
|
logging_log_level: int = logging.INFO
|
|
287
287
|
logging_flush_level: int = logging.ERROR
|
|
@@ -291,16 +291,16 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
291
291
|
logging_exclude_endpoints: list[str] = []
|
|
292
292
|
```
|
|
293
293
|
|
|
294
|
-
|
|
294
|
+
Parameter descriptions:
|
|
295
295
|
|
|
296
|
-
- `logging_log_level` - default log level.
|
|
297
|
-
- `logging_flush_level` -
|
|
298
|
-
- `logging_buffer_capacity` -
|
|
299
|
-
- `logging_unset_handlers` -
|
|
300
|
-
- `logging_extra_processors` -
|
|
301
|
-
- `logging_exclude_endpoints` -
|
|
296
|
+
- `logging_log_level` - The default log level.
|
|
297
|
+
- `logging_flush_level` - All messages will be flushed from the buffer when a log with this level appears.
|
|
298
|
+
- `logging_buffer_capacity` - The number of messages your buffer will store before being flushed.
|
|
299
|
+
- `logging_unset_handlers` - Unset logger handlers.
|
|
300
|
+
- `logging_extra_processors` - Set additional structlog processors if needed.
|
|
301
|
+
- `logging_exclude_endpoints` - Exclude logging on specific endpoints.
|
|
302
302
|
|
|
303
|
-
###
|
|
303
|
+
### CORS
|
|
304
304
|
|
|
305
305
|
```python
|
|
306
306
|
from microbootstrap.bootstrappers.litestar import BaseBootstrapSettings
|
|
@@ -316,15 +316,15 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
316
316
|
cors_max_age: int = 600
|
|
317
317
|
```
|
|
318
318
|
|
|
319
|
-
|
|
319
|
+
Parameter descriptions:
|
|
320
320
|
|
|
321
|
-
- `cors_allowed_origins` - list of origins that are
|
|
322
|
-
- `cors_allowed_methods` - list of
|
|
323
|
-
- `cors_allowed_headers` - list of
|
|
324
|
-
- `cors_exposed_headers` - list of headers that are exposed via the 'Access-Control-Expose-Headers' header.
|
|
325
|
-
- `cors_allowed_credentials` - boolean
|
|
326
|
-
- `cors_allowed_origin_regex` - regex to match origins
|
|
327
|
-
- `cors_max_age` - response caching TTL in seconds, defaults to 600.
|
|
321
|
+
- `cors_allowed_origins` - A list of origins that are permitted.
|
|
322
|
+
- `cors_allowed_methods` - A list of HTTP methods that are allowed.
|
|
323
|
+
- `cors_allowed_headers` - A list of headers that are permitted.
|
|
324
|
+
- `cors_exposed_headers` - A list of headers that are exposed via the 'Access-Control-Expose-Headers' header.
|
|
325
|
+
- `cors_allowed_credentials` - A boolean value that dictates whether or not to set the 'Access-Control-Allow-Credentials' header.
|
|
326
|
+
- `cors_allowed_origin_regex` - A regex used to match against origins.
|
|
327
|
+
- `cors_max_age` - The response caching Time-To-Live (TTL) in seconds, defaults to 600.
|
|
328
328
|
|
|
329
329
|
### Swagger
|
|
330
330
|
|
|
@@ -343,25 +343,25 @@ class YourSettings(BaseBootstrapSettings):
|
|
|
343
343
|
swagger_extra_params: dict[str, Any] = {}
|
|
344
344
|
```
|
|
345
345
|
|
|
346
|
-
|
|
346
|
+
Parameter descriptions:
|
|
347
347
|
|
|
348
|
-
- `
|
|
349
|
-
- `
|
|
350
|
-
- `
|
|
351
|
-
- `service_static_path` -
|
|
352
|
-
- `swagger_path` - path
|
|
353
|
-
- `swagger_offline_docs` -
|
|
354
|
-
- `swagger_extra_params` -
|
|
348
|
+
- `service_name` - The name of the service, which will be displayed in the documentation.
|
|
349
|
+
- `service_description` - A brief description of the service, which will also be displayed in the documentation.
|
|
350
|
+
- `service_version` - The current version of the service.
|
|
351
|
+
- `service_static_path` - The path for static files in the service.
|
|
352
|
+
- `swagger_path` - The path where the documentation can be found.
|
|
353
|
+
- `swagger_offline_docs` - A boolean value that, when set to True, allows the Swagger JS bundles to be accessed offline. This is because the service starts to host via static.
|
|
354
|
+
- `swagger_extra_params` - Additional parameters to pass into the OpenAPI configuration.
|
|
355
355
|
|
|
356
356
|
## Configuration
|
|
357
357
|
|
|
358
|
-
|
|
358
|
+
While settings provide a convenient mechanism, it's not always feasible to store everything within them.
|
|
359
359
|
|
|
360
|
-
|
|
360
|
+
There may be cases where you need to configure a tool directly. Here's how it can be done.
|
|
361
361
|
|
|
362
362
|
### Instruments configuration
|
|
363
363
|
|
|
364
|
-
To configure
|
|
364
|
+
To manually configure an instrument, you need to import one of the available configurations from <b>microbootstrap</b>:
|
|
365
365
|
|
|
366
366
|
- `SentryConfig`
|
|
367
367
|
- `OpentelemetryConfig`
|
|
@@ -370,7 +370,7 @@ To configure instruemt manually, you have to import one of available configs fro
|
|
|
370
370
|
- `SwaggerConfig`
|
|
371
371
|
- `CorsConfig`
|
|
372
372
|
|
|
373
|
-
|
|
373
|
+
These configurations can then be passed into the `.configure_instrument` or `.configure_instruments` bootstrapper methods.
|
|
374
374
|
|
|
375
375
|
```python
|
|
376
376
|
import litestar
|
|
@@ -382,12 +382,12 @@ from microbootstrap import SentryConfig, OpentelemetryConfig
|
|
|
382
382
|
application: litestar.Litestar = (
|
|
383
383
|
LitestarBootstrapper(settings)
|
|
384
384
|
.configure_instrument(SentryConfig(sentry_dsn="https://new-dsn"))
|
|
385
|
-
.configure_instrument(OpentelemetryConfig(
|
|
385
|
+
.configure_instrument(OpentelemetryConfig(opentelemetry_endpoint="/new-endpoint"))
|
|
386
386
|
.bootstrap()
|
|
387
387
|
)
|
|
388
388
|
```
|
|
389
389
|
|
|
390
|
-
|
|
390
|
+
Alternatively,
|
|
391
391
|
|
|
392
392
|
```python
|
|
393
393
|
import litestar
|
|
@@ -408,7 +408,7 @@ application: litestar.Litestar = (
|
|
|
408
408
|
|
|
409
409
|
### Application configuration
|
|
410
410
|
|
|
411
|
-
|
|
411
|
+
The application can be configured in a similar manner:
|
|
412
412
|
|
|
413
413
|
```python
|
|
414
414
|
import litestar
|
|
@@ -424,17 +424,16 @@ async def my_handler() -> str:
|
|
|
424
424
|
|
|
425
425
|
application: litestar.Litestar = (
|
|
426
426
|
LitestarBootstrapper(settings)
|
|
427
|
-
.
|
|
427
|
+
.configure_application(AppConfig(route_handlers=[my_handler]))
|
|
428
428
|
.bootstrap()
|
|
429
429
|
)
|
|
430
430
|
```
|
|
431
431
|
|
|
432
432
|
> ### Important
|
|
433
433
|
>
|
|
434
|
-
> When configuring parameters with simple data types such as: `str`, `int`, `float`,
|
|
435
|
-
> Those variables are rewriting previous values.
|
|
434
|
+
> When configuring parameters with simple data types such as: `str`, `int`, `float`, etc., these variables overwrite previous values.
|
|
436
435
|
>
|
|
437
|
-
> Example
|
|
436
|
+
> Example:
|
|
438
437
|
>
|
|
439
438
|
> ```python
|
|
440
439
|
> from microbootstrap import LitestarSettings, SentryConfig
|
|
@@ -453,13 +452,11 @@ application: litestar.Litestar = (
|
|
|
453
452
|
> )
|
|
454
453
|
> ```
|
|
455
454
|
>
|
|
456
|
-
> In this example application will be bootstrapped with new `https://my-new-configured-sentry-dsn`
|
|
457
|
-
> instead of old one.
|
|
455
|
+
> In this example, the application will be bootstrapped with the new `https://my-new-configured-sentry-dsn` Sentry DSN, replacing the old one.
|
|
458
456
|
>
|
|
459
|
-
>
|
|
460
|
-
> They are being expanded or merged into each other.
|
|
457
|
+
> However, when you configure parameters with complex data types such as: `list`, `tuple`, `dict`, or `set`, they are expanded or merged.
|
|
461
458
|
>
|
|
462
|
-
> Example
|
|
459
|
+
> Example:
|
|
463
460
|
>
|
|
464
461
|
> ```python
|
|
465
462
|
> from microbootstrap import LitestarSettings, PrometheusConfig
|
|
@@ -478,14 +475,12 @@ application: litestar.Litestar = (
|
|
|
478
475
|
> )
|
|
479
476
|
> ```
|
|
480
477
|
>
|
|
481
|
-
> In this case
|
|
482
|
-
> This is also true for `list`, `tuple` and `set`
|
|
478
|
+
> In this case, Prometheus will receive `{"first_value": 1, "second_value": 2}` inside `prometheus_additional_params`. This is also true for `list`, `tuple`, and `set`.
|
|
483
479
|
|
|
484
480
|
## Advanced
|
|
485
481
|
|
|
486
|
-
If you
|
|
487
|
-
|
|
488
|
-
Every instrument uses some config, so that's first thing, you have to define.
|
|
482
|
+
If you need a specific instrument, you can create your own.
|
|
483
|
+
Essentially, an `Instrument` is just a class with some abstract methods. Each instrument uses a certain configuration, which is the first thing you need to define.
|
|
489
484
|
|
|
490
485
|
```python
|
|
491
486
|
from microbootstrap.instruments.base import BaseInstrumentConfig
|
|
@@ -496,7 +491,7 @@ class MyInstrumentConfig(BaseInstrumentConfig):
|
|
|
496
491
|
your_list_parameter: list
|
|
497
492
|
```
|
|
498
493
|
|
|
499
|
-
|
|
494
|
+
Next, you can create an instrument class that inherits from `Instrument` and accepts your configuration as a generic parameter.
|
|
500
495
|
|
|
501
496
|
```python
|
|
502
497
|
from microbootstrap.instruments.base import Instrument
|
|
@@ -520,21 +515,20 @@ class MyInstrument(Instrument[MyInstrumentConfig]):
|
|
|
520
515
|
return MyInstrumentConfig
|
|
521
516
|
```
|
|
522
517
|
|
|
523
|
-
|
|
518
|
+
Now, you can define the behavior of your instrument.
|
|
524
519
|
|
|
525
520
|
Attributes:
|
|
526
521
|
|
|
527
|
-
- `instrument_name` -
|
|
528
|
-
- `ready_condition` -
|
|
522
|
+
- `instrument_name` - This will be displayed in your console during bootstrap.
|
|
523
|
+
- `ready_condition` - This will be displayed in your console during bootstrap if the instrument is not ready.
|
|
529
524
|
|
|
530
525
|
Methods:
|
|
531
526
|
|
|
532
|
-
- `is_ready` - defines
|
|
533
|
-
- `teardown` - graceful shutdown
|
|
534
|
-
- `bootstrap` - main instrument
|
|
527
|
+
- `is_ready` - This defines the readiness of the instrument for bootstrapping, based on its configuration values. This is required.
|
|
528
|
+
- `teardown` - This allows for a graceful shutdown of the instrument during application shutdown. This is not required.
|
|
529
|
+
- `bootstrap` - This is the main logic of the instrument. This is not required.
|
|
535
530
|
|
|
536
|
-
|
|
537
|
-
Let's adapt it for litestar for example
|
|
531
|
+
Once you have the framework of the instrument, you can adapt it for any existing framework. For instance, let's adapt it for litestar.
|
|
538
532
|
|
|
539
533
|
```python
|
|
540
534
|
import litestar
|
|
@@ -548,17 +542,16 @@ class LitestarMyInstrument(MyInstrument):
|
|
|
548
542
|
|
|
549
543
|
def bootstrap_after(self, application: litestar.Litestar) -> dict[str, typing.Any]:
|
|
550
544
|
pass
|
|
551
|
-
|
|
552
545
|
```
|
|
553
546
|
|
|
554
|
-
To bind instrument to a bootstrapper,
|
|
547
|
+
To bind the instrument to a bootstrapper, use the `.use_instrument` decorator.
|
|
555
548
|
|
|
556
|
-
To add
|
|
549
|
+
To add extra parameters to the application, you can use:
|
|
557
550
|
|
|
558
|
-
- `bootstrap_before` -
|
|
559
|
-
- `bootstrap_after` -
|
|
551
|
+
- `bootstrap_before` - This adds arguments to the application configuration before creation.
|
|
552
|
+
- `bootstrap_after` - This adds arguments to the application after creation.
|
|
560
553
|
|
|
561
|
-
|
|
554
|
+
Afterwards, you can use your instrument during the bootstrap process.
|
|
562
555
|
|
|
563
556
|
```python
|
|
564
557
|
import litestar
|
|
@@ -581,7 +574,7 @@ application: litestar.Litestar = (
|
|
|
581
574
|
)
|
|
582
575
|
```
|
|
583
576
|
|
|
584
|
-
|
|
577
|
+
Alternatively, you can fill these parameters within your main settings object.
|
|
585
578
|
|
|
586
579
|
```python
|
|
587
580
|
from microbootstrap import LitestarSettings
|
|
@@ -3,18 +3,17 @@ import typing
|
|
|
3
3
|
|
|
4
4
|
import litestar
|
|
5
5
|
import litestar.types
|
|
6
|
-
import sentry_sdk
|
|
7
6
|
import typing_extensions
|
|
8
|
-
from litestar import openapi
|
|
9
|
-
from litestar.config.app import AppConfig as LitestarConfig
|
|
7
|
+
from litestar import openapi
|
|
10
8
|
from litestar.config.cors import CORSConfig as LitestarCorsConfig
|
|
11
9
|
from litestar.contrib.opentelemetry.config import OpenTelemetryConfig as LitestarOpentelemetryConfig
|
|
12
10
|
from litestar.contrib.prometheus import PrometheusConfig as LitestarPrometheusConfig
|
|
13
11
|
from litestar.contrib.prometheus import PrometheusController
|
|
14
|
-
from litestar.exceptions.http_exceptions import HTTPException
|
|
15
12
|
from litestar_offline_docs import generate_static_files_config
|
|
13
|
+
from sentry_sdk.integrations.litestar import LitestarIntegration
|
|
16
14
|
|
|
17
15
|
from microbootstrap.bootstrappers.base import ApplicationBootstrapper
|
|
16
|
+
from microbootstrap.config.litestar import LitestarConfig
|
|
18
17
|
from microbootstrap.instruments.cors_instrument import CorsInstrument
|
|
19
18
|
from microbootstrap.instruments.logging_instrument import LoggingInstrument
|
|
20
19
|
from microbootstrap.instruments.opentelemetry_instrument import OpentelemetryInstrument
|
|
@@ -41,19 +40,13 @@ class LitestarBootstrapper(
|
|
|
41
40
|
|
|
42
41
|
@LitestarBootstrapper.use_instrument()
|
|
43
42
|
class LitestarSentryInstrument(SentryInstrument):
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
or exception.status_code >= status_codes.HTTP_500_INTERNAL_SERVER_ERROR
|
|
52
|
-
):
|
|
53
|
-
sentry_sdk.capture_exception(exception)
|
|
54
|
-
|
|
55
|
-
def bootstrap_before(self) -> dict[str, typing.Any]:
|
|
56
|
-
return {"after_exception": [self.sentry_exception_catcher_hook]}
|
|
43
|
+
def bootstrap(self) -> None:
|
|
44
|
+
for sentry_integration in self.instrument_config.sentry_integrations:
|
|
45
|
+
if isinstance(sentry_integration, LitestarIntegration):
|
|
46
|
+
break
|
|
47
|
+
else:
|
|
48
|
+
self.instrument_config.sentry_integrations.append(LitestarIntegration())
|
|
49
|
+
super().bootstrap()
|
|
57
50
|
|
|
58
51
|
|
|
59
52
|
@LitestarBootstrapper.use_instrument()
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import dataclasses
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
from litestar.config.app import AppConfig
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
if typing.TYPE_CHECKING:
|
|
9
|
+
from litestar.types import OnAppInitHandler
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclasses.dataclass
|
|
13
|
+
class LitestarConfig(AppConfig):
|
|
14
|
+
on_app_init: typing.Sequence[OnAppInitHandler] | None = None
|
|
File without changes
|
|
@@ -26,7 +26,7 @@ classifiers = [
|
|
|
26
26
|
"Programming Language :: Python :: 3.11",
|
|
27
27
|
"Programming Language :: Python :: 3.12",
|
|
28
28
|
]
|
|
29
|
-
version = "0.2.
|
|
29
|
+
version = "0.2.5"
|
|
30
30
|
description = "Package for bootstrapping new micro-services"
|
|
31
31
|
authors = ["community-of-python"]
|
|
32
32
|
readme = "README.md"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/logging_instrument.py
RENAMED
|
File without changes
|
{microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/opentelemetry_instrument.py
RENAMED
|
File without changes
|
{microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/prometheus_instrument.py
RENAMED
|
File without changes
|
{microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/sentry_instrument.py
RENAMED
|
File without changes
|
{microbootstrap-0.2.2 → microbootstrap-0.2.5}/microbootstrap/instruments/swagger_instrument.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|