Skip to content

Commit

Permalink
Merge pull request #140 from salute-developers/PNLP-7493.log-env
Browse files Browse the repository at this point in the history
PNLP-7493: Log environment from template-config
  • Loading branch information
dangerink authored Sep 21, 2023
2 parents 2cf85f0 + 1e97f40 commit a5a2ac2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
19 changes: 19 additions & 0 deletions core/utils/singleton.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from __future__ import annotations
from typing import TypeVar, Optional, Type

T = TypeVar("T")


class Singleton(type):
Expand All @@ -7,3 +11,18 @@ 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]:
return SingletonOneInstance._instance
12 changes: 7 additions & 5 deletions smart_kit/configs/settings.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from __future__ import annotations

import asyncio
import os
import typing
from typing import Optional, Any, Union, List

import yaml

from core.configs.base_config import BaseConfig
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"

Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions smart_kit/utils/logger_writer/logger_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit a5a2ac2

Please sign in to comment.