Skip to content

Commit

Permalink
orca: support to parse utilization from raw text (envoyproxy#37487)
Browse files Browse the repository at this point in the history
Signed-off-by: wangbaiping(wbpcode) <wangbaiping@bytedance.com>
  • Loading branch information
wbpcode authored Dec 5, 2024
1 parent d76d053 commit 850c7f1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
18 changes: 18 additions & 0 deletions source/common/orca/orca_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ absl::Status tryCopyMetricToOrcaLoadReport(absl::string_view metric_name,
fmt::format("custom backend load metric value({}) cannot be infinity.", metric_name));
}

// Check for negative values for all metrics.
if (value < 0) {
return absl::InvalidArgumentError(
fmt::format("custom backend load metric value({}) cannot be negative.", metric_name));
}

if (absl::StartsWith(metric_name, kUtilizationPrefix)) {
absl::string_view metric_name_without_prefix =
absl::StripPrefix(metric_name, kUtilizationPrefix);
if (metric_name_without_prefix.empty()) {
return absl::InvalidArgumentError("utilization metric key is empty.");
}

orca_load_report.mutable_utilization()->insert(
{std::string(metric_name_without_prefix), value});
return absl::OkStatus();
}

if (absl::StartsWith(metric_name, kNamedMetricsFieldPrefix)) {
auto metric_name_without_prefix = absl::StripPrefix(metric_name, kNamedMetricsFieldPrefix);
return tryCopyNamedMetricToOrcaLoadReport(metric_name_without_prefix, value, orca_load_report);
Expand Down
1 change: 1 addition & 0 deletions source/common/orca/orca_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static constexpr absl::string_view kMemUtilizationField = "mem_utilization";
static constexpr absl::string_view kEpsField = "eps";
static constexpr absl::string_view kRpsFractionalField = "rps_fractional";
static constexpr absl::string_view kNamedMetricsFieldPrefix = "named_metrics.";
static constexpr absl::string_view kUtilizationPrefix = "utilization.";

// Parses ORCA load metrics from a header map into an OrcaLoadReport proto.
// Supports native HTTP, JSON and serialized binary formats.
Expand Down
24 changes: 22 additions & 2 deletions test/common/orca/orca_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static xds::data::orca::v3::OrcaLoadReport exampleOrcaLoadReport() {
orca_load_report.set_rps_fractional(1000);
orca_load_report.mutable_named_metrics()->insert({"foo", 123});
orca_load_report.mutable_named_metrics()->insert({"bar", 0.2});
orca_load_report.mutable_utilization()->insert({"total", 0.5});
return orca_load_report;
}

Expand Down Expand Up @@ -74,7 +75,7 @@ TEST(OrcaParserUtilTest, NativeHttpEncodedHeader) {
absl::StrCat(kHeaderFormatPrefixText,
"cpu_utilization:0.7,application_utilization:0.8,mem_utilization:0.9,"
"rps_fractional:1000,eps:2,"
"named_metrics.foo:123,named_metrics.bar:0.2")}};
"named_metrics.foo:123,named_metrics.bar:0.2,utilization.total:0.5")}};
EXPECT_THAT(parseOrcaLoadReportHeaders(headers),
StatusHelpers::IsOkAndHolds(ProtoEq(exampleOrcaLoadReport())));
}
Expand Down Expand Up @@ -108,6 +109,15 @@ TEST(OrcaParserUtilTest, NativeHttpEncodedHeaderInfinityMetricValue) {
"infinity.")));
}

TEST(OrcaParserUtilTest, NativeHttpEncodedHeaderNegativeMetricValue) {
Http::TestRequestHeaderMapImpl headers{
{std::string(kEndpointLoadMetricsHeader),
absl::StrCat(kHeaderFormatPrefixText, "cpu_utilization:-1")}};
EXPECT_THAT(parseOrcaLoadReportHeaders(headers),
StatusHelpers::HasStatus(absl::InvalidArgumentError(
"custom backend load metric value(cpu_utilization) cannot be negative.")));
}

TEST(OrcaParserUtilTest, NativeHttpEncodedHeaderContainsDuplicateMetric) {
Http::TestRequestHeaderMapImpl headers{
{std::string(kEndpointLoadMetricsHeader),
Expand All @@ -126,6 +136,15 @@ TEST(OrcaParserUtilTest, NativeHttpEncodedHeaderUnsupportedMetric) {
absl::InvalidArgumentError("unsupported metric name: unsupported_metric")));
}

TEST(OrcaParserUtilTest, NativeHttpEncodedHeaderContainsEmptyUtilizationMetricKey) {
Http::TestRequestHeaderMapImpl headers{
{std::string(kEndpointLoadMetricsHeader),
absl::StrCat(kHeaderFormatPrefixText, "utilization.:0.9")}};
EXPECT_THAT(
parseOrcaLoadReportHeaders(headers),
StatusHelpers::HasStatus(absl::InvalidArgumentError("utilization metric key is empty.")));
}

TEST(OrcaParserUtilTest, NativeHttpEncodedHeaderContainsDuplicateNamedMetric) {
Http::TestRequestHeaderMapImpl headers{
{std::string(kEndpointLoadMetricsHeader),
Expand Down Expand Up @@ -161,7 +180,8 @@ TEST(OrcaParserUtilTest, JsonHeader) {
absl::StrCat(kHeaderFormatPrefixJson,
"{\"cpu_utilization\": 0.7, \"application_utilization\": 0.8, "
"\"mem_utilization\": 0.9, \"rps_fractional\": 1000, \"eps\": 2, "
"\"named_metrics\": {\"foo\": 123,\"bar\": 0.2}}")}};
"\"named_metrics\": {\"foo\": 123,\"bar\": 0.2}, "
"\"utilization\": {\"total\": 0.5}}")}};
EXPECT_THAT(parseOrcaLoadReportHeaders(headers),
StatusHelpers::IsOkAndHolds(ProtoEq(exampleOrcaLoadReport())));
}
Expand Down

0 comments on commit 850c7f1

Please sign in to comment.