ml: add generic metrics to machine learning service

Add generic metrics to machine learning service, to log event(success or
fail), time_cost, cpu_time_cost and memory_usage

BUG=chromium:892999
TEST=Tested with chromium browser by adding fake event in it and
monitoring the histograms

Change-Id: I3fbc364b18c21fd4f093427670624ced0dd1984b
Reviewed-on: https://chromium-review.googlesource.com/1312494
Commit-Ready: Xinglong Luan <alanlxl@chromium.org>
Tested-by: Xinglong Luan <alanlxl@chromium.org>
Reviewed-by: Andrew Moylan <amoylan@chromium.org>
diff --git a/ml/machine_learning_service_impl.cc b/ml/machine_learning_service_impl.cc
index 0d72404..8247db1 100644
--- a/ml/machine_learning_service_impl.cc
+++ b/ml/machine_learning_service_impl.cc
@@ -3,6 +3,7 @@
 // found in the LICENSE file.
 
 #include "ml/machine_learning_service_impl.h"
+#include "ml/request_metrics.h"
 
 #include <memory>
 #include <utility>
@@ -24,6 +25,8 @@
 using ::chromeos::machine_learning::mojom::ModelSpecPtr;
 
 constexpr char kSystemModelDir[] = "/opt/google/chrome/ml_models/";
+// Base name for UMA metrics related to LoadModel requests
+constexpr char kMetricsNameBase[] = "LoadModelResult";
 
 // To avoid passing a lambda as a base::Closure.
 void DeleteModelImpl(const ModelImpl* const model_impl) {
@@ -51,8 +54,11 @@
 void MachineLearningServiceImpl::LoadModel(ModelSpecPtr spec,
                                            ModelRequest request,
                                            const LoadModelCallback& callback) {
+  RequestMetrics<LoadModelResult> request_metrics(kMetricsNameBase);
+  request_metrics.StartRecordingPerformanceMetrics();
   if (spec->id <= ModelId::UNKNOWN || spec->id > ModelId::kMax) {
     callback.Run(LoadModelResult::MODEL_SPEC_ERROR);
+    request_metrics.RecordRequestEvent(LoadModelResult::MODEL_SPEC_ERROR);
     return;
   }
 
@@ -62,6 +68,7 @@
   if (metadata_lookup == model_metadata_.end()) {
     LOG(ERROR) << "No metadata present for model ID " << spec->id << ".";
     callback.Run(LoadModelResult::LOAD_MODEL_ERROR);
+    request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR);
     return;
   }
   const ModelMetadata& metadata = metadata_lookup->second;
@@ -73,6 +80,7 @@
   if (model == nullptr) {
     LOG(ERROR) << "Failed to load model file '" << model_path << "'.";
     callback.Run(LoadModelResult::LOAD_MODEL_ERROR);
+    request_metrics.RecordRequestEvent(LoadModelResult::LOAD_MODEL_ERROR);
     return;
   }
 
@@ -83,6 +91,8 @@
   model_impl->set_connection_error_handler(
       base::Bind(&DeleteModelImpl, base::Unretained(model_impl)));
   callback.Run(LoadModelResult::OK);
+  request_metrics.FinishRecordingPerformanceMetrics();
+  request_metrics.RecordRequestEvent(LoadModelResult::OK);
 }
 
 }  // namespace ml