Skip to content

Commit

Permalink
tracing: move the ConnectionManagerTracingConfig to tracing tree for …
Browse files Browse the repository at this point in the history
…common usage (envoyproxy#25256)

* initial commit

Signed-off-by: wbpcode <[email protected]>

* fix format

Signed-off-by: wbpcode <[email protected]>

* fix build

Signed-off-by: wbpcode <[email protected]>

* fix build 2

Signed-off-by: wbpcode <[email protected]>

* fix build 2

Signed-off-by: wbpcode <[email protected]>

* fix test

Signed-off-by: wbpcode <[email protected]>

* add a new test

Signed-off-by: wbpcode <[email protected]>

* fix format and build

Signed-off-by: wbpcode <[email protected]>

---------

Signed-off-by: wbpcode <[email protected]>
  • Loading branch information
code authored Feb 3, 2023
1 parent 0708820 commit 51b4927
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 63 deletions.
56 changes: 56 additions & 0 deletions envoy/tracing/trace_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,61 @@ class Config {
virtual uint32_t maxPathTagLength() const PURE;
};

/**
* Route or connection manager level tracing configuration.
*/
class TracingConfig {
public:
virtual ~TracingConfig() = default;

/**
* This method returns the client sampling percentage.
* @return the client sampling percentage
*/
virtual const envoy::type::v3::FractionalPercent& getClientSampling() const PURE;

/**
* This method returns the random sampling percentage.
* @return the random sampling percentage
*/
virtual const envoy::type::v3::FractionalPercent& getRandomSampling() const PURE;

/**
* This method returns the overall sampling percentage.
* @return the overall sampling percentage
*/
virtual const envoy::type::v3::FractionalPercent& getOverallSampling() const PURE;

/**
* This method returns the tracing custom tags.
* @return the tracing custom tags.
*/
virtual const Tracing::CustomTagMap& getCustomTags() const PURE;
};

/**
* Connection manager tracing configuration.
*/
class ConnectionManagerTracingConfig : public TracingConfig {
public:
/**
* @return operation name for tracing, e.g., ingress.
*/
virtual OperationName operationName() const PURE;

/**
* @return true if spans should be annotated with more detailed information.
*/
virtual bool verbose() const PURE;

/**
* @return the maximum length allowed for paths in the extracted HttpUrl tag. This is only used
* for HTTP protocol tracing.
*/
virtual uint32_t maxPathTagLength() const PURE;
};

using ConnectionManagerTracingConfigPtr = std::unique_ptr<ConnectionManagerTracingConfig>;

} // namespace Tracing
} // namespace Envoy
1 change: 1 addition & 0 deletions source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ envoy_cc_library(
"//source/common/local_reply:local_reply_lib",
"//source/common/network:utility_lib",
"//source/common/stats:symbol_table_lib",
"//source/common/tracing:tracer_config_lib",
"@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg_cc_proto",
"@envoy_api//envoy/type/v3:pkg_cc_proto",
],
Expand Down
17 changes: 2 additions & 15 deletions source/common/http/conn_manager_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "source/common/local_reply/local_reply.h"
#include "source/common/network/utility.h"
#include "source/common/stats/symbol_table.h"
#include "source/common/tracing/tracer_config_impl.h"

