microbootstrap 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.
- microbootstrap-0/PKG-INFO +920 -0
- microbootstrap-0/README.md +859 -0
- microbootstrap-0/microbootstrap/__init__.py +44 -0
- microbootstrap-0/microbootstrap/bootstrappers/__init__.py +0 -0
- microbootstrap-0/microbootstrap/bootstrappers/base.py +114 -0
- microbootstrap-0/microbootstrap/bootstrappers/fastapi.py +148 -0
- microbootstrap-0/microbootstrap/bootstrappers/faststream.py +121 -0
- microbootstrap-0/microbootstrap/bootstrappers/litestar.py +166 -0
- microbootstrap-0/microbootstrap/config/__init__.py +0 -0
- microbootstrap-0/microbootstrap/config/fastapi.py +64 -0
- microbootstrap-0/microbootstrap/config/faststream.py +27 -0
- microbootstrap-0/microbootstrap/config/litestar.py +21 -0
- microbootstrap-0/microbootstrap/console_writer.py +34 -0
- microbootstrap-0/microbootstrap/exceptions.py +10 -0
- microbootstrap-0/microbootstrap/granian_server.py +41 -0
- microbootstrap-0/microbootstrap/helpers.py +111 -0
- microbootstrap-0/microbootstrap/instruments/__init__.py +0 -0
- microbootstrap-0/microbootstrap/instruments/base.py +62 -0
- microbootstrap-0/microbootstrap/instruments/cors_instrument.py +29 -0
- microbootstrap-0/microbootstrap/instruments/health_checks_instrument.py +39 -0
- microbootstrap-0/microbootstrap/instruments/instrument_box.py +50 -0
- microbootstrap-0/microbootstrap/instruments/logging_instrument.py +202 -0
- microbootstrap-0/microbootstrap/instruments/opentelemetry_instrument.py +203 -0
- microbootstrap-0/microbootstrap/instruments/prometheus_instrument.py +63 -0
- microbootstrap-0/microbootstrap/instruments/pyroscope_instrument.py +54 -0
- microbootstrap-0/microbootstrap/instruments/sentry_instrument.py +124 -0
- microbootstrap-0/microbootstrap/instruments/swagger_instrument.py +30 -0
- microbootstrap-0/microbootstrap/instruments_setupper.py +72 -0
- microbootstrap-0/microbootstrap/middlewares/__init__.py +0 -0
- microbootstrap-0/microbootstrap/middlewares/fastapi.py +42 -0
- microbootstrap-0/microbootstrap/middlewares/litestar.py +49 -0
- microbootstrap-0/microbootstrap/py.typed +0 -0
- microbootstrap-0/microbootstrap/settings.py +112 -0
- microbootstrap-0/pyproject.toml +146 -0
|
@@ -0,0 +1,920 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: microbootstrap
|
|
3
|
+
Version: 0
|
|
4
|
+
Summary: Package for bootstrapping new micro-services
|
|
5
|
+
Keywords: python,microservice,bootstrap,opentelemetry,logging,error-tracing,litestar,fastapi
|
|
6
|
+
Author: community-of-python
|
|
7
|
+
Classifier: Typing :: Typed
|
|
8
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
9
|
+
Classifier: Operating System :: MacOS
|
|
10
|
+
Classifier: Operating System :: Microsoft
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Requires-Dist: eval-type-backport>=0.2
|
|
21
|
+
Requires-Dist: opentelemetry-api>=1.30.0
|
|
22
|
+
Requires-Dist: opentelemetry-exporter-otlp>=1.15.0
|
|
23
|
+
Requires-Dist: opentelemetry-exporter-prometheus-remote-write>=0.46b0
|
|
24
|
+
Requires-Dist: opentelemetry-instrumentation>=0.46b0
|
|
25
|
+
Requires-Dist: opentelemetry-instrumentation-system-metrics>=0.46b0
|
|
26
|
+
Requires-Dist: opentelemetry-sdk>=1.30.0
|
|
27
|
+
Requires-Dist: pydantic-settings>=2
|
|
28
|
+
Requires-Dist: rich>=13
|
|
29
|
+
Requires-Dist: sentry-sdk>=2.7
|
|
30
|
+
Requires-Dist: structlog>=24
|
|
31
|
+
Requires-Dist: pyroscope-io ; sys_platform != 'win32'
|
|
32
|
+
Requires-Dist: opentelemetry-distro[otlp]>=0.54b1
|
|
33
|
+
Requires-Dist: opentelemetry-instrumentation-aio-pika>=0.54b1
|
|
34
|
+
Requires-Dist: opentelemetry-instrumentation-aiohttp-client>=0.54b1
|
|
35
|
+
Requires-Dist: opentelemetry-instrumentation-aiokafka>=0.54b1
|
|
36
|
+
Requires-Dist: opentelemetry-instrumentation-asyncpg>=0.54b1
|
|
37
|
+
Requires-Dist: opentelemetry-instrumentation-httpx>=0.54b1
|
|
38
|
+
Requires-Dist: opentelemetry-instrumentation-logging>=0.54b1
|
|
39
|
+
Requires-Dist: opentelemetry-instrumentation-redis>=0.54b1
|
|
40
|
+
Requires-Dist: opentelemetry-instrumentation-psycopg>=0.54b1
|
|
41
|
+
Requires-Dist: opentelemetry-instrumentation-sqlalchemy>=0.54b1
|
|
42
|
+
Requires-Dist: opentelemetry-instrumentation-asyncio>=0.54b1
|
|
43
|
+
Requires-Dist: opentelemetry-instrumentation-asgi>=0.46b0
|
|
44
|
+
Requires-Dist: orjson>=3.10.18
|
|
45
|
+
Requires-Dist: fastapi>=0.100 ; extra == 'fastapi'
|
|
46
|
+
Requires-Dist: fastapi-offline-docs>=1 ; extra == 'fastapi'
|
|
47
|
+
Requires-Dist: opentelemetry-instrumentation-fastapi>=0.46b0 ; extra == 'fastapi'
|
|
48
|
+
Requires-Dist: prometheus-fastapi-instrumentator>=6.1 ; extra == 'fastapi'
|
|
49
|
+
Requires-Dist: faststream~=0.5 ; extra == 'faststream'
|
|
50
|
+
Requires-Dist: prometheus-client>=0.20 ; extra == 'faststream'
|
|
51
|
+
Requires-Dist: granian[reload]>=1 ; extra == 'granian'
|
|
52
|
+
Requires-Dist: litestar>=2.9 ; extra == 'litestar'
|
|
53
|
+
Requires-Dist: litestar-offline-docs>=1 ; extra == 'litestar'
|
|
54
|
+
Requires-Dist: prometheus-client>=0.20 ; extra == 'litestar'
|
|
55
|
+
Requires-Python: >=3.10, <4
|
|
56
|
+
Provides-Extra: fastapi
|
|
57
|
+
Provides-Extra: faststream
|
|
58
|
+
Provides-Extra: granian
|
|
59
|
+
Provides-Extra: litestar
|
|
60
|
+
Description-Content-Type: text/markdown
|
|
61
|
+
|
|
62
|
+
<p align="center">
|
|
63
|
+
<img src="https://raw.githubusercontent.com/community-of-python/microbootstrap/main/logo.svg" width="350">
|
|
64
|
+
</p>
|
|
65
|
+
<br>
|
|
66
|
+
<p align="center">
|
|
67
|
+
<a href="https://codecov.io/gh/community-of-python/microbootstrap" target="_blank"><img src="https://codecov.io/gh/community-of-python/microbootstrap/branch/main/graph/badge.svg"></a>
|
|
68
|
+
<a href="https://pypi.org/project/microbootstrap/" target="_blank"><img src="https://img.shields.io/pypi/pyversions/microbootstrap"></a>
|
|
69
|
+
<a href="https://pypi.org/project/microbootstrap/" target="_blank"><img src="https://img.shields.io/pypi/v/microbootstrap"></a>
|
|
70
|
+
<a href="https://pypistats.org/packages/microbootstrap" target="_blank"><img src="https://img.shields.io/pypi/dm/microbootstrap"></a>
|
|
71
|
+
</p>
|
|
72
|
+
|
|
73
|
+
<b>microbootstrap</b> assists you in creating applications with all the necessary instruments already set up.
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
# settings.py
|
|
77
|
+
from microbootstrap import LitestarSettings
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class YourSettings(LitestarSettings):
|
|
81
|
+
... # Your settings are stored here
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
settings = YourSettings()
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# application.py
|
|
88
|
+
import litestar
|
|
89
|
+
from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
90
|
+
|
|
91
|
+
from your_application.settings import settings
|
|
92
|
+
|
|
93
|
+
# Use the Litestar application!
|
|
94
|
+
application: litestar.Litestar = LitestarBootstrapper(settings).bootstrap()
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
With <b>microbootstrap</b>, you receive an application with lightweight built-in support for:
|
|
98
|
+
|
|
99
|
+
- `sentry`
|
|
100
|
+
- `prometheus`
|
|
101
|
+
- `opentelemetry`
|
|
102
|
+
- `logging`
|
|
103
|
+
- `cors`
|
|
104
|
+
- `swagger` - with additional offline version support
|
|
105
|
+
- `health-checks`
|
|
106
|
+
|
|
107
|
+
Those instruments can be bootstrapped for:
|
|
108
|
+
|
|
109
|
+
- `fastapi`,
|
|
110
|
+
- `litestar`,
|
|
111
|
+
- or `faststream` service,
|
|
112
|
+
- or even a service that doesn't use one of these frameworks.
|
|
113
|
+
|
|
114
|
+
Interested? Let's dive right in ⚡
|
|
115
|
+
|
|
116
|
+
## Table of Contents
|
|
117
|
+
|
|
118
|
+
- [Installation](#installation)
|
|
119
|
+
- [Quickstart](#quickstart)
|
|
120
|
+
- [Settings](#settings)
|
|
121
|
+
- [Service settings](#service-settings)
|
|
122
|
+
- [Instruments](#instruments)
|
|
123
|
+
- [Sentry](#sentry)
|
|
124
|
+
- [Prometheus](#prometheus)
|
|
125
|
+
- [Opentelemetry](#opentelemetry)
|
|
126
|
+
- [Logging](#logging)
|
|
127
|
+
- [CORS](#cors)
|
|
128
|
+
- [Swagger](#swagger)
|
|
129
|
+
- [Health checks](#health-checks)
|
|
130
|
+
- [Configuration](#configuration)
|
|
131
|
+
- [Instruments configuration](#instruments-configuration)
|
|
132
|
+
- [Application configuration](#application-configuration)
|
|
133
|
+
- [Advanced](#advanced)
|
|
134
|
+
|
|
135
|
+
## Installation
|
|
136
|
+
|
|
137
|
+
Also, you can specify extras during installation for concrete framework:
|
|
138
|
+
|
|
139
|
+
- `fastapi`
|
|
140
|
+
- `litestar`
|
|
141
|
+
- `faststream` (ASGI app)
|
|
142
|
+
|
|
143
|
+
Also we have `granian` extra that is requires for `create_granian_server`.
|
|
144
|
+
|
|
145
|
+
For uv:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
uv add "microbootstrap[fastapi]"
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
For poetry:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
poetry add microbootstrap -E fastapi
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
For pip:
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
pip install "microbootstrap[fastapi]"
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Quickstart
|
|
164
|
+
|
|
165
|
+
To configure your application, you can use the settings object.
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
from microbootstrap import LitestarSettings
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class YourSettings(LitestarSettings):
|
|
172
|
+
# General settings
|
|
173
|
+
service_debug: bool = False
|
|
174
|
+
service_name: str = "my-awesome-service"
|
|
175
|
+
|
|
176
|
+
# Sentry settings
|
|
177
|
+
sentry_dsn: str = "your-sentry-dsn"
|
|
178
|
+
|
|
179
|
+
# Prometheus settings
|
|
180
|
+
prometheus_metrics_path: str = "/my-path"
|
|
181
|
+
|
|
182
|
+
# Opentelemetry settings
|
|
183
|
+
opentelemetry_container_name: str = "your-container"
|
|
184
|
+
opentelemetry_endpoint: str = "/opentelemetry-endpoint"
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
settings = YourSettings()
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Next, use the `Bootstrapper` object to create an application based on your settings.
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
import litestar
|
|
195
|
+
from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
196
|
+
|
|
197
|
+
application: litestar.Litestar = LitestarBootstrapper(settings).bootstrap()
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
This approach will provide you with an application that has all the essential instruments already set up for you.
|
|
201
|
+
|
|
202
|
+
### FastAPI
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
import fastapi
|
|
206
|
+
|
|
207
|
+
from microbootstrap import FastApiSettings
|
|
208
|
+
from microbootstrap.bootstrappers.fastapi import FastApiBootstrapper
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
class YourSettings(FastApiSettings):
|
|
212
|
+
# General settings
|
|
213
|
+
service_debug: bool = False
|
|
214
|
+
service_name: str = "my-awesome-service"
|
|
215
|
+
|
|
216
|
+
# Sentry settings
|
|
217
|
+
sentry_dsn: str = "your-sentry-dsn"
|
|
218
|
+
|
|
219
|
+
# Prometheus settings
|
|
220
|
+
prometheus_metrics_path: str = "/my-path"
|
|
221
|
+
|
|
222
|
+
# Opentelemetry settings
|
|
223
|
+
opentelemetry_container_name: str = "your-container"
|
|
224
|
+
opentelemetry_endpoint: str = "/opentelemetry-endpoint"
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
settings = YourSettings()
|
|
228
|
+
|
|
229
|
+
application: fastapi.FastAPI = FastApiBootstrapper(settings).bootstrap()
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### FastStream
|
|
233
|
+
|
|
234
|
+
```python
|
|
235
|
+
from faststream.asgi import AsgiFastStream
|
|
236
|
+
|
|
237
|
+
from microbootstrap import FastStreamSettings
|
|
238
|
+
from microbootstrap.bootstrappers.faststream import FastStreamBootstrapper
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
class YourSettings(FastStreamSettings):
|
|
242
|
+
# General settings
|
|
243
|
+
service_debug: bool = False
|
|
244
|
+
service_name: str = "my-awesome-service"
|
|
245
|
+
|
|
246
|
+
# Sentry settings
|
|
247
|
+
sentry_dsn: str = "your-sentry-dsn"
|
|
248
|
+
|
|
249
|
+
# Prometheus settings
|
|
250
|
+
prometheus_metrics_path: str = "/my-path"
|
|
251
|
+
|
|
252
|
+
# Opentelemetry settings
|
|
253
|
+
opentelemetry_container_name: str = "your-container"
|
|
254
|
+
opentelemetry_endpoint: str = "/opentelemetry-endpoint"
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
settings = YourSettings()
|
|
258
|
+
|
|
259
|
+
application: AsgiFastStream = FastStreamBootstrapper(settings).bootstrap()
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## Settings
|
|
263
|
+
|
|
264
|
+
The settings object is the core of microbootstrap.
|
|
265
|
+
|
|
266
|
+
All framework-related settings inherit from the `BaseServiceSettings` object. `BaseServiceSettings` defines parameters for the service and various instruments.
|
|
267
|
+
|
|
268
|
+
However, the number of parameters is <b>not confined</b> to those defined in `BaseServiceSettings`. You can add as many as you need.
|
|
269
|
+
|
|
270
|
+
These parameters can be sourced from your environment. By default, no prefix is added to these parameters.
|
|
271
|
+
|
|
272
|
+
Example:
|
|
273
|
+
|
|
274
|
+
```python
|
|
275
|
+
class YourSettings(BaseServiceSettings):
|
|
276
|
+
service_debug: bool = True
|
|
277
|
+
service_name: str = "micro-service"
|
|
278
|
+
|
|
279
|
+
your_awesome_parameter: str = "really awesome"
|
|
280
|
+
|
|
281
|
+
... # Other settings here
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
To source `your_awesome_parameter` from the environment, set the environment variable named `YOUR_AWESOME_PARAMETER`.
|
|
285
|
+
|
|
286
|
+
If you prefer to use a prefix when sourcing parameters, set the `ENVIRONMENT_PREFIX` environment variable in advance.
|
|
287
|
+
|
|
288
|
+
Example:
|
|
289
|
+
|
|
290
|
+
```bash
|
|
291
|
+
$ export ENVIRONMENT_PREFIX=YOUR_PREFIX_
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Then the settings object will attempt to source the variable named `YOUR_PREFIX_YOUR_AWESOME_PARAMETER`.
|
|
295
|
+
|
|
296
|
+
## Service settings
|
|
297
|
+
|
|
298
|
+
Each settings object for every framework includes service parameters that can be utilized by various instruments.
|
|
299
|
+
|
|
300
|
+
You can configure them manually, or set the corresponding environment variables and let <b>microbootstrap</b> to source them automatically.
|
|
301
|
+
|
|
302
|
+
```python
|
|
303
|
+
from microbootstrap.settings import BaseServiceSettings
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class ServiceSettings(BaseServiceSettings):
|
|
307
|
+
service_debug: bool = True
|
|
308
|
+
service_environment: str | None = None
|
|
309
|
+
service_name: str = "micro-service"
|
|
310
|
+
service_description: str = "Micro service description"
|
|
311
|
+
service_version: str = "1.0.0"
|
|
312
|
+
|
|
313
|
+
... # Other settings here
|
|
314
|
+
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## Instruments
|
|
318
|
+
|
|
319
|
+
At present, the following instruments are supported for bootstrapping:
|
|
320
|
+
|
|
321
|
+
- `sentry`
|
|
322
|
+
- `prometheus`
|
|
323
|
+
- `opentelemetry`
|
|
324
|
+
- `pyroscope`
|
|
325
|
+
- `logging`
|
|
326
|
+
- `cors`
|
|
327
|
+
- `swagger`
|
|
328
|
+
|
|
329
|
+
Let's clarify the process required to bootstrap these instruments.
|
|
330
|
+
|
|
331
|
+
### [Sentry](https://sentry.io/)
|
|
332
|
+
|
|
333
|
+
To bootstrap Sentry, you must provide at least the `sentry_dsn`.
|
|
334
|
+
Additional parameters can also be supplied through the settings object.
|
|
335
|
+
|
|
336
|
+
```python
|
|
337
|
+
from microbootstrap.settings import BaseServiceSettings
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
class YourSettings(BaseServiceSettings):
|
|
341
|
+
service_environment: str | None = None
|
|
342
|
+
|
|
343
|
+
sentry_dsn: str | None = None
|
|
344
|
+
sentry_traces_sample_rate: float | None = None
|
|
345
|
+
sentry_sample_rate: float = pydantic.Field(default=1.0, le=1.0, ge=0.0)
|
|
346
|
+
sentry_max_breadcrumbs: int = 15
|
|
347
|
+
sentry_max_value_length: int = 16384
|
|
348
|
+
sentry_attach_stacktrace: bool = True
|
|
349
|
+
sentry_integrations: list[Integration] = []
|
|
350
|
+
sentry_additional_params: dict[str, typing.Any] = {}
|
|
351
|
+
sentry_tags: dict[str, str] | None = None
|
|
352
|
+
sentry_opentelemetry_trace_url_template: str | None = None
|
|
353
|
+
|
|
354
|
+
... # Other settings here
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
These settings are subsequently passed to the [sentry-sdk](https://pypi.org/project/sentry-sdk/) package, finalizing your Sentry integration.
|
|
358
|
+
|
|
359
|
+
Parameter descriptions:
|
|
360
|
+
|
|
361
|
+
- `service_environment` - The environment name for Sentry events.
|
|
362
|
+
- `sentry_dsn` - The Data Source Name for your Sentry project.
|
|
363
|
+
- `sentry_traces_sample_rate` - The rate at which traces are sampled (via Sentry Tracing, not OpenTelemetry).
|
|
364
|
+
- `sentry_sample_rate` - The rate at which transactions are sampled.
|
|
365
|
+
- `sentry_max_breadcrumbs` - The maximum number of breadcrumbs to keep.
|
|
366
|
+
- `sentry_max_value_length` - The maximum length of values in Sentry events.
|
|
367
|
+
- `sentry_attach_stacktrace` - Whether to attach stacktraces to messages.
|
|
368
|
+
- `sentry_integrations` - A list of Sentry integrations to enable.
|
|
369
|
+
- `sentry_additional_params` - Additional parameters to pass to Sentry SDK.
|
|
370
|
+
- `sentry_tags` - Tags to apply to all Sentry events.
|
|
371
|
+
- `sentry_opentelemetry_trace_url_template` - Template for OpenTelemetry trace URLs to add to Sentry events (example: `"https://example.com/traces/{trace_id}"`).
|
|
372
|
+
|
|
373
|
+
### [Prometheus](https://prometheus.io/)
|
|
374
|
+
|
|
375
|
+
Prometheus integration presents a challenge because the underlying libraries for `FastAPI`, `Litestar` and `FastStream` differ significantly, making it impossible to unify them under a single interface. As a result, the Prometheus settings for `FastAPI`, `Litestar` and `FastStream` must be configured separately.
|
|
376
|
+
|
|
377
|
+
#### FastAPI
|
|
378
|
+
|
|
379
|
+
To bootstrap prometheus you have to provide `prometheus_metrics_path`
|
|
380
|
+
|
|
381
|
+
```python
|
|
382
|
+
from microbootstrap.settings import FastApiSettings
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
class YourSettings(FastApiSettings):
|
|
386
|
+
service_name: str
|
|
387
|
+
|
|
388
|
+
prometheus_metrics_path: str = "/metrics"
|
|
389
|
+
prometheus_metrics_include_in_schema: bool = False
|
|
390
|
+
prometheus_instrumentator_params: dict[str, typing.Any] = {}
|
|
391
|
+
prometheus_instrument_params: dict[str, typing.Any] = {}
|
|
392
|
+
prometheus_expose_params: dict[str, typing.Any] = {}
|
|
393
|
+
|
|
394
|
+
... # Other settings here
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
Parameters description:
|
|
398
|
+
|
|
399
|
+
- `service_name` - will be attached to metrics's names, but has to be named in [snake_case](https://en.wikipedia.org/wiki/Snake_case).
|
|
400
|
+
- `prometheus_metrics_path` - path to metrics handler.
|
|
401
|
+
- `prometheus_metrics_include_in_schema` - whether to include metrics route in OpenAPI schema.
|
|
402
|
+
- `prometheus_instrumentator_params` - will be passed to `Instrumentor` during initialization.
|
|
403
|
+
- `prometheus_instrument_params` - will be passed to `Instrumentor.instrument(...)`.
|
|
404
|
+
- `prometheus_expose_params` - will be passed to `Instrumentor.expose(...)`.
|
|
405
|
+
|
|
406
|
+
FastAPI prometheus bootstrapper uses [prometheus-fastapi-instrumentator](https://github.com/trallnag/prometheus-fastapi-instrumentator) that's why there are three different dict for parameters.
|
|
407
|
+
|
|
408
|
+
#### Litestar
|
|
409
|
+
|
|
410
|
+
To bootstrap prometheus you have to provide `prometheus_metrics_path`
|
|
411
|
+
|
|
412
|
+
```python
|
|
413
|
+
from microbootstrap.settings import LitestarSettings
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
class YourSettings(LitestarSettings):
|
|
417
|
+
service_name: str
|
|
418
|
+
|
|
419
|
+
prometheus_metrics_path: str = "/metrics"
|
|
420
|
+
prometheus_additional_params: dict[str, typing.Any] = {}
|
|
421
|
+
|
|
422
|
+
... # Other settings here
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
Parameters description:
|
|
426
|
+
|
|
427
|
+
- `service_name` - will be attached to metric's names, there are no name restrictions.
|
|
428
|
+
- `prometheus_metrics_path` - path to metrics handler.
|
|
429
|
+
- `prometheus_additional_params` - will be passed to `litestar.contrib.prometheus.PrometheusConfig`.
|
|
430
|
+
|
|
431
|
+
#### FastStream
|
|
432
|
+
|
|
433
|
+
To bootstrap prometheus you have to provide `prometheus_metrics_path` and `prometheus_middleware_cls`:
|
|
434
|
+
|
|
435
|
+
```python
|
|
436
|
+
from microbootstrap import FastStreamSettings
|
|
437
|
+
from faststream.redis.prometheus import RedisPrometheusMiddleware
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
class YourSettings(FastStreamSettings):
|
|
441
|
+
service_name: str
|
|
442
|
+
|
|
443
|
+
prometheus_metrics_path: str = "/metrics"
|
|
444
|
+
prometheus_middleware_cls: type[FastStreamPrometheusMiddlewareProtocol] | None = RedisPrometheusMiddleware
|
|
445
|
+
|
|
446
|
+
... # Other settings here
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
Parameters description:
|
|
450
|
+
|
|
451
|
+
- `service_name` - will be attached to metric's names, there are no name restrictions.
|
|
452
|
+
- `prometheus_metrics_path` - path to metrics handler.
|
|
453
|
+
- `prometheus_middleware_cls` - Prometheus middleware for your broker.
|
|
454
|
+
|
|
455
|
+
### [OpenTelemetry](https://opentelemetry.io/)
|
|
456
|
+
|
|
457
|
+
To bootstrap OpenTelemetry, you must provide `opentelemetry_endpoint` or set `opentelemetry_log_traces` to `True`.
|
|
458
|
+
|
|
459
|
+
However, additional parameters can also be supplied if needed.
|
|
460
|
+
|
|
461
|
+
```python
|
|
462
|
+
from microbootstrap.settings import BaseServiceSettings, FastStreamPrometheusMiddlewareProtocol
|
|
463
|
+
from microbootstrap.instruments.opentelemetry_instrument import OpenTelemetryInstrumentor
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
class YourSettings(BaseServiceSettings):
|
|
467
|
+
service_name: str
|
|
468
|
+
service_version: str
|
|
469
|
+
|
|
470
|
+
opentelemetry_service_name: str | None = None
|
|
471
|
+
opentelemetry_container_name: str | None = None
|
|
472
|
+
opentelemetry_endpoint: str | None = None
|
|
473
|
+
opentelemetry_namespace: str | None = None
|
|
474
|
+
opentelemetry_insecure: bool = True
|
|
475
|
+
opentelemetry_instrumentors: list[OpenTelemetryInstrumentor] = []
|
|
476
|
+
opentelemetry_exclude_urls: list[str] = []
|
|
477
|
+
|
|
478
|
+
... # Other settings here
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
Parameters description:
|
|
482
|
+
|
|
483
|
+
- `service_name` - will be passed to the `Resource`.
|
|
484
|
+
- `service_version` - will be passed to the `Resource`.
|
|
485
|
+
- `opentelemetry_service_name` - if provided, will be passed to the `Resource` instead of `service_name`.
|
|
486
|
+
- `opentelemetry_endpoint` - will be passed to `OTLPSpanExporter` as endpoint.
|
|
487
|
+
- `opentelemetry_namespace` - will be passed to the `Resource`.
|
|
488
|
+
- `opentelemetry_insecure` - is opentelemetry connection secure.
|
|
489
|
+
- `opentelemetry_container_name` - will be passed to the `Resource`.
|
|
490
|
+
- `opentelemetry_instrumentors` - a list of extra instrumentors.
|
|
491
|
+
- `opentelemetry_exclude_urls` - list of ignored urls.
|
|
492
|
+
- `opentelemetry_log_traces` - traces will be logged to stdout.
|
|
493
|
+
|
|
494
|
+
These settings are subsequently passed to [opentelemetry](https://opentelemetry.io/), finalizing your Opentelemetry integration.
|
|
495
|
+
|
|
496
|
+
#### FastStream
|
|
497
|
+
|
|
498
|
+
For FastStream you also should pass `opentelemetry_middleware_cls` - OpenTelemetry middleware for your broker
|
|
499
|
+
|
|
500
|
+
```python
|
|
501
|
+
from microbootstrap import FastStreamSettings, FastStreamTelemetryMiddlewareProtocol
|
|
502
|
+
from faststream.redis.opentelemetry import RedisTelemetryMiddleware
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
class YourSettings(FastStreamSettings):
|
|
506
|
+
...
|
|
507
|
+
opentelemetry_middleware_cls: type[FastStreamTelemetryMiddlewareProtocol] | None = RedisTelemetryMiddleware
|
|
508
|
+
...
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
### [Pyroscope](https://pyroscope.io)
|
|
512
|
+
|
|
513
|
+
To integrate Pyroscope, specify the `pyroscope_endpoint`.
|
|
514
|
+
|
|
515
|
+
- The `opentelemetry_service_name` will be used as the application name.
|
|
516
|
+
- `service_namespace` tag will be added with `opentelemetry_namespace` value.
|
|
517
|
+
- You can also set `pyroscope_sample_rate`, `pyroscope_auth_token`, `pyroscope_tags` and `pyroscope_additional_params` — params that will be passed to `pyroscope.configure`.
|
|
518
|
+
|
|
519
|
+
When both Pyroscope and OpenTelemetry are enabled, profile span IDs will be included in traces using [`pyroscope-otel`](https://github.com/grafana/otel-profiling-python) for correlation.
|
|
520
|
+
|
|
521
|
+
Note that Pyroscope integration is not supported on Windows.
|
|
522
|
+
|
|
523
|
+
### Logging
|
|
524
|
+
|
|
525
|
+
<b>microbootstrap</b> provides in-memory JSON logging through the use of [structlog](https://pypi.org/project/structlog/).
|
|
526
|
+
For more information on in-memory logging, refer to [MemoryHandler](https://docs.python.org/3/library/logging.handlers.html#memoryhandler).
|
|
527
|
+
|
|
528
|
+
To utilize this feature, your application must be in non-debug mode, meaning `service_debug` should be set to `False`.
|
|
529
|
+
|
|
530
|
+
```python
|
|
531
|
+
import logging
|
|
532
|
+
|
|
533
|
+
from microbootstrap.settings import BaseServiceSettings
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
class YourSettings(BaseServiceSettings):
|
|
537
|
+
service_debug: bool = False
|
|
538
|
+
|
|
539
|
+
logging_log_level: int = logging.INFO
|
|
540
|
+
logging_flush_level: int = logging.ERROR
|
|
541
|
+
logging_buffer_capacity: int = 10
|
|
542
|
+
logging_unset_handlers: list[str] = ["uvicorn", "uvicorn.access"]
|
|
543
|
+
logging_extra_processors: list[typing.Any] = []
|
|
544
|
+
logging_exclude_endpoints: list[str] = ["/health/", "/metrics"]
|
|
545
|
+
logging_turn_off_middleware: bool = False
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
Parameters description:
|
|
549
|
+
|
|
550
|
+
- `logging_log_level` - The default log level.
|
|
551
|
+
- `logging_flush_level` - All messages will be flushed from the buffer when a log with this level appears.
|
|
552
|
+
- `logging_buffer_capacity` - The number of messages your buffer will store before being flushed.
|
|
553
|
+
- `logging_unset_handlers` - Unset logger handlers.
|
|
554
|
+
- `logging_extra_processors` - Set additional structlog processors if needed.
|
|
555
|
+
- `logging_exclude_endpoints` - Exclude logging on specific endpoints.
|
|
556
|
+
- `logging_turn_off_middleware` - Turning off logging middleware.
|
|
557
|
+
|
|
558
|
+
### CORS
|
|
559
|
+
|
|
560
|
+
```python
|
|
561
|
+
from microbootstrap.settings import BaseServiceSettings
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
class YourSettings(BaseServiceSettings):
|
|
565
|
+
cors_allowed_origins: list[str] = pydantic.Field(default_factory=list)
|
|
566
|
+
cors_allowed_methods: list[str] = pydantic.Field(default_factory=list)
|
|
567
|
+
cors_allowed_headers: list[str] = pydantic.Field(default_factory=list)
|
|
568
|
+
cors_exposed_headers: list[str] = pydantic.Field(default_factory=list)
|
|
569
|
+
cors_allowed_credentials: bool = False
|
|
570
|
+
cors_allowed_origin_regex: str | None = None
|
|
571
|
+
cors_max_age: int = 600
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
Parameter descriptions:
|
|
575
|
+
|
|
576
|
+
- `cors_allowed_origins` - A list of origins that are permitted.
|
|
577
|
+
- `cors_allowed_methods` - A list of HTTP methods that are allowed.
|
|
578
|
+
- `cors_allowed_headers` - A list of headers that are permitted.
|
|
579
|
+
- `cors_exposed_headers` - A list of headers that are exposed via the 'Access-Control-Expose-Headers' header.
|
|
580
|
+
- `cors_allowed_credentials` - A boolean value that dictates whether or not to set the 'Access-Control-Allow-Credentials' header.
|
|
581
|
+
- `cors_allowed_origin_regex` - A regex used to match against origins.
|
|
582
|
+
- `cors_max_age` - The response caching Time-To-Live (TTL) in seconds, defaults to 600.
|
|
583
|
+
|
|
584
|
+
### Swagger
|
|
585
|
+
|
|
586
|
+
```python
|
|
587
|
+
from microbootstrap.settings import BaseServiceSettings
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
class YourSettings(BaseServiceSettings):
|
|
591
|
+
service_name: str = "micro-service"
|
|
592
|
+
service_description: str = "Micro service description"
|
|
593
|
+
service_version: str = "1.0.0"
|
|
594
|
+
service_static_path: str = "/static"
|
|
595
|
+
|
|
596
|
+
swagger_path: str = "/docs"
|
|
597
|
+
swagger_offline_docs: bool = False
|
|
598
|
+
swagger_extra_params: dict[str, Any] = {}
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
Parameter descriptions:
|
|
602
|
+
|
|
603
|
+
- `service_name` - The name of the service, which will be displayed in the documentation.
|
|
604
|
+
- `service_description` - A brief description of the service, which will also be displayed in the documentation.
|
|
605
|
+
- `service_version` - The current version of the service.
|
|
606
|
+
- `service_static_path` - The path for static files in the service.
|
|
607
|
+
- `swagger_path` - The path where the documentation can be found.
|
|
608
|
+
- `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.
|
|
609
|
+
- `swagger_extra_params` - Additional parameters to pass into the OpenAPI configuration.
|
|
610
|
+
|
|
611
|
+
#### FastStream AsyncAPI documentation
|
|
612
|
+
|
|
613
|
+
AsyncAPI documentation is available by default under `/asyncapi` route. You can change that by setting `asyncapi_path`:
|
|
614
|
+
|
|
615
|
+
```python
|
|
616
|
+
from microbootstrap import FastStreamSettings
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
class YourSettings(FastStreamSettings):
|
|
620
|
+
asyncapi_path: str | None = None
|
|
621
|
+
```
|
|
622
|
+
|
|
623
|
+
### Health checks
|
|
624
|
+
|
|
625
|
+
```python
|
|
626
|
+
from microbootstrap.settings import BaseServiceSettings
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
class YourSettings(BaseServiceSettings):
|
|
630
|
+
service_name: str = "micro-service"
|
|
631
|
+
service_version: str = "1.0.0"
|
|
632
|
+
|
|
633
|
+
health_checks_enabled: bool = True
|
|
634
|
+
health_checks_path: str = "/health/"
|
|
635
|
+
health_checks_include_in_schema: bool = False
|
|
636
|
+
```
|
|
637
|
+
|
|
638
|
+
Parameter descriptions:
|
|
639
|
+
|
|
640
|
+
- `service_name` - Will be displayed in health check response.
|
|
641
|
+
- `service_version` - Will be displayed in health check response.
|
|
642
|
+
- `health_checks_enabled` - Must be True to enable health checks.
|
|
643
|
+
- `health_checks_path` - Path for health check handler.
|
|
644
|
+
- `health_checks_include_in_schema` - Must be True to include `health_checks_path` (`/health/`) in OpenAPI schema.
|
|
645
|
+
|
|
646
|
+
## Configuration
|
|
647
|
+
|
|
648
|
+
While settings provide a convenient mechanism, it's not always feasible to store everything within them.
|
|
649
|
+
|
|
650
|
+
There may be cases where you need to configure a tool directly. Here's how it can be done.
|
|
651
|
+
|
|
652
|
+
### Instruments configuration
|
|
653
|
+
|
|
654
|
+
To manually configure an instrument, you need to import one of the available configurations from <b>microbootstrap</b>:
|
|
655
|
+
|
|
656
|
+
- `SentryConfig`
|
|
657
|
+
- `OpentelemetryConfig`
|
|
658
|
+
- `PrometheusConfig`
|
|
659
|
+
- `LoggingConfig`
|
|
660
|
+
- `SwaggerConfig`
|
|
661
|
+
- `CorsConfig`
|
|
662
|
+
|
|
663
|
+
These configurations can then be passed into the `.configure_instrument` or `.configure_instruments` bootstrapper methods.
|
|
664
|
+
|
|
665
|
+
```python
|
|
666
|
+
import litestar
|
|
667
|
+
|
|
668
|
+
from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
669
|
+
from microbootstrap import SentryConfig, OpentelemetryConfig
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
application: litestar.Litestar = (
|
|
673
|
+
LitestarBootstrapper(settings)
|
|
674
|
+
.configure_instrument(SentryConfig(sentry_dsn="https://new-dsn"))
|
|
675
|
+
.configure_instrument(OpentelemetryConfig(opentelemetry_endpoint="/new-endpoint"))
|
|
676
|
+
.bootstrap()
|
|
677
|
+
)
|
|
678
|
+
```
|
|
679
|
+
|
|
680
|
+
Alternatively,
|
|
681
|
+
|
|
682
|
+
```python
|
|
683
|
+
import litestar
|
|
684
|
+
|
|
685
|
+
from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
686
|
+
from microbootstrap import SentryConfig, OpentelemetryConfig
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
application: litestar.Litestar = (
|
|
690
|
+
LitestarBootstrapper(settings)
|
|
691
|
+
.configure_instruments(
|
|
692
|
+
SentryConfig(sentry_dsn="https://examplePublicKey@o0.ingest.sentry.io/0"),
|
|
693
|
+
OpentelemetryConfig(opentelemetry_endpoint="/new-endpoint")
|
|
694
|
+
)
|
|
695
|
+
.bootstrap()
|
|
696
|
+
)
|
|
697
|
+
```
|
|
698
|
+
|
|
699
|
+
### Application configuration
|
|
700
|
+
|
|
701
|
+
The application can be configured in a similar manner:
|
|
702
|
+
|
|
703
|
+
```python
|
|
704
|
+
import litestar
|
|
705
|
+
|
|
706
|
+
from microbootstrap.config.litestar import LitestarConfig
|
|
707
|
+
from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
708
|
+
from microbootstrap import SentryConfig, OpentelemetryConfig
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
@litestar.get("/my-handler")
|
|
712
|
+
async def my_handler() -> str:
|
|
713
|
+
return "Ok"
|
|
714
|
+
|
|
715
|
+
application: litestar.Litestar = (
|
|
716
|
+
LitestarBootstrapper(settings)
|
|
717
|
+
.configure_application(LitestarConfig(route_handlers=[my_handler]))
|
|
718
|
+
.bootstrap()
|
|
719
|
+
)
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
> ### Important
|
|
723
|
+
>
|
|
724
|
+
> When configuring parameters with simple data types such as: `str`, `int`, `float`, etc., these variables overwrite previous values.
|
|
725
|
+
>
|
|
726
|
+
> Example:
|
|
727
|
+
>
|
|
728
|
+
> ```python
|
|
729
|
+
> from microbootstrap import LitestarSettings, SentryConfig
|
|
730
|
+
>
|
|
731
|
+
>
|
|
732
|
+
> class YourSettings(LitestarSettings):
|
|
733
|
+
> sentry_dsn: str = "https://my-sentry-dsn"
|
|
734
|
+
>
|
|
735
|
+
>
|
|
736
|
+
> application: litestar.Litestar = (
|
|
737
|
+
> LitestarBootstrapper(YourSettings())
|
|
738
|
+
> .configure_instrument(
|
|
739
|
+
> SentryConfig(sentry_dsn="https://my-new-configured-sentry-dsn")
|
|
740
|
+
> )
|
|
741
|
+
> .bootstrap()
|
|
742
|
+
> )
|
|
743
|
+
> ```
|
|
744
|
+
>
|
|
745
|
+
> In this example, the application will be bootstrapped with the new `https://my-new-configured-sentry-dsn` Sentry DSN, replacing the old one.
|
|
746
|
+
>
|
|
747
|
+
> However, when you configure parameters with complex data types such as: `list`, `tuple`, `dict`, or `set`, they are expanded or merged.
|
|
748
|
+
>
|
|
749
|
+
> Example:
|
|
750
|
+
>
|
|
751
|
+
> ```python
|
|
752
|
+
> from microbootstrap import LitestarSettings, PrometheusConfig
|
|
753
|
+
>
|
|
754
|
+
>
|
|
755
|
+
> class YourSettings(LitestarSettings):
|
|
756
|
+
> prometheus_additional_params: dict[str, Any] = {"first_value": 1}
|
|
757
|
+
>
|
|
758
|
+
>
|
|
759
|
+
> application: litestar.Litestar = (
|
|
760
|
+
> LitestarBootstrapper(YourSettings())
|
|
761
|
+
> .configure_instrument(
|
|
762
|
+
> PrometheusConfig(prometheus_additional_params={"second_value": 2})
|
|
763
|
+
> )
|
|
764
|
+
> .bootstrap()
|
|
765
|
+
> )
|
|
766
|
+
> ```
|
|
767
|
+
>
|
|
768
|
+
> 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`.
|
|
769
|
+
|
|
770
|
+
### Using microbootstrap without a framework
|
|
771
|
+
|
|
772
|
+
When working on projects that don't use Litestar or FastAPI, you can still take advantage of monitoring and logging capabilities using `InstrumentsSetupper`. This class sets up Sentry, OpenTelemetry, Pyroscope and Logging instruments in a way that's easy to integrate with your project.
|
|
773
|
+
|
|
774
|
+
You can use `InstrumentsSetupper` as a context manager, like this:
|
|
775
|
+
|
|
776
|
+
```python
|
|
777
|
+
from microbootstrap.instruments_setupper import InstrumentsSetupper
|
|
778
|
+
from microbootstrap import InstrumentsSetupperSettings
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
class YourSettings(InstrumentsSetupperSettings):
|
|
782
|
+
...
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
with InstrumentsSetupper(YourSettings()):
|
|
786
|
+
while True:
|
|
787
|
+
print("doing something useful")
|
|
788
|
+
time.sleep(1)
|
|
789
|
+
```
|
|
790
|
+
|
|
791
|
+
Alternatively, you can use the `setup()` and `teardown()` methods instead of a context manager:
|
|
792
|
+
|
|
793
|
+
```python
|
|
794
|
+
current_setupper = InstrumentsSetupper(YourSettings())
|
|
795
|
+
current_setupper.setup()
|
|
796
|
+
try:
|
|
797
|
+
while True:
|
|
798
|
+
print("doing something useful")
|
|
799
|
+
time.sleep(1)
|
|
800
|
+
finally:
|
|
801
|
+
current_setupper.teardown()
|
|
802
|
+
```
|
|
803
|
+
|
|
804
|
+
Like bootstrappers, you can reconfigure instruments using the `configure_instrument()` and `configure_instruments()` methods.
|
|
805
|
+
|
|
806
|
+
## Advanced
|
|
807
|
+
|
|
808
|
+
If you miss some instrument, you can add your own.
|
|
809
|
+
Essentially, `Instrument` is just a class with some abstractmethods.
|
|
810
|
+
Every instrument uses some config, so that's first thing, you have to define.
|
|
811
|
+
|
|
812
|
+
```python
|
|
813
|
+
from microbootstrap.instruments.base import BaseInstrumentConfig
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
class MyInstrumentConfig(BaseInstrumentConfig):
|
|
817
|
+
your_string_parameter: str
|
|
818
|
+
your_list_parameter: list
|
|
819
|
+
```
|
|
820
|
+
|
|
821
|
+
Next, you can create an instrument class that inherits from `Instrument` and accepts your configuration as a generic parameter.
|
|
822
|
+
|
|
823
|
+
```python
|
|
824
|
+
from microbootstrap.instruments.base import Instrument
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
class MyInstrument(Instrument[MyInstrumentConfig]):
|
|
828
|
+
instrument_name: str
|
|
829
|
+
ready_condition: str
|
|
830
|
+
|
|
831
|
+
def is_ready(self) -> bool:
|
|
832
|
+
pass
|
|
833
|
+
|
|
834
|
+
def teardown(self) -> None:
|
|
835
|
+
pass
|
|
836
|
+
|
|
837
|
+
def bootstrap(self) -> None:
|
|
838
|
+
pass
|
|
839
|
+
|
|
840
|
+
@classmethod
|
|
841
|
+
def get_config_type(cls) -> type[MyInstrumentConfig]:
|
|
842
|
+
return MyInstrumentConfig
|
|
843
|
+
```
|
|
844
|
+
|
|
845
|
+
Now, you can define the behavior of your instrument.
|
|
846
|
+
|
|
847
|
+
Attributes:
|
|
848
|
+
|
|
849
|
+
- `instrument_name` - This will be displayed in your console during bootstrap.
|
|
850
|
+
- `ready_condition` - This will be displayed in your console during bootstrap if the instrument is not ready.
|
|
851
|
+
|
|
852
|
+
Methods:
|
|
853
|
+
|
|
854
|
+
- `is_ready` - This defines the readiness of the instrument for bootstrapping, based on its configuration values. This is required.
|
|
855
|
+
- `teardown` - This allows for a graceful shutdown of the instrument during application shutdown. This is not required.
|
|
856
|
+
- `bootstrap` - This is the main logic of the instrument. This is not required.
|
|
857
|
+
|
|
858
|
+
Once you have the framework of the instrument, you can adapt it for any existing framework. For instance, let's adapt it for litestar.
|
|
859
|
+
|
|
860
|
+
```python
|
|
861
|
+
import litestar
|
|
862
|
+
|
|
863
|
+
from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
864
|
+
|
|
865
|
+
@LitestarBootstrapper.use_instrument()
|
|
866
|
+
class LitestarMyInstrument(MyInstrument):
|
|
867
|
+
def bootstrap_before(self) -> dict[str, typing.Any]:
|
|
868
|
+
pass
|
|
869
|
+
|
|
870
|
+
def bootstrap_after(self, application: litestar.Litestar) -> dict[str, typing.Any]:
|
|
871
|
+
pass
|
|
872
|
+
```
|
|
873
|
+
|
|
874
|
+
To bind the instrument to a bootstrapper, use the `.use_instrument` decorator.
|
|
875
|
+
|
|
876
|
+
To add extra parameters to the application, you can use:
|
|
877
|
+
|
|
878
|
+
- `bootstrap_before` - This adds arguments to the application configuration before creation.
|
|
879
|
+
- `bootstrap_after` - This adds arguments to the application after creation.
|
|
880
|
+
|
|
881
|
+
Afterwards, you can use your instrument during the bootstrap process.
|
|
882
|
+
|
|
883
|
+
```python
|
|
884
|
+
import litestar
|
|
885
|
+
|
|
886
|
+
from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
887
|
+
from microbootstrap import SentryConfig, OpentelemetryConfig
|
|
888
|
+
|
|
889
|
+
from your_app import MyInstrumentConfig
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
application: litestar.Litestar = (
|
|
893
|
+
LitestarBootstrapper(settings)
|
|
894
|
+
.configure_instrument(
|
|
895
|
+
MyInstrumentConfig(
|
|
896
|
+
your_string_parameter="very-nice-parameter",
|
|
897
|
+
your_list_parameter=["very-special-list"],
|
|
898
|
+
)
|
|
899
|
+
)
|
|
900
|
+
.bootstrap()
|
|
901
|
+
)
|
|
902
|
+
```
|
|
903
|
+
|
|
904
|
+
Alternatively, you can fill these parameters within your main settings object.
|
|
905
|
+
|
|
906
|
+
```python
|
|
907
|
+
from microbootstrap import LitestarSettings
|
|
908
|
+
from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
|
|
909
|
+
|
|
910
|
+
from your_app import MyInstrumentConfig
|
|
911
|
+
|
|
912
|
+
|
|
913
|
+
class YourSettings(LitestarSettings, MyInstrumentConfig):
|
|
914
|
+
your_string_parameter: str = "very-nice-parameter"
|
|
915
|
+
your_list_parameter: list = ["very-special-list"]
|
|
916
|
+
|
|
917
|
+
settings = YourSettings()
|
|
918
|
+
|
|
919
|
+
application: litestar.Litestar = LitestarBootstrapper(settings).bootstrap()
|
|
920
|
+
```
|