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}
This commit is contained in:
Mirko Bonadei 2023-01-31 13:02:45 +01:00 committed by WebRTC LUCI CQ
parent 217b384c1b
commit 0b7184ce06
3 changed files with 35 additions and 0 deletions

View File

@ -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 @@ 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<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);

View File

@ -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 @@ 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<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)

View File

@ -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<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