Skip to content

Commit 3b31ad9

Browse files
committed
instrument logging directly when logging is setup
1 parent f98422d commit 3b31ad9

File tree

22 files changed

+48
-10
lines changed
  • packages/service-library/src/servicelib
  • services
    • agent/src/simcore_service_agent/core
    • api-server/src/simcore_service_api_server/core
    • autoscaling/src/simcore_service_autoscaling
    • catalog/src/simcore_service_catalog
    • clusters-keeper/src/simcore_service_clusters_keeper
    • dask-sidecar/src/simcore_service_dask_sidecar
    • datcore-adapter/src/simcore_service_datcore_adapter/core
    • director-v2/src/simcore_service_director_v2/core
    • director/src/simcore_service_director
    • dynamic-scheduler/src/simcore_service_dynamic_scheduler
    • dynamic-sidecar/src/simcore_service_dynamic_sidecar/core
    • efs-guardian/src/simcore_service_efs_guardian
    • invitations/src/simcore_service_invitations
    • payments/src/simcore_service_payments
    • resource-usage-tracker/src/simcore_service_resource_usage_tracker
    • storage/src/simcore_service_storage
    • web/server

22 files changed

+48
-10
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from pathlib import Path
1717
from typing import Any, NotRequired, TypeAlias, TypedDict, TypeVar
1818

19+
from servicelib.tracing import setup_log_tracing
20+
from settings_library.tracing import TracingSettings
21+
1922
from .logging_utils_filtering import GeneralLogFilter, LoggerName, MessageSubstring
2023
from .utils_secrets import mask_sensitive_data
2124

@@ -80,12 +83,8 @@ def format(self, record) -> str:
8083

8184

8285
# SEE https://docs.python.org/3/library/logging.html#logrecord-attributes
83-
DEFAULT_FORMATTING = (
84-
"log_level=%(levelname)s | log_timestamp=%(asctime)s | log_source=%(name)s:%(funcName)s(%(lineno)d) | log_uid=%(log_uid)s "
85-
"| trace_id=%(otelTraceID)s | span_id=%(otelSpanID)s | resource.service.name=%(otelServiceName)s | trace_sampled=%(otelTraceSampled)s] "
86-
"| log_msg=%(message)s"
87-
)
88-
LOCAL_FORMATTING = "%(levelname)s: [%(asctime)s/%(processName)s] [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s resource.service.name=%(otelServiceName)s trace_sampled=%(otelTraceSampled)s] [%(name)s:%(funcName)s(%(lineno)d)] - %(message)s"
86+
DEFAULT_FORMATTING = "log_level=%(levelname)s | log_timestamp=%(asctime)s | log_source=%(name)s:%(funcName)s(%(lineno)d) | log_uid=%(log_uid)s | log_msg=%(message)s"
87+
LOCAL_FORMATTING = "%(levelname)s: [%(asctime)s/%(processName)s] [%(name)s:%(funcName)s(%(lineno)d)] - %(message)s"
8988

9089
# Graylog Grok pattern extractor:
9190
# log_level=%{WORD:log_level} \| log_timestamp=%{TIMESTAMP_ISO8601:log_timestamp} \| log_source=%{DATA:log_source} \| log_msg=%{GREEDYDATA:log_msg}
@@ -95,20 +94,30 @@ def config_all_loggers(
9594
*,
9695
log_format_local_dev_enabled: bool,
9796
logger_filter_mapping: dict[LoggerName, list[MessageSubstring]],
97+
tracing_settings: TracingSettings | None,
9898
) -> None:
9999
"""
100100
Applies common configuration to ALL registered loggers
101101
"""
102-
fmt = DEFAULT_FORMATTING
103102
the_manager: logging.Manager = logging.Logger.manager
104103
root_logger = logging.getLogger()
105104

106105
loggers = [root_logger] + [
107106
logging.getLogger(name) for name in the_manager.loggerDict
108107
]
109108

109+
fmt = DEFAULT_FORMATTING
110+
if tracing_settings is not None:
111+
fmt = (
112+
"log_level=%(levelname)s | log_timestamp=%(asctime)s | log_source=%(name)s:%(funcName)s(%(lineno)d) | log_uid=%(log_uid)s "
113+
"| trace_id=%(otelTraceID)s | span_id=%(otelSpanID)s | resource.service.name=%(otelServiceName)s | trace_sampled=%(otelTraceSampled)s] "
114+
"| log_msg=%(message)s"
115+
)
116+
setup_log_tracing(tracing_settings=tracing_settings)
110117
if log_format_local_dev_enabled:
111118
fmt = LOCAL_FORMATTING
119+
if tracing_settings is not None:
120+
fmt = "%(levelname)s: [%(asctime)s/%(processName)s] [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s resource.service.name=%(otelServiceName)s trace_sampled=%(otelTraceSampled)s] [%(name)s:%(funcName)s(%(lineno)d)] - %(message)s"
112121

