Add possibility to set MetricsSet metadata.

Bug: b/266997275
Change-Id: I2c4fadcff7044a8c72ef7e46caf4eff398e29f91
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/291700
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#39237}
diff --git a/api/test/metrics/metrics_set_proto_file_exporter.cc b/api/test/metrics/metrics_set_proto_file_exporter.cc
index 86e6f2e..f6f3d39 100644
--- a/api/test/metrics/metrics_set_proto_file_exporter.cc
+++ b/api/test/metrics/metrics_set_proto_file_exporter.cc
@@ -11,7 +11,9 @@
 
 #include <stdio.h>
 
+#include <map>
 #include <string>
+#include <utility>
 
 #include "api/test/metrics/metric.h"
 #include "rtc_base/logging.h"
@@ -124,10 +126,17 @@
     bool export_whole_time_series)
     : export_file_path(export_file_path),
       export_whole_time_series(export_whole_time_series) {}
+MetricsSetProtoFileExporter::Options::Options(
+    absl::string_view export_file_path,
+    std::map<std::string, std::string> metadata)
+    : export_file_path(export_file_path), metadata(std::move(metadata)) {}
 
 bool MetricsSetProtoFileExporter::Export(rtc::ArrayView<const Metric> metrics) {
 #if WEBRTC_ENABLE_PROTOBUF
   webrtc::test_metrics::MetricsSet metrics_set;
+  for (const auto& [key, value] : options_.metadata) {
+    metrics_set.mutable_metadata()->insert({key, value});
+  }
   for (const Metric& metric : metrics) {
     webrtc::test_metrics::Metric* metric_proto = metrics_set.add_metrics();
     metric_proto->set_name(metric.name);
diff --git a/api/test/metrics/metrics_set_proto_file_exporter.h b/api/test/metrics/metrics_set_proto_file_exporter.h
index f996e9e7..586ab83 100644
--- a/api/test/metrics/metrics_set_proto_file_exporter.h
+++ b/api/test/metrics/metrics_set_proto_file_exporter.h
@@ -11,6 +11,7 @@
 #ifndef API_TEST_METRICS_METRICS_SET_PROTO_FILE_EXPORTER_H_
 #define API_TEST_METRICS_METRICS_SET_PROTO_FILE_EXPORTER_H_
 
+#include <map>
 #include <string>
 
 #include "api/array_view.h"
@@ -27,12 +28,16 @@
   struct Options {
     explicit Options(absl::string_view export_file_path);
     Options(absl::string_view export_file_path, bool export_whole_time_series);
+    Options(absl::string_view export_file_path,
+            std::map<std::string, std::string> metadata);
 
     // File to export proto.
     std::string export_file_path;
     // If true will write all time series values to the output proto file,
     // otherwise will write stats only.
     bool export_whole_time_series = true;
+    // Metadata associated to the whole MetricsSet.
+    std::map<std::string, std::string> metadata;
   };
 
   explicit MetricsSetProtoFileExporter(const Options& options)
diff --git a/api/test/metrics/metrics_set_proto_file_exporter_test.cc b/api/test/metrics/metrics_set_proto_file_exporter_test.cc
index eb4d483..9202d31 100644
--- a/api/test/metrics/metrics_set_proto_file_exporter_test.cc
+++ b/api/test/metrics/metrics_set_proto_file_exporter_test.cc
@@ -146,6 +146,27 @@
   EXPECT_THAT(actual_metrics_set.metrics(1).stats().max(), Eq(40.0));
 }
 
+TEST_F(MetricsSetProtoFileExporterTest, NoMetricsSetMetadata) {
+  MetricsSetProtoFileExporter::Options options(temp_filename_);
+  MetricsSetProtoFileExporter exporter(options);
+  ASSERT_TRUE(exporter.Export(std::vector<Metric>{}));
+  webrtc::test_metrics::MetricsSet actual_metrics_set;
+  actual_metrics_set.ParseFromString(ReadFileAsString(temp_filename_));
+  EXPECT_EQ(actual_metrics_set.metadata_size(), 0);
+}
+
+TEST_F(MetricsSetProtoFileExporterTest, MetricsSetMetadata) {
+  MetricsSetProtoFileExporter::Options options(
+      temp_filename_, {{"a_metadata_key", "a_metadata_value"}});
+  MetricsSetProtoFileExporter exporter(options);
+  ASSERT_TRUE(exporter.Export(std::vector<Metric>{}));
+  webrtc::test_metrics::MetricsSet actual_metrics_set;
+  actual_metrics_set.ParseFromString(ReadFileAsString(temp_filename_));
+  EXPECT_EQ(actual_metrics_set.metadata_size(), 1);
+  EXPECT_EQ(actual_metrics_set.metadata().at("a_metadata_key"),
+            "a_metadata_value");
+}
+
 }  // namespace
 }  // namespace test
 }  // namespace webrtc