Skip to content

Add a Hook to Execute Code Before Final Application Bootstrap #80

@jahermosilla

Description

@jahermosilla

Currently, the bootstrap method initializes the application and allows instruments to modify it in the bootstrap_after phase:

for instrument in self.instrument_box.instruments:
    if instrument.is_ready():
        application = instrument.bootstrap_after(application)

return self.bootstrap_after(application)

However, there is no way to execute code just before this step, after the application instance is created but before instruments modify it. This limitation makes it difficult to introduce additional setup or modifications that should take place at this stage.

One particular use case for this hook is the need to set up dependency injection and initialize SQLAlchemy instrumentator with the engine. See:

_container = modern_di_fastapi.setup_di(application)

with _container as container:
    engine = _engine.sync_resolve(container)

self.settings.opentelemetry_instrumentors.append(
    OpenTelemetryInstrumentor(
        SQLAlchemyInstrumentor(),
        {
            "enable_commenter": True,
            "commenter_options": {},
            "engine": engine
        },
    ),
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions