Report metrics to statsd from update engine

Call the proper logging functions after an update attempt or a
successful update. This is part of the effort for the new metrics
mechanism migration.

Bug: 137682371
Test: run statsd_testdrive and check events
Change-Id: I1174ff37d049172a8a6b14d47aa40c54f26be183
Merged-In: I1174ff37d049172a8a6b14d47aa40c54f26be183
(cherry picked from commit a215b59432218842c99b244107b15d4894944577)
diff --git a/Android.bp b/Android.bp
index e457cd1..693abf8 100644
--- a/Android.bp
+++ b/Android.bp
@@ -266,6 +266,7 @@
         "liblog",
         "libmetricslogger",
         "libssl",
+        "libstatslog",
         "libutils",
     ],
 }
diff --git a/metrics_reporter_android.cc b/metrics_reporter_android.cc
index 9165f0d..4165c14 100644
--- a/metrics_reporter_android.cc
+++ b/metrics_reporter_android.cc
@@ -16,10 +16,14 @@
 
 #include "update_engine/metrics_reporter_android.h"
 
+#include <stdint.h>
+
 #include <memory>
 #include <string>
 
+#include <android-base/properties.h>
 #include <metricslogger/metrics_logger.h>
+#include <statslog.h>
 
 #include "update_engine/common/constants.h"
 
@@ -28,6 +32,16 @@
   android::metricslogger::LogHistogram(metrics, value);
   LOG(INFO) << "uploading " << value << " to histogram for metric " << metrics;
 }
+
+// A number offset adds on top of the enum value. e.g. ErrorCode::SUCCESS will
+// be reported as 10000, and AttemptResult::UPDATE_CANCELED will be reported as
+// 10011. The keeps the ordering of update engine's enum definition when statsd
+// atoms reserve the value 0 for unknown state.
+constexpr auto kMetricsReporterEnumOffset = 10000;
+
+int32_t GetStatsdEnumValue(int32_t value) {
+  return kMetricsReporterEnumOffset + value;
+}
 }  // namespace
 
 namespace chromeos_update_engine {
@@ -100,6 +114,17 @@
                static_cast<int>(attempt_result));
   LogHistogram(metrics::kMetricsUpdateEngineAttemptErrorCode,
                static_cast<int>(error_code));
+
+  android::util::stats_write(
+      android::util::UPDATE_ENGINE_UPDATE_ATTEMPT_REPORTED,
+      attempt_number,
+      GetStatsdEnumValue(static_cast<int32_t>(payload_type)),
+      duration.InMinutes(),
+      duration_uptime.InMinutes(),
+      payload_size_mib,
+      GetStatsdEnumValue(static_cast<int32_t>(attempt_result)),
+      GetStatsdEnumValue(static_cast<int32_t>(error_code)),
+      android::base::GetProperty("ro.build.fingerprint", "").c_str());
 }
 
 void MetricsReporterAndroid::ReportUpdateAttemptDownloadMetrics(
@@ -148,6 +173,16 @@
       total_duration.InMinutes());
   LogHistogram(metrics::kMetricsUpdateEngineSuccessfulUpdateRebootCount,
                reboot_count);
+
+  android::util::stats_write(
+      android::util::UPDATE_ENGINE_SUCCESSFUL_UPDATE_REPORTED,
+      attempt_count,
+      GetStatsdEnumValue(static_cast<int32_t>(payload_type)),
+      payload_size_mib,
+      total_bytes_downloaded,
+      download_overhead_percentage,
+      total_duration.InMinutes(),
+      reboot_count);
 }
 
 void MetricsReporterAndroid::ReportAbnormallyTerminatedUpdateAttemptMetrics() {