Microbootstrap
Bootstrap your microservices in a second!
Install / Use
/learn @community-of-python/MicrobootstrapREADME
<b>microbootstrap</b> assists you in creating applications with all the necessary instruments already set up.
# settings.py
from microbootstrap import LitestarSettings
class YourSettings(LitestarSettings):
... # Your settings are stored here
settings = YourSettings()
# application.py
import litestar
from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
from your_application.settings import settings
# Use the Litestar application!
application: litestar.Litestar = LitestarBootstrapper(settings).bootstrap()
With <b>microbootstrap</b>, you receive an application with lightweight built-in support for:
sentryprometheusopentelemetryloggingcorsswagger- with additional offline version supporthealth-checks
Those instruments can be bootstrapped for:
fastapi,litestar,- or
faststreamservice, - or even a service that doesn't use one of these frameworks.
Interested? Let's dive right in ⚡
Table of Contents
Installation
Also, you can specify extras during installation for concrete framework:
fastapilitestarfaststream(ASGI app)
Also we have granian extra that is requires for create_granian_server.
For uv:
uv add "microbootstrap[fastapi]"
For poetry:
poetry add microbootstrap -E fastapi
For pip:
pip install "microbootstrap[fastapi]"
Quickstart
To configure your application, you can use the settings object.
from microbootstrap import LitestarSettings
class YourSettings(LitestarSettings):
# General settings
service_debug: bool = False
service_name: str = "my-awesome-service"
# Sentry settings
sentry_dsn: str = "your-sentry-dsn"
# Prometheus settings
prometheus_metrics_path: str = "/my-path"
# Opentelemetry settings
opentelemetry_container_name: str = "your-container"
opentelemetry_endpoint: str = "/opentelemetry-endpoint"
settings = YourSettings()
Next, use the Bootstrapper object to create an application based on your settings.
import litestar
from microbootstrap.bootstrappers.litestar import LitestarBootstrapper
application: litestar.Litestar = LitestarBootstrapper(settings).bootstrap()
This approach will provide you with an application that has all the essential instruments already set up for you.
FastAPI
import fastapi
from microbootstrap import FastApiSettings
from microbootstrap.bootstrappers.fastapi import FastApiBootstrapper
class YourSettings(FastApiSettings):
# General settings
service_debug: bool = False
service_name: str = "my-awesome-service"
# Sentry settings
sentry_dsn: str = "your-sentry-dsn"
# Prometheus settings
prometheus_metrics_path: str = "/my-path"
# Opentelemetry settings
opentelemetry_container_name: str = "your-container"
opentelemetry_endpoint: str = "/opentelemetry-endpoint"
settings = YourSettings()
application: fastapi.FastAPI = FastApiBootstrapper(settings).bootstrap()
FastStream
from faststream.asgi import AsgiFastStream
from microbootstrap import FastStreamSettings
from microbootstrap.bootstrappers.faststream import FastStreamBootstrapper
class YourSettings(FastStreamSettings):
# General settings
service_debug: bool = False
service_name: str = "my-awesome-service"
# Sentry settings
sentry_dsn: str = "your-sentry-dsn"
# Prometheus settings
prometheus_metrics_path: str = "/my-path"
# Opentelemetry settings
opentelemetry_container_name: str = "your-container"
opentelemetry_endpoint: str = "/opentelemetry-endpoint"
settings = YourSettings()
application: AsgiFastStream = FastStreamBootstrapper(settings).bootstrap()
Settings
The settings object is the core of microbootstrap.
All framework-related settings inherit from the BaseServiceSettings object. BaseServiceSettings defines parameters for the service and various instruments.
However, the number of parameters is <b>not confined</b> to those defined in BaseServiceSettings. You can add as many as you need.
These parameters can be sourced from your environment. By default, no prefix is added to these parameters.
Example:
class YourSettings(BaseServiceSettings):
service_debug: bool = True
service_name: str = "micro-service"
your_awesome_parameter: str = "really awesome"
... # Other settings here
To source your_awesome_parameter from the environment, set the environment variable named YOUR_AWESOME_PARAMETER.
If you prefer to use a prefix when sourcing parameters, set the ENVIRONMENT_PREFIX environment variable in advance.
Example:
$ export ENVIRONMENT_PREFIX=YOUR_PREFIX_
Then the settings object will attempt to source the variable named YOUR_PREFIX_YOUR_AWESOME_PARAMETER.
Service settings
Each settings object for every framework includes service parameters that can be utilized by various instruments.
You can configure them manually, or set the corresponding environment variables and let <b>microbootstrap</b> to source them automatically.
from microbootstrap.settings import BaseServiceSettings
class ServiceSettings(BaseServiceSettings):
service_debug: bool = True
service_environment: str | None = None
service_name: str = "micro-service"
service_description: str = "Micro service description"
service_version: str = "1.0.0"
... # Other settings here
Instruments
At present, the following instruments are supported for bootstrapping:
sentryprometheusopentelemetrypyroscopeloggingcorsswagger
Let's clarify the process required to bootstrap these instruments.
Sentry
To bootstrap Sentry, you must provide at least the sentry_dsn.
Additional parameters can also be supplied through the settings object.
from microbootstrap.settings import BaseServiceSettings
class YourSettings(BaseServiceSettings):
service_environment: str | None = None
sentry_dsn: str | None = None
sentry_traces_sample_rate: float | None = None
sentry_sample_rate: float = pydantic.Field(default=1.0, le=1.0, ge=0.0)
sentry_max_breadcrumbs: int = 15
sentry_max_value_length: int = 16384
sentry_attach_stacktrace: bool = True
sentry_integrations: list[Integration] = []
sentry_additional_params: dict[str, typing.Any] = {}
sentry_tags: dict[str, str] | None = None
sentry_opentelemetry_trace_url_template: str | None = None
... # Other settings here
These settings are subsequently passed to the sentry-sdk package, finalizing your Sentry integration.
Parameter descriptions:
service_environment- The environment name for Sentry events.sentry_dsn- The Data Source Name for your Sentry project.sentry_traces_sample_rate- The rate at which traces are sampled (via Sentry Tracing, not OpenTelemetry).sentry_sample_rate- The rate at which transactions are sampled.sentry_max_breadcrumbs- The maximum number of breadcrumbs to keep.sentry_max_value_length- The maximum length of values in Sentry events.sentry_attach_stacktrace- Whether to attach stacktraces to messages.sentry_integrations- A list of Sentry integrations to enable.sentry_additional_params- Additional parameters to pass to Sentry SDK.sentry_tags- Tags to apply to all Sentry events.sentry_opentelemetry_trace_url_template- Template for OpenTelemetry trace URLs to add to Sentry events (example:"https://example.com/traces/{trace_id}").
Prometheus
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.
FastAPI
To bootstrap prometheus you have to provide prometheus_metrics_path
from microbootstrap.settings import FastApiSettings
class YourSettings(FastApiSettings):
service_name: str
prometheus_metrics_path: str = "/metrics"
prometheus_metrics_include_in_schema: bool = False
prometheus_instrumentator_params: dict[str, typing.Any] = {}
prometheus_instrument_params: dict[str, typing.Any] = {}
prometheus_expose_params: dict[str, typing.Any] = {}
... # Other settings here
Parameters description:
service_name- will be attached to metrics's names, but has to be named in [snake_case](https://en.wikipedia.org/wiki/Snake_
