Add function to get a Metric by id

To provide the LogElapsedTime 1.0 FIDL method, we need to correctly
identify the parts of the metric that correspond to the elapsed time,
component name and event index. This function will expose the relevant
metric to extract the metric parts.

Change-Id: I74284b9a48b2ff76a820f21ead850bbb29470ce3
diff --git a/encoder/encoder.cc b/encoder/encoder.cc
index 2ee0415..43ab861 100644
--- a/encoder/encoder.cc
+++ b/encoder/encoder.cc
@@ -545,6 +545,10 @@
   return project_->DefaultEncodingsForMetric(metric_id);
 }
 
+const Metric* Encoder::GetMetric(uint32_t metric_id) {
+  return project_->Metric(metric_id);
+}
+
 ValuePart& Encoder::Value::AddPart(uint32_t encoding_config_id,
                                    const std::string& part_name) {
   // emplace() returns a pair whose first element is an iterator over
diff --git a/encoder/encoder.h b/encoder/encoder.h
index 8a45f67..ae262ad 100644
--- a/encoder/encoder.h
+++ b/encoder/encoder.h
@@ -283,6 +283,9 @@
   const std::unordered_map<std::string, uint32_t>& DefaultEncodingsForMetric(
       uint32_t metric_id);
 
+  // Returns a pointer to the metric given by |metric_id|.
+  const Metric* GetMetric(uint32_t metric_id);
+
   // Sets a static value to use for the current time when computing the
   // day index. By default an Encoder uses the real system clock to determine
   // the current time. But this function may be invoked to override that
diff --git a/encoder/encoder_test.cc b/encoder/encoder_test.cc
index d02f7b4..37124fd 100644
--- a/encoder/encoder_test.cc
+++ b/encoder/encoder_test.cc
@@ -672,6 +672,17 @@
   EXPECT_EQ(kForculusEncodingId, encodings.begin()->second);
 }
 
+TEST(EncoderTest, GetMetric) {
+  // Build the ProjectContext encapsulating our test config data.
+  std::shared_ptr<ProjectContext> project = GetTestProject();
+  FakeSystemData system_data;
+
+  // Construct the Encoder.
+  Encoder encoder(project, ClientSecret::GenerateNewSecret(), &system_data);
+  auto metric = encoder.GetMetric(kSingleStringMetricId);
+  EXPECT_NE(nullptr, metric);
+}
+
 // Tests the advanced API, when used corretly.
 TEST(EncoderTest, AdvancedApiNoErrors) {
   // Build the ProjectContext encapsulating our test config data.