Skip to content

Commit

Permalink
More accurate reported runtime when pantsd is in use. (#14177)
Browse files Browse the repository at this point in the history
The `PantsLoader` captures a `start_time` as early as possible, which is then supposed to be propagated all the way to the `pantsd` server via environment variables. But the `RemotePantsRunner` was calculating its own start time, which would miss out on some of the startup costs (options parsing in particular).

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
stuhood authored Jan 14, 2022
1 parent 6759bd7 commit c8e7cf7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/python/pants/bin/daemon_pants_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ def single_daemonized_run(
# Capture the client's start time, which we propagate here in order to get an accurate
# view of total time.
env_start_time = env.get("PANTSD_RUNTRACKER_CLIENT_START_TIME", None)
if not env_start_time:
# NB: We warn rather than erroring here because it eases use of non-Pants nailgun
# clients for testing.
logger.warning(
"No start time was reported by the client! Metrics may be inaccurate."
)
start_time = float(env_start_time) if env_start_time else time.time()

options_bootstrapper = OptionsBootstrapper.create(
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/bin/pants_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def run(self, start_time: float) -> ExitCode:
if self._should_run_with_pantsd(global_bootstrap_options):
try:
remote_runner = RemotePantsRunner(self.args, self.env, options_bootstrapper)
return remote_runner.run()
return remote_runner.run(start_time)
except RemotePantsRunner.Fallback as e:
logger.warning(f"Client exception: {e!r}, falling back to non-daemon mode")

Expand Down
11 changes: 6 additions & 5 deletions src/python/pants/bin/remote_pants_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,24 @@ def __init__(
:param env: The environment (e.g. os.environ) for this run.
:param options_bootstrapper: The bootstrap options.
"""
self._start_time = time.time()
self._args = args
self._env = env
self._options_bootstrapper = options_bootstrapper
self._bootstrap_options = options_bootstrapper.bootstrap_options
self._client = PantsDaemonClient(self._bootstrap_options)

def run(self) -> ExitCode:
def run(self, start_time: float) -> ExitCode:
"""Starts up a pantsd instance if one is not already running, then connects to it via
nailgun."""

pantsd_handle = self._client.maybe_launch()
logger.debug(f"Connecting to pantsd on port {pantsd_handle.port}")

return self._connect_and_execute(pantsd_handle)
return self._connect_and_execute(pantsd_handle, start_time)

def _connect_and_execute(self, pantsd_handle: PantsDaemonClient.Handle) -> ExitCode:
def _connect_and_execute(
self, pantsd_handle: PantsDaemonClient.Handle, start_time: float
) -> ExitCode:
global_options = self._bootstrap_options.for_global_scope()
executor = GlobalOptions.create_py_executor(global_options)

Expand All @@ -126,7 +127,7 @@ def _connect_and_execute(self, pantsd_handle: PantsDaemonClient.Handle) -> ExitC
modified_env = {
**self._env,
**ng_env,
"PANTSD_RUNTRACKER_CLIENT_START_TIME": str(self._start_time),
"PANTSD_RUNTRACKER_CLIENT_START_TIME": str(start_time),
"PANTSD_REQUEST_TIMEOUT_LIMIT": str(
global_options.pantsd_timeout_when_multiple_invocations
),
Expand Down

0 comments on commit c8e7cf7

Please sign in to comment.