Ensure we always report the test_pair

When a timeout occurs we could lose the test_pair reporting
Ensure it's backfilled for proper history.

Test: presubmit
Bug: 309106387
Change-Id: If51e363ebde84f4f1bb16670ae86da2fa273af6d
diff --git a/src/com/android/tradefed/sandbox/SandboxInvocationRunner.java b/src/com/android/tradefed/sandbox/SandboxInvocationRunner.java
index 9e453cd..cbb70f8 100644
--- a/src/com/android/tradefed/sandbox/SandboxInvocationRunner.java
+++ b/src/com/android/tradefed/sandbox/SandboxInvocationRunner.java
@@ -18,6 +18,8 @@
 import com.android.tradefed.config.Configuration;
 import com.android.tradefed.config.IConfiguration;
 import com.android.tradefed.invoker.TestInformation;
+import com.android.tradefed.invoker.logger.InvocationMetricLogger;
+import com.android.tradefed.invoker.logger.InvocationMetricLogger.InvocationMetricKey;
 import com.android.tradefed.log.LogUtil.CLog;
 import com.android.tradefed.result.ITestInvocationListener;
 import com.android.tradefed.util.CommandResult;
@@ -82,11 +84,18 @@
         if (sandbox == null) {
             throw new RuntimeException("Couldn't find the sandbox object.");
         }
+        long start = System.currentTimeMillis();
         try {
             CommandResult result = sandbox.run(info, config, listener);
             return CommandStatus.SUCCESS.equals(result.getStatus());
         } finally {
             sandbox.tearDown();
+            // Only log if it was no already logged to keep the value closest to execution
+            if (!InvocationMetricLogger.getInvocationMetrics()
+                    .containsKey(InvocationMetricKey.TEST_PAIR.toString())) {
+                InvocationMetricLogger.addInvocationPairMetrics(
+                        InvocationMetricKey.TEST_PAIR, start, System.currentTimeMillis());
+            }
         }
     }
 }