Skip to content

Commit 2588e4e

Browse files
committed
set a suffix
1 parent 97cb753 commit 2588e4e

File tree

14 files changed

+58
-32
lines changed

14 files changed

+58
-32
lines changed

packages/service-library/src/servicelib/db_asyncpg_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ async def create_async_engine_and_database_ready(
3131
)
3232

3333
server_settings = {
34-
"application_name": settings.client_name(f"{application_name}-asyncpg"),
34+
"application_name": settings.client_name(
35+
f"{application_name}", suffix="asyncpg"
36+
),
3537
}
3638

3739
engine = create_async_engine(
@@ -68,7 +70,7 @@ async def check_postgres_liveness(engine: AsyncEngine) -> LivenessResult:
6870

6971
@contextlib.asynccontextmanager
7072
async def with_async_pg_engine(
71-
settings: PostgresSettings,
73+
settings: PostgresSettings, *, application_name: str
7274
) -> AsyncIterator[AsyncEngine]:
7375
"""
7476
Creates an asyncpg engine and ensures it is properly closed after use.
@@ -80,7 +82,9 @@ async def with_async_pg_engine(
8082
f"connection to db {settings.dsn_with_async_sqlalchemy}",
8183
):
8284
server_settings = {
83-
"application_name": settings.client_name("-asyncpg"),
85+
"application_name": settings.client_name(
86+
application_name, suffix="asyncpg"
87+
),
8488
}
8589

8690
engine = create_async_engine(

packages/settings-library/src/settings_library/postgres.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,18 @@ def dsn_with_async_sqlalchemy(self) -> str:
8282
)
8383
return f"{url}"
8484

85-
def dsn_with_query(self, application_name: str) -> str:
85+
def dsn_with_query(self, application_name: str, *, suffix: str | None) -> str:
8686
"""Some clients do not support queries in the dsn"""
8787
dsn = self.dsn
88-
return self._update_query(dsn, application_name)
88+
return self._update_query(dsn, application_name, suffix=suffix)
8989

90-
def client_name(self, application_name: str) -> str:
91-
return f"{application_name}{'-' if self.POSTGRES_CLIENT_NAME else ''}{self.POSTGRES_CLIENT_NAME or ''}"
90+
def client_name(self, application_name: str, *, suffix: str | None) -> str:
91+
return f"{application_name}{'-' if self.POSTGRES_CLIENT_NAME else ''}{self.POSTGRES_CLIENT_NAME or ''}{'-' + suffix if suffix else ''}"
9292

93-
def _update_query(self, uri: str, application_name: str) -> str:
93+
def _update_query(self, uri: str, application_name: str, suffix: str | None) -> str:
9494
# SEE https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
9595
new_params: dict[str, str] = {
96-
"application_name": self.client_name(application_name),
96+
"application_name": self.client_name(application_name, suffix=suffix),
9797
}
9898

9999
if new_params:

packages/settings-library/tests/test_postgres.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def mock_environment(mock_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPa
2525

2626

2727
def test_cached_property_dsn(mock_environment: EnvVarsDict):
28-
2928
settings = PostgresSettings.create_from_envs()
3029

3130
# all are upper-case
@@ -46,8 +45,8 @@ def test_dsn_with_query(
4645
assert settings.dsn == "postgresql://foo:secret@localhost:5432/foodb"
4746
app_name = faker.pystr()
4847
assert (
49-
settings.dsn_with_query(app_name)
50-
== f"postgresql://foo:secret@localhost:5432/foodb?application_name={app_name}-Some+%2643+funky+name"
48+
settings.dsn_with_query(app_name, suffix="my-suffix")
49+
== f"postgresql://foo:secret@localhost:5432/foodb?application_name={app_name}-Some+%2643+funky+name-my-suffix"
5150
)
5251

5352
with monkeypatch.context() as patch:
@@ -56,7 +55,7 @@ def test_dsn_with_query(
5655

5756
assert not settings.POSTGRES_CLIENT_NAME
5857
assert f"{settings.dsn}?application_name=blah" == settings.dsn_with_query(
59-
"blah"
58+
"blah", suffix=None
6059
)
6160

6261

services/api-server/src/simcore_service_api_server/clients/postgres.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from servicelib.fastapi.lifespan_utils import LifespanOnStartupError
44
from sqlalchemy.ext.asyncio import AsyncEngine
55

6+
from .._meta import APP_NAME
67
from ..core.settings import ApplicationSettings
78

89

910
class PostgresNotConfiguredError(LifespanOnStartupError):
1011
msg_template = LifespanOnStartupError.msg_template + (
11-
"Postgres settings are not configured. "
12-
"Please check your application settings. "
12+
"Postgres settings are not configured. Please check your application settings. "
1313
)
1414

1515

@@ -30,7 +30,9 @@ async def _on_startup() -> None:
3030
settings=settings,
3131
)
3232

33-
await connect_to_db(app, settings.API_SERVER_POSTGRES)
33+
await connect_to_db(
34+
app, settings.API_SERVER_POSTGRES, application_name=APP_NAME
35+
)
3436
assert app.state.engine # nosec
3537
assert isinstance(app.state.engine, AsyncEngine) # nosec
3638

services/director-v2/src/simcore_service_director_v2/modules/db/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
from servicelib.fastapi.db_asyncpg_engine import get_engine as get_db_engine
77
from settings_library.postgres import PostgresSettings
88

9+
from ..._meta import APP_NAME
10+
911

1012
def setup(app: FastAPI, settings: PostgresSettings) -> None:
1113
async def on_startup() -> None:
12-
await connect_to_db(app, settings)
14+
await connect_to_db(app, settings, application_name=APP_NAME)
1315

1416
async def on_shutdown() -> None:
1517
await close_db_connection(app)

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/_meta.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
""" Package Metadata
2-
3-
"""
1+
"""Package Metadata"""
42

53
from importlib.metadata import distribution, version
64

75
_current_distribution = distribution("simcore-service-dynamic-sidecar")
86

7+
98
PROJECT_NAME: str = _current_distribution.metadata["Name"]
109

1110
API_VERSION: str = version("simcore-service-dynamic-sidecar")

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/modules/database.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from servicelib.db_asyncpg_utils import check_postgres_liveness, with_async_pg_engine
33
from settings_library.postgres import PostgresSettings
44

5+
from .._meta import PROJECT_NAME
56
from ..core.settings import ApplicationSettings
67
from .service_liveness import (
78
wait_for_service_liveness,
@@ -17,7 +18,9 @@ async def wait_for_database_liveness(app: FastAPI) -> None:
1718
assert isinstance(app_settings, ApplicationSettings) # nosec
1819
postgres_settings = app_settings.POSTGRES_SETTINGS
1920
assert isinstance(postgres_settings, PostgresSettings) # nosec
20-
async with with_async_pg_engine(postgres_settings) as engine:
21+
async with with_async_pg_engine(
22+
postgres_settings, application_name=PROJECT_NAME
23+
) as engine:
2124
await wait_for_service_liveness(
2225
check_postgres_liveness,
2326
engine,

services/efs-guardian/src/simcore_service_efs_guardian/services/modules/db.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from fastapi import FastAPI
22
from servicelib.fastapi.db_asyncpg_engine import close_db_connection, connect_to_db
33

4+
from ..._meta import APP_NAME
5+
46

57
def setup(app: FastAPI):
68
async def on_startup() -> None:
7-
await connect_to_db(app, app.state.settings.EFS_GUARDIAN_POSTGRES)
9+
await connect_to_db(
10+
app, app.state.settings.EFS_GUARDIAN_POSTGRES, application_name=APP_NAME
11+
)
812

913
async def on_shutdown() -> None:
1014
await close_db_connection(app)

services/payments/src/simcore_service_payments/services/postgres.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from servicelib.fastapi.db_asyncpg_engine import close_db_connection, connect_to_db
33
from sqlalchemy.ext.asyncio import AsyncEngine
44

5+
from .._meta import APP_NAME
56
from ..core.settings import ApplicationSettings
67

78

@@ -16,7 +17,7 @@ def setup_postgres(app: FastAPI):
1617

1718
async def _on_startup() -> None:
1819
settings: ApplicationSettings = app.state.settings
19-
await connect_to_db(app, settings.PAYMENTS_POSTGRES)
20+
await connect_to_db(app, settings.PAYMENTS_POSTGRES, application_name=APP_NAME)
2021
assert app.state.engine # nosec
2122
assert isinstance(app.state.engine, AsyncEngine) # nosec
2223

services/resource-usage-tracker/src/simcore_service_resource_usage_tracker/services/modules/db/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from servicelib.fastapi.db_asyncpg_engine import close_db_connection, connect_to_db
55
from servicelib.logging_utils import log_context
66

7+
from ...._meta import APP_NAME
8+
79
_logger = logging.getLogger(__name__)
810

911

@@ -14,7 +16,11 @@ async def on_startup() -> None:
1416
logging.INFO,
1517
msg="RUT startup DB",
1618
):
17-
await connect_to_db(app, app.state.settings.RESOURCE_USAGE_TRACKER_POSTGRES)
19+
await connect_to_db(
20+
app,
21+
app.state.settings.RESOURCE_USAGE_TRACKER_POSTGRES,
22+
application_name=APP_NAME,
23+
)
1824

1925
async def on_shutdown() -> None:
2026
with log_context(

0 commit comments

Comments
 (0)