Add metrics logging to analyzer/**

This is the last of the references to LOG(ERROR) in server-side c++ code

CB-70

Change-Id: If32a957994310d859ff50eefeeaf16b10e75de28
diff --git a/analyzer/analyzer_service/analyzer_service.cc b/analyzer/analyzer_service/analyzer_service.cc
index 4c856fc..4d80857 100644
--- a/analyzer/analyzer_service/analyzer_service.cc
+++ b/analyzer/analyzer_service/analyzer_service.cc
@@ -27,6 +27,7 @@
 #include "analyzer/store/bigtable_store.h"
 #include "analyzer/store/data_store.h"
 #include "util/encrypted_message_util.h"
+#include "util/log_based_metrics.h"
 #include "util/pem_util.h"
 
 namespace cobalt {
@@ -38,6 +39,12 @@
 using util::MessageDecrypter;
 using util::PemUtil;
 
+// Stackdriver metric constants
+namespace {
+const char kAddObservationsFailure[] =
+    "analyzer-service-add-observations-failure";
+}  // namespace
+
 DEFINE_int32(port, 0, "The port that the Analyzer Service should listen on.");
 DEFINE_string(
     private_key_pem_file, "",
@@ -108,7 +115,8 @@
   for (const EncryptedMessage& em : batch->encrypted_observation()) {
     if (!message_decrypter_.DecryptMessage(em, &observations[index++])) {
       std::string error_message = "Decryption of an Observation failed.";
-      LOG(ERROR) << error_message;
+      LOG_STACKDRIVER_COUNT_METRIC(ERROR, kAddObservationsFailure)
+          << error_message;
       return grpc::Status(grpc::INVALID_ARGUMENT, error_message);
     }
   }
@@ -116,8 +124,8 @@
   auto add_status =
       observation_store_->AddObservationBatch(batch->meta_data(), observations);
   if (add_status != store::kOK) {
-    LOG(ERROR) << "AddObservationBatch() failed with status code "
-               << add_status;
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kAddObservationsFailure)
+        << "AddObservationBatch() failed with status code " << add_status;
     switch (add_status) {
       case store::kInvalidArguments:
         return grpc::Status(grpc::INVALID_ARGUMENT, "");
diff --git a/analyzer/report_master/report_serializer.cc b/analyzer/report_master/report_serializer.cc
index 5a28d9b..053bfd1 100644
--- a/analyzer/report_master/report_serializer.cc
+++ b/analyzer/report_master/report_serializer.cc
@@ -395,7 +395,8 @@
                     "to be specified for RAW_DUMP reports. For ReportConfig "
                  << IdString(*report_config_);
     std::string message = error_stream.str();
-    LOG(ERROR) << message;
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kStartSerializingReportFailure)
+        << message;
     return grpc::Status(grpc::INVALID_ARGUMENT, message);
   }
   fixed_leftmost_column_values_.clear();
@@ -521,7 +522,7 @@
         error_stream << "Expecting a RAW_DUMP row but the row_type=" << row_type
                      << ". For ReportConfig " << IdString(*report_config_);
         std::string message = error_stream.str();
-        LOG(ERROR) << message;
+        LOG_STACKDRIVER_COUNT_METRIC(ERROR, kAppendRowsFailure) << message;
         return grpc::Status(grpc::INTERNAL, message);
       }
       return AppendCSVRawDumpReportRow(report_row.raw_dump(), stream);
@@ -581,7 +582,7 @@
                  << num_values_this_row << ". For ReportConfig "
                  << IdString(*report_config_);
     std::string message = error_stream.str();
-    LOG(ERROR) << message;
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kAppendRowsFailure) << message;
     return grpc::Status(grpc::INTERNAL, message);
   }
   for (const std::string& v : fixed_leftmost_column_values_) {
diff --git a/analyzer/store/bigtable_store.cc b/analyzer/store/bigtable_store.cc
index 320c6d0..9c6e689 100644
--- a/analyzer/store/bigtable_store.cc
+++ b/analyzer/store/bigtable_store.cc
@@ -61,6 +61,11 @@
 namespace {
 const char kReadRowsFailure[] = "bigtable-store-read-rows-failure";
 const char kWriteRowsFailure[] = "bigtable-store-write-rows-failure";
+const char kDeleteAllRowsFailure[] = "bigtable-store-delete-all-rows-failure";
+const char kDeleteRowFailure[] = "bigtable-store-delete-row-failure";
+const char kDeleteRowsWithPrefixFailure[] =
+    "bigtable-store-delete-rows-with-prefix-failure";
+const char kDoWriteRowsFailure[] = "bigtable-store-do-write-rows-failure";
 }  // namespace
 
 namespace {
@@ -232,7 +237,8 @@
       // later.
       std::string encoded_column_name;
       if (!RegexEncode(pair.first, &encoded_column_name)) {
-        LOG(ERROR) << "RegexEncode failed on '" << pair.first << "'";
+        LOG_STACKDRIVER_COUNT_METRIC(ERROR, kDoWriteRowsFailure)
+            << "RegexEncode failed on '" << pair.first << "'";
         return grpc::Status(grpc::INVALID_ARGUMENT, "RegexEncode failed.");
       }
       cell->mutable_column_qualifier()->swap(encoded_column_name);
@@ -453,7 +459,8 @@
   if (!status.ok()) {
     // TODO(rudominer) Consider doing a retry here. Consider if this
     // method should be asynchronous.
-    LOG(ERROR) << ErrorMessage(status, "DeleteRow");
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kDeleteRowFailure)
+        << ErrorMessage(status, "DeleteRow");
     return GrpcStatusToStoreStatus(status);
   }
 
@@ -473,7 +480,8 @@
   if (!status.ok()) {
     // TODO(rudominer) Consider doing a retry here. Consider if this
     // method should be asynchronous.
-    LOG(ERROR) << ErrorMessage(status, "DeleteRowsWithPrefix");
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kDeleteRowsWithPrefixFailure)
+        << ErrorMessage(status, "DeleteRowsWithPrefix");
     return GrpcStatusToStoreStatus(status);
   }
 
@@ -492,7 +500,8 @@
   if (!status.ok()) {
     // TODO(rudominer) Consider doing a retry here. Consider if this
     // method should be asynchronous.
-    LOG(ERROR) << ErrorMessage(status, "DeleteAllRows");
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kDeleteAllRowsFailure)
+        << ErrorMessage(status, "DeleteAllRows");
     return GrpcStatusToStoreStatus(status);
   }
 
diff --git a/analyzer/store/report_store.cc b/analyzer/store/report_store.cc
index f128418..f81fddf 100644
--- a/analyzer/store/report_store.cc
+++ b/analyzer/store/report_store.cc
@@ -26,6 +26,7 @@
 #include "glog/logging.h"
 #include "util/crypto_util/random.h"
 #include "util/datetime_util.h"
+#include "util/log_based_metrics.h"
 
 using google::protobuf::MessageLite;
 
@@ -35,6 +36,20 @@
 
 using util::ToUnixSeconds;
 
+// Stackdriver metric constants
+namespace {
+const char kParseSingleColumnFailure[] = "parse-single-column-failure";
+const char kCheckRowTypeFailure[] = "check-row-type-failure";
+const char kWriteMetadataFailure[] = "report-store-write-metadata-failure";
+const char kWriteBulkMetadataFailure[] =
+    "report-store-write-bulk-metadata-failure";
+const char kStartNewReportFailure[] = "report-store-start-new-report-failure";
+const char kCreateDependentReportFailure[] =
+    "report-store-create-dependent-report-failure";
+const char kAddReportRowsFailure[] = "report-store-add-report-rows-failure";
+const char kGetReportFailure[] = "report-store-get-report-failure";
+}  // namespace
+
 namespace {
 // We currently do not support reports with more than this many rows.
 // TODO(rudominer) Consider supporting arbitrarily large reports. Currently
@@ -90,26 +105,28 @@
                          const std::string& error_message_prefix,
                          MessageLite* proto_message) {
   if (row.column_values.size() != 1) {
-    LOG(ERROR) << error_message_prefix << " for report_id "
-               << ReportStore::ToString(report_id)
-               << ": expected to receive one column but recieved "
-               << row.column_values.size() << " columns.";
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kParseSingleColumnFailure)
+        << error_message_prefix << " for report_id "
+        << ReportStore::ToString(report_id)
+        << ": expected to receive one column but recieved "
+        << row.column_values.size() << " columns.";
     return kOperationFailed;
   }
 
   auto iter = row.column_values.find(column_name);
 
   if (iter == row.column_values.end()) {
-    LOG(ERROR) << error_message_prefix << " for report_id "
-               << ReportStore::ToString(report_id)
-               << ": Column not found: " << column_name;
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kParseSingleColumnFailure)
+        << error_message_prefix << " for report_id "
+        << ReportStore::ToString(report_id)
+        << ": Column not found: " << column_name;
     return kOperationFailed;
   }
 
   if (!proto_message->ParseFromString(iter->second)) {
-    LOG(ERROR) << error_message_prefix << " for report_id "
-               << ReportStore::ToString(report_id)
-               << ": Unable to parse ReportRow";
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kParseSingleColumnFailure)
+        << error_message_prefix << " for report_id "
+        << ReportStore::ToString(report_id) << ": Unable to parse ReportRow";
     return kOperationFailed;
   }
   return kOK;
@@ -154,8 +171,9 @@
       return report_row.has_joint();
     }
     default: {
-      LOG(ERROR) << "Unrecognized ReportType: " << metadata.report_type()
-                 << " for report_id=" << ReportStore::ToString(report_id);
+      LOG_STACKDRIVER_COUNT_METRIC(ERROR, kCheckRowTypeFailure)
+          << "Unrecognized ReportType: " << metadata.report_type()
+          << " for report_id=" << ReportStore::ToString(report_id);
       return false;
     }
   }
@@ -186,9 +204,10 @@
   // Write the Row to the report_metadata table.
   Status status = store_->WriteRow(DataStore::kReportMetadata, std::move(row));
   if (status != kOK) {
-    LOG(ERROR) << "Error while writing metadata for report_id "
-               << ToString(report_id) << ": WriteRow() "
-               << "failed with status=" << status;
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kWriteMetadataFailure)
+        << "Error while writing metadata for report_id " << ToString(report_id)
+        << ": WriteRow() "
+        << "failed with status=" << status;
     return status;
   }
 
@@ -208,9 +227,10 @@
   Status status =
       store_->WriteRows(DataStore::kReportMetadata, std::move(rows));
   if (status != kOK) {
-    LOG(ERROR) << "Error while writing metadata for " << num_reports
-               << "reports: WriteRows() "
-               << "failed with status=" << status;
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kWriteBulkMetadataFailure)
+        << "Error while writing metadata for " << num_reports
+        << "reports: WriteRows() "
+        << "failed with status=" << status;
     return status;
   }
 
@@ -239,7 +259,8 @@
   metadata.set_export_name(export_name);
   metadata.set_in_store(in_store);
   if (in_store && report_type == RAW_DUMP) {
-    LOG(ERROR) << "A RAW_DUMP report may not be stored in the ReportStore.";
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kStartNewReportFailure)
+        << "A RAW_DUMP report may not be stored in the ReportStore.";
     return kInvalidArguments;
   }
 
@@ -273,7 +294,8 @@
   metadata.set_report_type(report_type);
   metadata.set_in_store(in_store);
   if (in_store && report_type == RAW_DUMP) {
-    LOG(ERROR) << "A RAW_DUMP report may not be stored in the ReportStore.";
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kCreateDependentReportFailure)
+        << "A RAW_DUMP report may not be stored in the ReportStore.";
     return kInvalidArguments;
   }
 
@@ -336,29 +358,31 @@
     return kOK;
   }
   if (report_id.creation_time_seconds() == 0 || report_id.instance_id() == 0) {
-    LOG(ERROR) << "Attempt to AddReportRow for incomplete report_id: "
-               << ToString(report_id);
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kAddReportRowsFailure)
+        << "Attempt to AddReportRow for incomplete report_id: "
+        << ToString(report_id);
     return kInvalidArguments;
   }
 
   ReportMetadataLite metadata;
   Status status = GetMetadata(report_id, &metadata);
   if (status != kOK) {
-    LOG(ERROR) << "Failed to get metadata for report_id: "
-               << ToString(report_id);
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kAddReportRowsFailure)
+        << "Failed to get metadata for report_id: " << ToString(report_id);
     return status;
   }
 
   if (!metadata.in_store()) {
-    LOG(ERROR)
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kAddReportRowsFailure)
         << "Cannot add report rows for a report for which in_store is false."
         << " report_id: " << ToString(report_id);
     return kInvalidArguments;
   }
 
   if (metadata.state() != IN_PROGRESS) {
-    LOG(ERROR) << "Report is not IN_PROGRESS. state=" << metadata.state()
-               << " report_id: " << ToString(report_id);
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kAddReportRowsFailure)
+        << "Report is not IN_PROGRESS. state=" << metadata.state()
+        << " report_id: " << ToString(report_id);
     return kPreconditionFailed;
   }
 
@@ -371,7 +395,8 @@
 
     std::string serialized_row;
     if (!report_row.SerializeToString(&serialized_row)) {
-      LOG(ERROR) << "Serializing report_row failed";
+      LOG_STACKDRIVER_COUNT_METRIC(ERROR, kAddReportRowsFailure)
+          << "Serializing report_row failed";
       return kOperationFailed;
     }
 
@@ -386,9 +411,10 @@
   status =
       store_->WriteRows(DataStore::kReportRows, std::move(data_store_rows));
   if (status != kOK) {
-    LOG(ERROR) << "Error while attempting to write report rows for report_id "
-               << ToString(report_id) << ": WriteRow() "
-               << "failed with status=" << status;
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kAddReportRowsFailure)
+        << "Error while attempting to write report rows for report_id "
+        << ToString(report_id) << ": WriteRow() "
+        << "failed with status=" << status;
     return status;
   }
 
@@ -440,8 +466,8 @@
   }
 
   if (read_response.more_available) {
-    LOG(ERROR) << "Report contains too many rows to return! "
-               << ToString(report_id);
+    LOG_STACKDRIVER_COUNT_METRIC(ERROR, kGetReportFailure)
+        << "Report contains too many rows to return! " << ToString(report_id);
     return kPreconditionFailed;
   }