From 0b7184ce06e1f306025bd05b860abd85a18e6722 Mon Sep 17 00:00:00 2001 From: Mirko Bonadei Date: Tue, 31 Jan 2023 13:02:45 +0100 Subject: [PATCH] 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 Commit-Queue: Mirko Bonadei Cr-Commit-Position: refs/heads/main@{#39237} --- .../metrics_set_proto_file_exporter.cc | 9 ++++++++ .../metrics/metrics_set_proto_file_exporter.h | 5 +++++ .../metrics_set_proto_file_exporter_test.cc | 21 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/api/test/metrics/metrics_set_proto_file_exporter.cc b/api/test/metrics/metrics_set_proto_file_exporter.cc index 86e6f2e136..f6f3d392a2 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 +#include #include +#include #include "api/test/metrics/metric.h" #include "rtc_base/logging.h" @@ -124,10 +126,17 @@ MetricsSetProtoFileExporter::Options::Options( 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 metadata) + : export_file_path(export_file_path), metadata(std::move(metadata)) {} bool MetricsSetProtoFileExporter::Export(rtc::ArrayView 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 f996e9e7b0..586ab83d00 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 #include #include "api/array_view.h" @@ -27,12 +28,16 @@ class MetricsSetProtoFileExporter : public MetricsExporter { 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 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 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 eb4d483068..9202d31343 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 @@ TEST_F(MetricsSetProtoFileExporterTest, MetricsAreExportedCorrectly) { 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{})); + 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{})); + 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