Skip to content

Commit 890b312

Browse files
[DOP-19796] - add log_url before sedning run task to celery worker (#110)
1 parent 19b23bf commit 890b312

File tree

15 files changed

+118
-14
lines changed

15 files changed

+118
-14
lines changed

.env.docker

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ ENV=LOCAL
44
# Debug
55
SYNCMASTER__SERVER__DEBUG=true
66

7-
# Logging
7+
# Logging Backend
88
SYNCMASTER__SERVER__LOGGING__SETUP=True
99
SYNCMASTER__SERVER__LOGGING__PRESET=colored
1010

11+
# Logging Worker
12+
SYNCMASTER__WORKER__LOGGING__SETUP=True
13+
SYNCMASTER__WORKER__LOGGING__PRESET=json
14+
1115
# Postgres
1216
SYNCMASTER__DATABASE__URL=postgresql+asyncpg://syncmaster:changeme@db:5432/syncmaster
1317

.env.local

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export SYNCMASTER__SERVER__DEBUG=true
88
export SYNCMASTER__SERVER__LOGGING__SETUP=True
99
export SYNCMASTER__SERVER__LOGGING__PRESET=colored
1010

11+
# Logging Worker
12+
export SYNCMASTER__WORKER__LOGGING__SETUP=True
13+
export SYNCMASTER__WORKER__LOGGING__PRESET=json
14+
1115
# Postgres
1216
export SYNCMASTER__DATABASE__URL=postgresql+asyncpg://syncmaster:changeme@localhost:5432/syncmaster
1317

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
.. _backend-configuration-logging:
1+
.. _configuration-logging:
22

33
Logging settings
44
================
55

66

7-
.. autopydantic_model:: syncmaster.settings.server.log.LoggingSettings
7+
.. autopydantic_model:: syncmaster.settings.log.LoggingSettings

poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ worker = [
9898
"jinja2",
9999
"psycopg",
100100
"uuid6",
101+
"coloredlogs",
102+
"python-json-logger",
101103
]
102-
103104
[tool.poetry.group.test.dependencies]
104105
pandas-stubs = "^2.2.2.240909"
105106
pytest = "^8.3.3"

syncmaster/backend/api/v1/runs.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
22
# SPDX-License-Identifier: Apache-2.0
33
from datetime import datetime
4+
from typing import Annotated
45

6+
from asgi_correlation_id import correlation_id
57
from fastapi import APIRouter, Depends, Query
8+
from jinja2 import Template
69
from kombu.exceptions import KombuError
710

11+
from syncmaster.backend.dependencies import Stub
812
from syncmaster.backend.services import UnitOfWork, get_user
913
from syncmaster.db.models import Status, User
1014
from syncmaster.db.utils import Permission
@@ -18,6 +22,7 @@
1822
ReadRunSchema,
1923
RunPageSchema,
2024
)
25+
from syncmaster.settings import Settings
2126
from syncmaster.worker.config import celery
2227

