Skip to content

Commit

Permalink
Fix logs not propagated to azure
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonSohoel committed Dec 13, 2024
1 parent ca1c51f commit 4958390
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/ert/services/_storage_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

from opentelemetry.trace.span import Span

DARK_STORAGE_APP = "ert.dark_storage.app:app"


class Server(uvicorn.Server):
def __init__(
Expand Down Expand Up @@ -84,7 +86,7 @@ def _create_connection_info(sock: socket.socket, authtoken: str) -> Dict[str, An

return connection_info

def run_server(args: Optional[argparse.Namespace] = None, debug: bool = False) -> None:
def run_server(args: Optional[argparse.Namespace] = None, debug: bool = False, uvicorn_config = None) -> None:
trace_id = get_trace_id()
if args is None:
args = parse_args()
Expand All @@ -106,7 +108,7 @@ def run_server(args: Optional[argparse.Namespace] = None, debug: bool = False) -
# Appropriated from uvicorn.main:run
os.environ["ERT_STORAGE_NO_TOKEN"] = "1"
os.environ["ERT_STORAGE_ENS_PATH"] = os.path.abspath(args.project)
config = uvicorn.Config("ert.dark_storage.app:app", **config_args)
config = uvicorn.Config(DARK_STORAGE_APP, **config_args) if uvicorn_config is None else uvicorn_config #uvicorn.Config() resets the logging config (overriding additional handlers added to loggers like e.g. the ert_azurelogger handler added through the pluggin system
server = Server(config, json.dumps(connection_info))

logger = logging.getLogger("ert.shared.storage.info")
Expand Down Expand Up @@ -150,12 +152,18 @@ def check_parent_alive() -> bool:


def main():
args = parse_args()
config_args: Dict[str, Any] = {}
with open(STORAGE_LOG_CONFIG, encoding="utf-8") as conf_file:
logging_conf = yaml.safe_load(conf_file)
logging.config.dictConfig(logging_conf)
config_args.update(log_config=logging_conf)
warnings.filterwarnings("ignore", category=DeprecationWarning)
uvicorn.config.LOGGING_CONFIG.clear()
uvicorn.config.LOGGING_CONFIG.update(logging_conf)

if args.debug:
config_args.update(reload=True, reload_dirs=[os.path.dirname(ert_shared_path)])
uvicorn_config = uvicorn.Config(DARK_STORAGE_APP, **config_args) # Need to run uvicorn.Config before entering the ErtPluginContext because uvicorn.Config overrides the configuration of existing loggers, thus removing log handlers added by ErtPluginContext

_stopped = threading.Event()
terminate_on_parent_death_thread = threading.Thread(
target=terminate_on_parent_death, args=[_stopped, 1.0]
Expand All @@ -166,7 +174,7 @@ def main():
try:
current_span = trace.get_current_span()
print(f"Opertation ID: {get_trace_id()}")
run_server(debug=False)
run_server(args, debug=False, uvicorn_config = uvicorn_config)
except BaseException as err:
print(f"Stopped with exception {err}")
finally:
Expand Down

0 comments on commit 4958390

Please sign in to comment.