Check nullptr before dereferencing in MetricId

Fixes a nullptr deference in case of an invalid metric_name.

Change-Id: I529c1016affae2917e972b1f4a1d44e642103996
diff --git a/encoder/encoder.cc b/encoder/encoder.cc
index e4b8cd3..2ee0415 100644
--- a/encoder/encoder.cc
+++ b/encoder/encoder.cc
@@ -533,7 +533,11 @@
 }
 
 uint32_t Encoder::MetricId(const std::string& metric_name) {
-  return project_->Metric(metric_name)->id();
+  const Metric* metric = project_->Metric(metric_name);
+  if (metric) {
+    return metric->id();
+  }
+  return 0;
 }
 
 const std::unordered_map<std::string, uint32_t>&
diff --git a/encoder/encoder.h b/encoder/encoder.h
index 6bb6214..8a45f67 100644
--- a/encoder/encoder.h
+++ b/encoder/encoder.h
@@ -276,7 +276,7 @@
   Result Encode(uint32_t metric_id, const Value& value);
 
   // Returns the metric_id associated with the given |metric_name| by looking
-  // it up in the ProjectContext.
+  // it up in the ProjectContext. If there is no such Metric, returns 0.
   uint32_t MetricId(const std::string& metric_name);
 
   // Returns a map from MetricPart names to default encoding_ids.