Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: GenericSQLAlchemyConfig.get_engine doesn't save created engine to self.engine_instance if self.engine_instance not exists #398

Open
1 of 4 tasks
alex-loktev opened this issue Feb 25, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@alex-loktev
Copy link

Description

Actually, I don't understand—is it a bug or a feature? I'm using this library for my app, and in my case, it's a bug.

I use OpenTelemetry's SQLAlchemyInstrumentor. To instrument SQLAlchemyInstrumentor, I need a SQLAlchemy engine. However, when I try to get the engine using GenericSQLAlchemyConfig.get_engine, it doesn't work correctly.

GenericSQLAlchemyConfig.get_engine is called twice: once during on_app_init and once when I try to get the engine. In this case, I need to provide my engine instance manually. Maybe we should store the created engine when using GenericSQLAlchemyConfig.get_engine?

URL to code causing the issue

No response

MCVE

def setup_plugins() -> list:
    plugins = []
    sqlalchemy_config = SQLAlchemyAsyncConfig(
        connection_string=str(ConfigInstance.env.db_url), # it's PostgresDSN
        engine_config=EngineConfig(
            poolclass=NullPool,
            connect_args={'statement_cache_size': 0},
        )
    )
    sqlalchemy_plugin = SQLAlchemyPlugin(
        config=sqlalchemy_config,
    )
    plugins.append(sqlalchemy_plugin)

    if ConfigInstance.env.app_environment == 'prod':
        SQLAlchemyInstrumentor().instrument(
            engine=sqlalchemy_config.get_engine().sync_engine
        )
        otel_plugin = OpenTelemetryPlugin(OpenTelemetryConfig())
        plugins.append(otel_plugin)
        
    return plugins

app = Litestar(
    route_handlers=controllers,
    plugins=setup_plugins(),
)

Steps to reproduce

Screenshots

No response

Logs

I added some logs to create_async_engine

INFO:     Started server process [22517]
INFO:     Waiting for application startup.
Created new engine instance (id=135510527018560)
Created new engine instance (id=135510525573632)
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Package Version

advanced_alchemy==0.30.3
sqlalchemy==2.0.38

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)
@alex-loktev alex-loktev added the bug Something isn't working label Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant