Skip to content

Commit

Permalink
Add span for run model and run ensemble
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonSohoel committed Oct 30, 2024
1 parent 2c58ebd commit 0a5b658
Showing 1 changed file with 83 additions and 45 deletions.
128 changes: 83 additions & 45 deletions src/ert/run_models/base_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
)

import numpy as np
from opentelemetry import trace
from opentelemetry.trace import Status, StatusCode

from _ert.events import (
EESnapshot,
Expand Down Expand Up @@ -323,51 +325,85 @@ def start_simulations_thread(
evaluator_server_config: EvaluatorServerConfig,
restart: bool = False,
) -> None:
failed = False
exception: Optional[Exception] = None
error_messages: MutableSequence[str] = []
try:
self.start_time = int(time.time())
self.stop_time = None
with captured_logs(error_messages):
self._set_default_env_context()
self.run_experiment(
evaluator_server_config=evaluator_server_config,
restart=restart,
tracer = trace.get_tracer("ert.main")
with tracer.start_as_current_span("ert.run_model.start") as span:
failed = False
exception: Optional[Exception] = None
error_messages: MutableSequence[str] = []
try:
span.add_event(
"log",
{
"log.severity": "info",
"log.message": f"Starting simulation thread {self.__class__.__name__}",
},
)
if self._completed_realizations_mask:
combined = np.logical_or(
np.array(self._completed_realizations_mask),
np.array(self.active_realizations),
self.start_time = int(time.time())
self.stop_time = None
with captured_logs(error_messages):
self._set_default_env_context()
self.run_experiment(
evaluator_server_config=evaluator_server_config,
restart=restart,
)
self._completed_realizations_mask = list(combined)
else:
self._completed_realizations_mask = copy.copy(
self.active_realizations
if self._completed_realizations_mask:
combined = np.logical_or(
np.array(self._completed_realizations_mask),
np.array(self.active_realizations),
)
self._completed_realizations_mask = list(combined)
else:
self._completed_realizations_mask = copy.copy(
self.active_realizations
)
except ErtRunError as e:
span.set_status(Status(StatusCode.ERROR))
span.record_exception(e)
span.add_event(
"log",
{
"log.severity": "exception",
"log.message": f'Simulation ended with error "{e}"',
},
)
self._completed_realizations_mask = []
failed = True
exception = e
except UserWarning as e:
span.record_exception(e)
span.add_event(
"log",
{
"log.severity": "exception",
"log.message": f'Simulation ended with warning "{e}"',
},
)
except Exception as e:
span.set_status(Status(StatusCode.ERROR))
span.record_exception(e)
span.add_event(
"log",
{
"log.severity": "exception",
"log.message": f'Simulation ended with error "{e}"',
},
)
failed = True
exception = e
finally:
self._clean_env_context()
self.stop_time = int(time.time())

self.send_event(
EndEvent(
failed=failed,
msg=(
self.format_error(exception, error_messages)
if failed
else "Experiment completed."
),
)
except ErtRunError as e:
self._completed_realizations_mask = []
failed = True
exception = e
except UserWarning:
pass
except Exception as e:
failed = True
exception = e
finally:
self._clean_env_context()
self.stop_time = int(time.time())

self.send_event(
EndEvent(
failed=failed,
msg=(
self.format_error(exception, error_messages)
if failed
else "Experiment completed."
),
)
)

@abstractmethod
def run_experiment(
Expand Down Expand Up @@ -579,10 +615,12 @@ def run_ensemble_evaluator(
ensemble: Ensemble,
ee_config: EvaluatorServerConfig,
) -> List[int]:
successful_realizations = asyncio.run(
self.run_ensemble_evaluator_async(run_args, ensemble, ee_config)
)
return successful_realizations
tracer = trace.get_tracer("ert.main")
with tracer.start_as_current_span("ert.run_model.run_ensemble"):
successful_realizations = asyncio.run(
self.run_ensemble_evaluator_async(run_args, ensemble, ee_config)
)
return successful_realizations

def _build_ensemble(
self,
Expand Down

0 comments on commit 0a5b658

Please sign in to comment.