namespace Envoy {
namespace Http {
Expand Down Expand Up @@ -122,21 +123,7 @@ struct ConnectionManagerTracingStats {
CONN_MAN_TRACING_STATS(GENERATE_COUNTER_STRUCT)
};

/**
* Configuration for tracing which is set on the connection manager level.
* Http Tracing can be enabled/disabled on a per connection manager basis.
* Here we specify some specific for connection manager settings.
*/
struct TracingConnectionManagerConfig {
Tracing::OperationName operation_name_;
Tracing::CustomTagMap custom_tags_;
envoy::type::v3::FractionalPercent client_sampling_;
envoy::type::v3::FractionalPercent random_sampling_;
envoy::type::v3::FractionalPercent overall_sampling_;
bool verbose_;
uint32_t max_path_tag_length_;
};

using TracingConnectionManagerConfig = Tracing::ConnectionManagerTracingConfigImpl;
using TracingConnectionManagerConfigPtr = std::unique_ptr<TracingConnectionManagerConfig>;

/**
Expand Down
2 changes: 2 additions & 0 deletions source/common/tracing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ envoy_cc_library(
"tracer_config_impl.h",
],
deps = [
":custom_tag_lib",
"//envoy/server:tracer_config_interface",
"@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg_cc_proto",
],
)

Expand Down
90 changes: 90 additions & 0 deletions source/common/tracing/tracer_config_impl.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#pragma once

#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.validate.h"
#include "envoy/protobuf/message_validator.h"
#include "envoy/server/tracer_config.h"

#include "source/common/tracing/custom_tag_impl.h"

namespace Envoy {
namespace Tracing {

using ConnectionManagerTracingConfigProto =
envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager_Tracing;

class TracerFactoryContextImpl : public Server::Configuration::TracerFactoryContext {
public:
TracerFactoryContextImpl(Server::Configuration::ServerFactoryContext& server_factory_context,
Expand All @@ -23,5 +30,88 @@ class TracerFactoryContextImpl : public Server::Configuration::TracerFactoryCont
ProtobufMessage::ValidationVisitor& validation_visitor_;
};

class ConnectionManagerTracingConfigImpl : public ConnectionManagerTracingConfig {
public:
ConnectionManagerTracingConfigImpl(envoy::config::core::v3::TrafficDirection traffic_direction,
const ConnectionManagerTracingConfigProto& tracing_config) {

// Listener level traffic direction overrides the operation name
switch (traffic_direction) {
PANIC_ON_PROTO_ENUM_SENTINEL_VALUES;
case envoy::config::core::v3::UNSPECIFIED:
// Continuing legacy behavior; if unspecified, we treat this as ingress.
operation_name_ = Tracing::OperationName::Ingress;
break;
case envoy::config::core::v3::INBOUND:
operation_name_ = Tracing::OperationName::Ingress;
break;
case envoy::config::core::v3::OUTBOUND:
operation_name_ = Tracing::OperationName::Egress;
break;
}

for (const auto& tag : tracing_config.custom_tags()) {
custom_tags_.emplace(tag.tag(), Tracing::CustomTagUtility::createCustomTag(tag));
}

client_sampling_.set_numerator(
tracing_config.has_client_sampling() ? tracing_config.client_sampling().value() : 100);

// 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.
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::v3::FractionalPercent::TEN_THOUSAND);

uint64_t overall_sampling_numerator{PROTOBUF_PERCENT_TO_ROUNDED_INTEGER_OR_DEFAULT(
tracing_config, overall_sampling, 10000, 10000)};
overall_sampling_.set_numerator(overall_sampling_numerator);
overall_sampling_.set_denominator(envoy::type::v3::FractionalPercent::TEN_THOUSAND);

verbose_ = tracing_config.verbose();
max_path_tag_length_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(tracing_config, max_path_tag_length,
Tracing::DefaultMaxPathTagLength);
}

ConnectionManagerTracingConfigImpl(Tracing::OperationName operation_name,
Tracing::CustomTagMap custom_tags,
envoy::type::v3::FractionalPercent client_sampling,
envoy::type::v3::FractionalPercent random_sampling,
envoy::type::v3::FractionalPercent overall_sampling,
bool verbose, uint32_t max_path_tag_length)
: operation_name_(operation_name), custom_tags_(custom_tags),
client_sampling_(client_sampling), random_sampling_(random_sampling),
overall_sampling_(overall_sampling), verbose_(verbose),
max_path_tag_length_(max_path_tag_length) {}

ConnectionManagerTracingConfigImpl() = default;

const envoy::type::v3::FractionalPercent& getClientSampling() const override {
return client_sampling_;
}
const envoy::type::v3::FractionalPercent& getRandomSampling() const override {
return random_sampling_;
}
const envoy::type::v3::FractionalPercent& getOverallSampling() const override {
return overall_sampling_;
}
const Tracing::CustomTagMap& getCustomTags() const override { return custom_tags_; }

Tracing::OperationName operationName() const override { return operation_name_; }
bool verbose() const override { return verbose_; }
uint32_t maxPathTagLength() const override { return max_path_tag_length_; }

// TODO(wbpcode): keep this field be public for compatibility. Then the HCM needn't change much
// code to use this config.
Tracing::OperationName operation_name_{};
Tracing::CustomTagMap custom_tags_;
envoy::type::v3::FractionalPercent client_sampling_;
envoy::type::v3::FractionalPercent random_sampling_;
envoy::type::v3::FractionalPercent overall_sampling_;
bool verbose_{};
uint32_t max_path_tag_length_{};
};

} // namespace Tracing
} // namespace Envoy
Original file line number Diff line number Diff line change
Expand Up @@ -541,54 +541,8 @@ HttpConnectionManagerConfig::HttpConnectionManagerConfig(

if (config.has_tracing()) {
http_tracer_ = tracer_manager.getOrCreateTracer(getPerFilterTracerConfig(config));

const auto& tracing_config = config.tracing();

Tracing::OperationName tracing_operation_name;

// Listener level traffic direction overrides the operation name
switch (context.direction()) {
PANIC_ON_PROTO_ENUM_SENTINEL_VALUES;
case envoy::config::core::v3::UNSPECIFIED:
// Continuing legacy behavior; if unspecified, we treat this as ingress.
tracing_operation_name = Tracing::OperationName::Ingress;
break;
case envoy::config::core::v3::INBOUND:
tracing_operation_name = Tracing::OperationName::Ingress;
break;
case envoy::config::core::v3::OUTBOUND:
tracing_operation_name = Tracing::OperationName::Egress;
break;
}

Tracing::CustomTagMap custom_tags;
for (const auto& tag : tracing_config.custom_tags()) {
custom_tags.emplace(tag.tag(), Tracing::CustomTagUtility::createCustomTag(tag));
}

envoy::type::v3::FractionalPercent client_sampling;
client_sampling.set_numerator(
tracing_config.has_client_sampling() ? tracing_config.client_sampling().value() : 100);
envoy::type::v3::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.
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::v3::FractionalPercent::TEN_THOUSAND);
envoy::type::v3::FractionalPercent overall_sampling;
uint64_t overall_sampling_numerator{PROTOBUF_PERCENT_TO_ROUNDED_INTEGER_OR_DEFAULT(
tracing_config, overall_sampling, 10000, 10000)};
overall_sampling.set_numerator(overall_sampling_numerator);
overall_sampling.set_denominator(envoy::type::v3::FractionalPercent::TEN_THOUSAND);

const uint32_t max_path_tag_length = PROTOBUF_GET_WRAPPED_OR_DEFAULT(
tracing_config, max_path_tag_length, Tracing::DefaultMaxPathTagLength);

tracing_config_ =
std::make_unique<Http::TracingConnectionManagerConfig>(Http::TracingConnectionManagerConfig{
tracing_operation_name, custom_tags, client_sampling, random_sampling, overall_sampling,
tracing_config.verbose(), max_path_tag_length});
tracing_config_ = std::make_unique<Http::TracingConnectionManagerConfig>(context.direction(),
config.tracing());
}

for (const auto& access_log : config.access_log()) {
Expand Down
10 changes: 10 additions & 0 deletions test/common/tracing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,13 @@ envoy_cc_test(
"//test/test_common:test_runtime_lib",
],
)

envoy_cc_test(
name = "tracer_config_impl_test",
srcs = [
"tracer_config_impl_test.cc",
],
deps = [
"//source/common/tracing:tracer_config_lib",
],
)
63 changes: 63 additions & 0 deletions test/common/tracing/tracer_config_impl_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "source/common/tracing/tracer_config_impl.h"

#include "test/test_common/utility.h"

#include "gtest/gtest.h"

namespace Envoy {
namespace Tracing {
namespace {

TEST(ConnectionManagerTracingConfigImplTest, SimpleTest) {
{
envoy::config::core::v3::TrafficDirection traffic_direction =
envoy::config::core::v3::TrafficDirection::INBOUND;
ConnectionManagerTracingConfigProto tracing_config;
tracing_config.mutable_client_sampling()->set_value(50);
tracing_config.mutable_random_sampling()->set_value(50);
tracing_config.mutable_overall_sampling()->set_value(50);
tracing_config.set_verbose(true);
tracing_config.mutable_max_path_tag_length()->set_value(128);

auto* custom_tag = tracing_config.add_custom_tags();
custom_tag->set_tag("foo");
custom_tag->mutable_literal()->set_value("bar");

ConnectionManagerTracingConfigImpl config(traffic_direction, tracing_config);

EXPECT_EQ(Tracing::OperationName::Ingress, config.operationName());
EXPECT_EQ(true, config.verbose());
EXPECT_EQ(128, config.maxPathTagLength());
EXPECT_EQ(1, config.getCustomTags().size());

EXPECT_EQ(50, config.getClientSampling().numerator());
EXPECT_EQ(5000, config.getRandomSampling().numerator());
EXPECT_EQ(5000, config.getOverallSampling().numerator());
}

{
envoy::config::core::v3::TrafficDirection traffic_direction =
envoy::config::core::v3::TrafficDirection::OUTBOUND;
ConnectionManagerTracingConfigProto tracing_config;
tracing_config.set_verbose(true);

auto* custom_tag = tracing_config.add_custom_tags();
custom_tag->set_tag("foo");
custom_tag->mutable_literal()->set_value("bar");

ConnectionManagerTracingConfigImpl config(traffic_direction, tracing_config);

EXPECT_EQ(Tracing::OperationName::Egress, config.operationName());
EXPECT_EQ(true, config.verbose());
EXPECT_EQ(256, config.maxPathTagLength());
EXPECT_EQ(1, config.getCustomTags().size());

EXPECT_EQ(100, config.getClientSampling().numerator());
EXPECT_EQ(10000, config.getRandomSampling().numerator());
EXPECT_EQ(10000, config.getOverallSampling().numerator());
}
}

} // namespace
} // namespace Tracing
} // namespace Envoy

0 comments on commit 51b4927

Please sign in to comment.