2328
router = APIRouter(tags=["Runs"], responses=get_error_responses())
@@ -77,6 +82,7 @@ async def read_run(
7782
@router.post("/runs")
7883
async def start_run(
7984
create_run_data: CreateRunSchema,
85+
settings: Annotated[Settings, Depends(Stub(Settings))],
8086
unit_of_work: UnitOfWork = Depends(UnitOfWork),
8187
current_user: User = Depends(get_user(is_active=True)),
8288
) -> ReadRunSchema:
@@ -112,6 +118,15 @@ async def start_run(
112118
target_creds=ReadAuthDataSchema(auth_data=credentials_target).dict(),
113119
)
114120

121+
log_url = Template(settings.worker.LOG_URL_TEMPLATE).render(
122+
run=run,
123+
correlation_id=correlation_id.get(),
124+
)
125+
run = await unit_of_work.run.update(
126+
run_id=run.id,
127+
log_url=log_url,
128+
)
129+
115130
try:
116131
celery.send_task(
117132
"run_transfer_task",

syncmaster/settings/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from syncmaster.settings.broker import RabbitMQSettings
1010
from syncmaster.settings.database import DatabaseSettings
1111
from syncmaster.settings.server import ServerSettings
12+
from syncmaster.settings.worker import WorkerSettings
1213

1314

1415
class EnvTypes(StrEnum):
@@ -48,16 +49,18 @@ class Settings(BaseSettings):
4849
SECURITY_ALGORITHM: str = "HS256"
4950
CRYPTO_KEY: str = "UBgPTioFrtH2unlC4XFDiGf5sYfzbdSf_VgiUSaQc94="
5051

51-
CORRELATION_CELERY_HEADER_ID: str = "CORRELATION_CELERY_HEADER_ID"
52-
5352
TOKEN_EXPIRED_TIME: int = 60 * 60 * 10 # 10 hours
5453
CREATE_SPARK_SESSION_FUNCTION: ImportString = "syncmaster.worker.spark.get_worker_spark_session"
5554

5655
database: DatabaseSettings = Field(description=":ref:`Database settings <backend-configuration-database>`")
5756
broker: RabbitMQSettings = Field(description=":ref:`Broker settings <backend-configuration-broker>`")
5857
server: ServerSettings = Field(
5958
default_factory=ServerSettings,
60-
description=":ref:`Server settings <backend-configuration>`",
59+
description="Server settings <backend-configuration",
60+
)
61+
worker: WorkerSettings = Field(
62+
default_factory=WorkerSettings,
63+
description="Celery worker settings",
6164
)
6265

6366
class Config:

syncmaster/settings/server/log/__init__.py renamed to syncmaster/settings/log/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,26 @@ class LoggingSettings(BaseModel):
2020
2121
.. code-block:: bash
2222
23+
# backend logging
2324
SYNCMASTER__SERVER__LOGGING__SETUP=True
2425
SYNCMASTER__SERVER__LOGGING__PRESET=json
2526
27+
# celery logging
28+
SYNCMASTER__WORKER__LOGGING__SETUP=True
29+
SYNCMASTER__WORKER__LOGGING__PRESET=json
30+
2631
Passing custom logging config file:
2732
2833
.. code-block:: bash
2934
35+
# backend logging
3036
SYNCMASTER__SERVER__LOGGING__SETUP=True
3137
SYNCMASTER__SERVER__LOGGING__CUSTOM__CONFIG_PATH=/some/logging.yml
3238
39+
# celery logging
40+
SYNCMASTER__WORKER__LOGGING__SETUP=True
41+
SYNCMASTER__WORKER__LOGGING__CUSTOM__CONFIG_PATH=/some/logging.yml
42+
3343
Setup logging in some other way, e.g. using `uvicorn args <https://www.uvicorn.org/settings/#logging>`_:
3444
3545
.. code-block:: bash

syncmaster/settings/server/log/colored.yml renamed to syncmaster/settings/log/colored.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ handlers:
2424
formatter: colored
2525
filters: [correlation_id]
2626
stream: ext://sys.stdout
27+
celery:
28+
class: logging.StreamHandler
29+
formatter: colored
30+
filters: [correlation_id]
31+
stream: ext://sys.stdout
2732

2833
loggers:
2934
'':
@@ -34,3 +39,7 @@ loggers:
3439
handlers: [main]
3540
level: INFO
3641
propagate: false
42+
celery:
43+
level: DEBUG
44+
handlers: [celery]
45+
propagate: false
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ handlers:
2323
formatter: json
2424
filters: [correlation_id]
2525
stream: ext://sys.stdout
26+
celery:
27+
class: logging.StreamHandler
28+
formatter: json
29+
filters: [correlation_id]
30+
stream: ext://sys.stdout
2631

2732
loggers:
2833
'':
@@ -33,3 +38,7 @@ loggers:
3338
handlers: [main]
3439
level: INFO
3540
propagate: false
41+
celery:
42+
level: DEBUG
43+
handlers: [celery]
44+
propagate: false

0 commit comments

Comments
 (0)