[autotest] Add sponge url to tko_job_keyvals

This is for Autotest to retrieve the url to Sponge invocation.
Also added more logging in Sponge upload failure.

BUG=None
TEST=local test

Change-Id: I6e1d6c269b810a3f235e7b94d635fe2882fe07cd
Reviewed-on: https://chromium-review.googlesource.com/398521
Commit-Ready: Dan Shi <dshi@google.com>
Tested-by: Dan Shi <dshi@google.com>
Reviewed-by: Kevin Cheng <kevcheng@chromium.org>
diff --git a/site_utils/sponge_utils.py b/site_utils/sponge_utils.py
index 7563860..dc5ac82 100644
--- a/site_utils/sponge_utils.py
+++ b/site_utils/sponge_utils.py
@@ -9,6 +9,7 @@
 import os
 import socket
 import time
+import traceback
 
 import common
 
@@ -20,6 +21,7 @@
 
 from autotest_lib.client.common_lib import decorators
 from autotest_lib.client.common_lib import global_config
+from autotest_lib.client.common_lib.cros.graphite import autotest_es
 from autotest_lib.site_utils import job_directories
 from autotest_lib.tko import models
 from autotest_lib.tko import utils as tko_utils
@@ -33,6 +35,9 @@
 USE_PROD_SERVER = CONFIG.get_config_value(
         'SERVER', 'use_prod_sponge_server', default=False, type=bool)
 
+# Type string of metadata.
+_SPONGE_UPLOAD_FAILURE_TYPE = 'sponge_upload_failure'
+
 @decorators.test_module_available(sponge)
 def upload_results_in_test(test, test_pass=True, acts_summary=None):
     """Upload test results to Sponge.
@@ -43,6 +48,8 @@
             considered to success, or exception like TestFail would have been
             raised if the test has failed.
     @param acts_summary: Path to the json file of ACTS test summary.
+
+    @return: A url to the Sponge invocation.
     """
     try:
         # job keyval file has the information about the test job except
@@ -71,14 +78,22 @@
         logging.debug('Test result is uploaded to Sponge: %s', invocation_url)
         return invocation_url
     except Exception as e:
+        metadata = {'method': 'upload_results_in_test',
+                    'job_id': job_id, 'error': str(e),
+                    'details': traceback.format_exc()}
+        autotest_es.post(use_http=True, type_str=_SPONGE_UPLOAD_FAILURE_TYPE,
+                         metadata=metadata)
         logging.exception('Failed to upload to Sponge: %s', e)
 
 
 @decorators.test_module_available(sponge)
-def upload_results(job):
+def upload_results(job, log=logging.debug):
     """Upload test results to Sponge with given job details.
 
     @param job: A job object created by tko/parsers.
+    @param log: Logging method, default is logging.debug.
+
+    @return: A url to the Sponge invocation.
     """
     job_id = job_directories.get_job_id_or_task_id(job.dir)
     results_dir = tko_utils.find_toplevel_job_dir(job.dir)
@@ -109,7 +124,13 @@
                 acts_summary=acts_summary,
                 job=job,
                 use_prod_server=USE_PROD_SERVER)
-        logging.debug('Test result is uploaded to Sponge: %s', invocation_url)
+        log('Test result is uploaded to Sponge: %s' % invocation_url)
         return invocation_url
     except Exception as e:
-        logging.exception('Failed to upload to Sponge: %s', e)
\ No newline at end of file
+        metadata = {'method': 'upload_results',
+                    'job_id': job_id, 'error': str(e),
+                    'details': traceback.format_exc()}
+        autotest_es.post(use_http=True, type_str=_SPONGE_UPLOAD_FAILURE_TYPE,
+                         metadata=metadata)
+        log('Failed to upload to Sponge: %s\nDetails:\n%s' %
+            (e, traceback.format_exc()))