Skip to content

Commit

Permalink
Set current span with decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonSohoel committed Oct 30, 2024
1 parent 41fb8ef commit e1970bb
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions src/ert/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from ert.run_models.multiple_data_assimilation import MultipleDataAssimilation
from ert.services import StorageService, WebvizErt
from ert.shared.storage.command import add_parser_options as ert_api_add_parser_options
from ert.trace import tracer, tracer_provider
from ert.trace import trace, tracer, tracer_provider
from ert.validation import (
IntegerArgument,
NumberListStringArgument,
Expand Down Expand Up @@ -649,7 +649,9 @@ def log_process_usage() -> None:
)


@tracer.start_as_current_span("ert.application.start")
def main() -> None:
span = trace.get_current_span()
warnings.filterwarnings("ignore", category=DeprecationWarning)
locale.setlocale(locale.LC_NUMERIC, "C")

Expand Down Expand Up @@ -680,43 +682,41 @@ def main() -> None:
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
root_logger.addHandler(handler)

with tracer.start_as_current_span("ert.application.start") as span:
try:
with ErtPluginContext(
logger=logging.getLogger(), trace_provider=tracer_provider
) as context:
logger.info(f"Running ert with {args}")
args.func(args, context.plugin_manager)
except ErtCliError as err:
span.set_status(Status(StatusCode.ERROR))
span.record_exception(err)
logger.debug(str(err))
sys.exit(str(err))
except ConfigValidationError as err:
span.set_status(Status(StatusCode.ERROR))
span.record_exception(err)
err_msg = err.cli_message()
logger.debug(err_msg)
sys.exit(err_msg)
except BaseException as err:
span.set_status(Status(StatusCode.ERROR))
span.record_exception(err)
logger.exception(f'ERT crashed unexpectedly with "{err}"')

logfiles = set() # Use set to avoid duplicates...
for loghandler in logging.getLogger().handlers:
if isinstance(loghandler, logging.FileHandler):
logfiles.add(loghandler.baseFilename)

msg = f'ERT crashed unexpectedly with "{err}".\nSee logfile(s) for details:'
msg += "\n " + "\n ".join(logfiles)

sys.exit(msg)
finally:
log_process_usage()
os.environ.pop("ERT_LOG_DIR")
ThreadingInstrumentor().uninstrument()
try:
with ErtPluginContext(
logger=logging.getLogger(), trace_provider=tracer_provider
) as context:
logger.info(f"Running ert with {args}")
args.func(args, context.plugin_manager)
except ErtCliError as err:
span.set_status(Status(StatusCode.ERROR))
span.record_exception(err)
logger.debug(str(err))
sys.exit(str(err))
except ConfigValidationError as err:
span.set_status(Status(StatusCode.ERROR))
span.record_exception(err)
err_msg = err.cli_message()
logger.debug(err_msg)
sys.exit(err_msg)
except BaseException as err:
span.set_status(Status(StatusCode.ERROR))
span.record_exception(err)
logger.exception(f'ERT crashed unexpectedly with "{err}"')

logfiles = set() # Use set to avoid duplicates...
for loghandler in logging.getLogger().handlers:
if isinstance(loghandler, logging.FileHandler):
logfiles.add(loghandler.baseFilename)

msg = f'ERT crashed unexpectedly with "{err}".\nSee logfile(s) for details:'
msg += "\n " + "\n ".join(logfiles)

sys.exit(msg)
finally:
log_process_usage()
os.environ.pop("ERT_LOG_DIR")
ThreadingInstrumentor().uninstrument()


if __name__ == "__main__":
Expand Down

0 comments on commit e1970bb

Please sign in to comment.