From eb11fdede8548316a7b24d04f4427121927bc999 Mon Sep 17 00:00:00 2001 From: Pengyuan Bian Date: Wed, 11 Sep 2019 09:53:09 -0700 Subject: [PATCH] tracing: fix random sample fraction percent (#8205) Signed-off-by: Pengyuan Bian --- .../network/http_connection_manager/config.cc | 5 +-- .../http_connection_manager/config_test.cc | 36 ++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/source/extensions/filters/network/http_connection_manager/config.cc b/source/extensions/filters/network/http_connection_manager/config.cc index 532f0e086e5d..998dce6709ab 100644 --- a/source/extensions/filters/network/http_connection_manager/config.cc +++ b/source/extensions/filters/network/http_connection_manager/config.cc @@ -282,8 +282,9 @@ HttpConnectionManagerConfig::HttpConnectionManagerConfig( envoy::type::FractionalPercent random_sampling; // TODO: Random sampling historically was an integer and default to out of 10,000. We should // deprecate that and move to a straight fractional percent config. - random_sampling.set_numerator( - tracing_config.has_random_sampling() ? tracing_config.random_sampling().value() : 10000); + uint64_t random_sampling_numerator{PROTOBUF_PERCENT_TO_ROUNDED_INTEGER_OR_DEFAULT( + tracing_config, random_sampling, 10000, 10000)}; + random_sampling.set_numerator(random_sampling_numerator); random_sampling.set_denominator(envoy::type::FractionalPercent::TEN_THOUSAND); envoy::type::FractionalPercent overall_sampling; overall_sampling.set_numerator( diff --git a/test/extensions/filters/network/http_connection_manager/config_test.cc b/test/extensions/filters/network/http_connection_manager/config_test.cc index b16fbfc97ebb..3341802e659c 100644 --- a/test/extensions/filters/network/http_connection_manager/config_test.cc +++ b/test/extensions/filters/network/http_connection_manager/config_test.cc @@ -355,7 +355,7 @@ TEST_F(HttpConnectionManagerConfigTest, SamplingConfigured) { EXPECT_EQ(1, config.tracingConfig()->client_sampling_.numerator()); EXPECT_EQ(envoy::type::FractionalPercent::HUNDRED, config.tracingConfig()->client_sampling_.denominator()); - EXPECT_EQ(2, config.tracingConfig()->random_sampling_.numerator()); + EXPECT_EQ(200, config.tracingConfig()->random_sampling_.numerator()); EXPECT_EQ(envoy::type::FractionalPercent::TEN_THOUSAND, config.tracingConfig()->random_sampling_.denominator()); EXPECT_EQ(3, config.tracingConfig()->overall_sampling_.numerator()); @@ -363,6 +363,40 @@ TEST_F(HttpConnectionManagerConfigTest, SamplingConfigured) { config.tracingConfig()->overall_sampling_.denominator()); } +TEST_F(HttpConnectionManagerConfigTest, FractionalSamplingConfigured) { + const std::string yaml_string = R"EOF( + stat_prefix: ingress_http + internal_address_config: + unix_sockets: true + route_config: + name: local_route + tracing: + operation_name: ingress + client_sampling: + value: 0.1 + random_sampling: + value: 0.2 + overall_sampling: + value: 0.3 + http_filters: + - name: envoy.router + )EOF"; + + HttpConnectionManagerConfig config(parseHttpConnectionManagerFromV2Yaml(yaml_string), context_, + date_provider_, route_config_provider_manager_, + scoped_routes_config_provider_manager_); + + EXPECT_EQ(0, config.tracingConfig()->client_sampling_.numerator()); + EXPECT_EQ(envoy::type::FractionalPercent::HUNDRED, + config.tracingConfig()->client_sampling_.denominator()); + EXPECT_EQ(20, config.tracingConfig()->random_sampling_.numerator()); + EXPECT_EQ(envoy::type::FractionalPercent::TEN_THOUSAND, + config.tracingConfig()->random_sampling_.denominator()); + EXPECT_EQ(0, config.tracingConfig()->overall_sampling_.numerator()); + EXPECT_EQ(envoy::type::FractionalPercent::HUNDRED, + config.tracingConfig()->overall_sampling_.denominator()); +} + TEST_F(HttpConnectionManagerConfigTest, UnixSocketInternalAddress) { const std::string yaml_string = R"EOF( stat_prefix: ingress_http