113122
for logger in loggers:
114123
_set_logging_handler(

services/agent/src/simcore_service_agent/core/application.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def _setup_logger(settings: ApplicationSettings):
3333
config_all_loggers(
3434
log_format_local_dev_enabled=settings.AGENT_VOLUMES_LOG_FORMAT_LOCAL_DEV_ENABLED,
3535
logger_filter_mapping=settings.AGENT_VOLUMES_LOG_FILTER_MAPPING,
36+
tracing_settings=settings.AGENT_TRACING,
3637
)
3738

3839

services/api-server/src/simcore_service_api_server/core/application.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def init_app(settings: ApplicationSettings | None = None) -> FastAPI:
5454
config_all_loggers(
5555
log_format_local_dev_enabled=settings.API_SERVER_LOG_FORMAT_LOCAL_DEV_ENABLED,
5656
logger_filter_mapping=settings.API_SERVER_LOG_FILTER_MAPPING,
57+
tracing_settings=settings.API_SERVER_TRACING,
5758
)
5859
_logger.debug("App settings:\n%s", settings.model_dump_json(indent=2))
5960

services/autoscaling/src/simcore_service_autoscaling/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
config_all_loggers(
1616
log_format_local_dev_enabled=the_settings.AUTOSCALING_LOG_FORMAT_LOCAL_DEV_ENABLED,
1717
logger_filter_mapping=the_settings.AUTOSCALING_LOG_FILTER_MAPPING,
18+
tracing_settings=the_settings.AUTOSCALING_TRACING,
1819
)
1920

2021
# SINGLETON FastAPI app

services/catalog/src/simcore_service_catalog/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
config_all_loggers(
1717
log_format_local_dev_enabled=_the_settings.CATALOG_LOG_FORMAT_LOCAL_DEV_ENABLED,
1818
logger_filter_mapping=_the_settings.CATALOG_LOG_FILTER_MAPPING,
19+
tracing_settings=_the_settings.CATALOG_TRACING,
1920
)
2021

2122

services/clusters-keeper/src/simcore_service_clusters_keeper/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
config_all_loggers(
1616
log_format_local_dev_enabled=the_settings.CLUSTERS_KEEPER_LOG_FORMAT_LOCAL_DEV_ENABLED,
1717
logger_filter_mapping=the_settings.CLUSTERS_KEEPER_LOG_FILTER_MAPPING,
18+
tracing_settings=the_settings.CLUSTERS_KEEPER_TRACING,
1819
)
1920

2021
# SINGLETON FastAPI app

services/dask-sidecar/src/simcore_service_dask_sidecar/tasks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ async def dask_setup(worker: distributed.Worker) -> None:
6565
config_all_loggers(
6666
log_format_local_dev_enabled=settings.DASK_LOG_FORMAT_LOCAL_DEV_ENABLED,
6767
logger_filter_mapping=settings.DASK_LOG_FILTER_MAPPING,
68+
tracing_settings=None, # no tracing for dask sidecar
6869
)
6970

7071
logger.info("Setting up worker...")

services/datcore-adapter/src/simcore_service_datcore_adapter/core/application.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def create_app(settings: ApplicationSettings | None = None) -> FastAPI:
4242
config_all_loggers(
4343
log_format_local_dev_enabled=settings.DATCORE_ADAPTER_LOG_FORMAT_LOCAL_DEV_ENABLED,
4444
logger_filter_mapping=settings.DATCORE_ADAPTER_LOG_FILTER_MAPPING,
45+
tracing_settings=settings.DATCORE_ADAPTER_TRACING,
4546
)
4647

4748
# keep mostly quiet noisy loggers

services/director-v2/src/simcore_service_director_v2/core/application.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def create_base_app(settings: AppSettings | None = None) -> FastAPI:
115115
config_all_loggers(
116116
log_format_local_dev_enabled=settings.DIRECTOR_V2_LOG_FORMAT_LOCAL_DEV_ENABLED,
117117
logger_filter_mapping=settings.DIRECTOR_V2_LOG_FILTER_MAPPING,
118+
tracing_settings=settings.DIRECTOR_V2_TRACING,
118119
)
119120
_logger.debug(settings.model_dump_json(indent=2))
120121

services/director/src/simcore_service_director/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
config_all_loggers(
1818
log_format_local_dev_enabled=_the_settings.DIRECTOR_LOG_FORMAT_LOCAL_DEV_ENABLED,
1919
logger_filter_mapping=_the_settings.DIRECTOR_LOG_FILTER_MAPPING,
20+
tracing_settings=_the_settings.DIRECTOR_TRACING,
2021
)
2122

2223
# SINGLETON FastAPI app

0 commit comments

Comments
 (0)