From e8eb9208c25d964158766a58d7a2aa897da777af Mon Sep 17 00:00:00 2001 From: Artem Batalov Date: Wed, 30 Aug 2023 13:59:16 +0300 Subject: [PATCH 1/3] PNLP-7493: Log environment from template-config --- smart_kit/configs/settings.py | 14 +++++++++++--- smart_kit/utils/logger_writer/logger_formatter.py | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/smart_kit/configs/settings.py b/smart_kit/configs/settings.py index a1d7bc37..aa312235 100644 --- a/smart_kit/configs/settings.py +++ b/smart_kit/configs/settings.py @@ -1,6 +1,8 @@ +from __future__ import annotations + import asyncio import os -import typing +from typing import Optional, Any, Union, List import yaml @@ -43,7 +45,7 @@ def override_repositories(self, repositories: list): """ return repositories - def _load_base_repositories(self) -> typing.List[FileRepository]: + def _load_base_repositories(self) -> List[FileRepository]: """Load base repositories with service settings""" template_settings_repo = UpdatableFileRepository( self.subfolder_path("template_config.yml"), loader=yaml.safe_load, key="template_settings") @@ -67,7 +69,7 @@ def _load_base_repositories(self) -> typing.List[FileRepository]: ] def _get_kafka_settings_filepath( - self, filename: typing.Any, use_secrets_path: bool = True) -> typing.Union[bytes, str]: + self, filename: Any, use_secrets_path: bool = True) -> Union[bytes, str]: """Возвращает путь к файлу с настройками кафки. По умолчанию возвращает путь к файлу в секретах.""" if use_secrets_path: return self.subfolder_secret_path(filename) @@ -97,3 +99,9 @@ def get_source(self): self._source = source return source + + @classmethod + def get_instance(cls) -> Optional[Settings]: + if cls._instances: + return next(iter(cls._instances.values())) + return None diff --git a/smart_kit/utils/logger_writer/logger_formatter.py b/smart_kit/utils/logger_writer/logger_formatter.py index 9c4b99a7..86924bd3 100644 --- a/smart_kit/utils/logger_writer/logger_formatter.py +++ b/smart_kit/utils/logger_writer/logger_formatter.py @@ -7,6 +7,7 @@ import logging from core.model.factory import build_factory from core.model.registered import Registered +from smart_kit.configs.settings import Settings def to_num(s): @@ -54,6 +55,8 @@ def add_fields(self, log_record, record, message_dict): log_record["nlpf_version"] = self.NLPF_VERSION log_record["team"] = self.DEV_TEAM log_record["application"] = self.APPLICATION_NAME + if settings := Settings.get_instance(): + log_record["environment"] = settings.get("template_settings").get("environment") if isinstance(record.args, dict): log_record["args"] = self._check_fields(record.args) From ff35d91bf74fe5620353fd6ef577bec6141988ec Mon Sep 17 00:00:00 2001 From: Artem Batalov Date: Wed, 13 Sep 2023 11:18:43 +0300 Subject: [PATCH 2/3] SingletonOneInstance class --- core/utils/singleton.py | 21 +++++++++++++++++++++ smart_kit/configs/settings.py | 9 ++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/core/utils/singleton.py b/core/utils/singleton.py index 852543d6..55daea03 100644 --- a/core/utils/singleton.py +++ b/core/utils/singleton.py @@ -1,3 +1,7 @@ +from __future__ import annotations +from typing import TypeVar, Optional, Type + +T = TypeVar("T") class Singleton(type): @@ -7,3 +11,20 @@ def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] + + def get_instance(cls: Type[T]) -> Optional[T]: + return cls._instances.get(cls) + + +class SingletonOneInstance(type): + _instance = None + + def __call__(cls, *args, **kwargs): + if SingletonOneInstance._instance is None: + SingletonOneInstance._instance = super(SingletonOneInstance, cls).__call__(*args, **kwargs) + return SingletonOneInstance._instance + + def get_instance(cls: Type[T]) -> Optional[T]: + if SingletonOneInstance._instance: + return cls.__call__() + return None diff --git a/smart_kit/configs/settings.py b/smart_kit/configs/settings.py index aa312235..45e1b3fd 100644 --- a/smart_kit/configs/settings.py +++ b/smart_kit/configs/settings.py @@ -10,10 +10,10 @@ from core.db_adapter.ceph.ceph_adapter import CephAdapter from core.db_adapter.os_adapter import OSAdapter from core.repositories.file_repository import UpdatableFileRepository, FileRepository -from core.utils.singleton import Singleton +from core.utils.singleton import SingletonOneInstance -class Settings(BaseConfig, metaclass=Singleton): +class Settings(BaseConfig, metaclass=SingletonOneInstance): CephAdapterKey = "ceph" OSAdapterKey = "os" @@ -100,8 +100,3 @@ def get_source(self): self._source = source return source - @classmethod - def get_instance(cls) -> Optional[Settings]: - if cls._instances: - return next(iter(cls._instances.values())) - return None From 1e97f40909645e26f414f9b25f2071e136beeff6 Mon Sep 17 00:00:00 2001 From: Artem Batalov Date: Wed, 13 Sep 2023 19:15:27 +0300 Subject: [PATCH 3/3] simplify --- core/utils/singleton.py | 4 +--- smart_kit/configs/settings.py | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/core/utils/singleton.py b/core/utils/singleton.py index 55daea03..2a823b55 100644 --- a/core/utils/singleton.py +++ b/core/utils/singleton.py @@ -25,6 +25,4 @@ def __call__(cls, *args, **kwargs): return SingletonOneInstance._instance def get_instance(cls: Type[T]) -> Optional[T]: - if SingletonOneInstance._instance: - return cls.__call__() - return None + return SingletonOneInstance._instance diff --git a/smart_kit/configs/settings.py b/smart_kit/configs/settings.py index 45e1b3fd..c29f81d8 100644 --- a/smart_kit/configs/settings.py +++ b/smart_kit/configs/settings.py @@ -99,4 +99,3 @@ def get_source(self): self._source = source return source -