Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h" 4 : #include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.validate.h" 5 : #include "envoy/protobuf/message_validator.h" 6 : #include "envoy/server/tracer_config.h" 7 : 8 : #include "source/common/tracing/custom_tag_impl.h" 9 : 10 : namespace Envoy { 11 : namespace Tracing { 12 : 13 : using ConnectionManagerTracingConfigProto = 14 : envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager_Tracing; 15 : 16 : class TracerFactoryContextImpl : public Server::Configuration::TracerFactoryContext { 17 : public: 18 : TracerFactoryContextImpl(Server::Configuration::ServerFactoryContext& server_factory_context, 19 : ProtobufMessage::ValidationVisitor& validation_visitor) 20 96 : : server_factory_context_(server_factory_context), validation_visitor_(validation_visitor) {} 21 0 : Server::Configuration::ServerFactoryContext& serverFactoryContext() override { 22 0 : return server_factory_context_; 23 0 : } 24 0 : ProtobufMessage::ValidationVisitor& messageValidationVisitor() override { 25 0 : return validation_visitor_; 26 0 : } 27 : 28 : private: 29 : Server::Configuration::ServerFactoryContext& server_factory_context_; 30 : ProtobufMessage::ValidationVisitor& validation_visitor_; 31 : }; 32 : 33 : class ConnectionManagerTracingConfigImpl : public ConnectionManagerTracingConfig { 34 : public: 35 : ConnectionManagerTracingConfigImpl(envoy::config::core::v3::TrafficDirection traffic_direction, 36 0 : const ConnectionManagerTracingConfigProto& tracing_config) { 37 : 38 : // Listener level traffic direction overrides the operation name 39 0 : switch (traffic_direction) { 40 0 : PANIC_ON_PROTO_ENUM_SENTINEL_VALUES; 41 0 : case envoy::config::core::v3::UNSPECIFIED: 42 : // Continuing legacy behavior; if unspecified, we treat this as ingress. 43 0 : operation_name_ = Tracing::OperationName::Ingress; 44 0 : break; 45 0 : case envoy::config::core::v3::INBOUND: 46 0 : operation_name_ = Tracing::OperationName::Ingress; 47 0 : break; 48 0 : case envoy::config::core::v3::OUTBOUND: 49 0 : operation_name_ = Tracing::OperationName::Egress; 50 0 : break; 51 0 : } 52 : 53 0 : spawn_upstream_span_ = 54 0 : PROTOBUF_GET_WRAPPED_OR_DEFAULT(tracing_config, spawn_upstream_span, false); 55 : 56 0 : for (const auto& tag : tracing_config.custom_tags()) { 57 0 : custom_tags_.emplace(tag.tag(), Tracing::CustomTagUtility::createCustomTag(tag)); 58 0 : } 59 : 60 0 : client_sampling_.set_numerator( 61 0 : tracing_config.has_client_sampling() ? tracing_config.client_sampling().value() : 100); 62 : 63 : // TODO: Random sampling historically was an integer and default to out of 10,000. We should 64 : // deprecate that and move to a straight fractional percent config. 65 0 : uint64_t random_sampling_numerator{PROTOBUF_PERCENT_TO_ROUNDED_INTEGER_OR_DEFAULT( 66 0 : tracing_config, random_sampling, 10000, 10000)}; 67 0 : random_sampling_.set_numerator(random_sampling_numerator); 68 0 : random_sampling_.set_denominator(envoy::type::v3::FractionalPercent::TEN_THOUSAND); 69 : 70 0 : uint64_t overall_sampling_numerator{PROTOBUF_PERCENT_TO_ROUNDED_INTEGER_OR_DEFAULT( 71 0 : tracing_config, overall_sampling, 10000, 10000)}; 72 0 : overall_sampling_.set_numerator(overall_sampling_numerator); 73 0 : overall_sampling_.set_denominator(envoy::type::v3::FractionalPercent::TEN_THOUSAND); 74 : 75 0 : verbose_ = tracing_config.verbose(); 76 0 : max_path_tag_length_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(tracing_config, max_path_tag_length, 77 0 : Tracing::DefaultMaxPathTagLength); 78 0 : } 79 : 80 : ConnectionManagerTracingConfigImpl(Tracing::OperationName operation_name, 81 : Tracing::CustomTagMap custom_tags, 82 : envoy::type::v3::FractionalPercent client_sampling, 83 : envoy::type::v3::FractionalPercent random_sampling, 84 : envoy::type::v3::FractionalPercent overall_sampling, 85 : bool verbose, uint32_t max_path_tag_length) 86 : : operation_name_(operation_name), custom_tags_(custom_tags), 87 : client_sampling_(client_sampling), random_sampling_(random_sampling), 88 : overall_sampling_(overall_sampling), verbose_(verbose), 89 0 : max_path_tag_length_(max_path_tag_length) {} 90 : 91 : ConnectionManagerTracingConfigImpl() = default; 92 : 93 0 : const envoy::type::v3::FractionalPercent& getClientSampling() const override { 94 0 : return client_sampling_; 95 0 : } 96 0 : const envoy::type::v3::FractionalPercent& getRandomSampling() const override { 97 0 : return random_sampling_; 98 0 : } 99 0 : const envoy::type::v3::FractionalPercent& getOverallSampling() const override { 100 0 : return overall_sampling_; 101 0 : } 102 0 : const Tracing::CustomTagMap& getCustomTags() const override { return custom_tags_; } 103 : 104 0 : Tracing::OperationName operationName() const override { return operation_name_; } 105 0 : bool verbose() const override { return verbose_; } 106 0 : uint32_t maxPathTagLength() const override { return max_path_tag_length_; } 107 0 : bool spawnUpstreamSpan() const override { return spawn_upstream_span_; } 108 : 109 : // TODO(wbpcode): keep this field be public for compatibility. Then the HCM needn't change much 110 : // code to use this config. 111 : Tracing::OperationName operation_name_{}; 112 : Tracing::CustomTagMap custom_tags_; 113 : envoy::type::v3::FractionalPercent client_sampling_; 114 : envoy::type::v3::FractionalPercent random_sampling_; 115 : envoy::type::v3::FractionalPercent overall_sampling_; 116 : bool verbose_{}; 117 : uint32_t max_path_tag_length_{}; 118 : bool spawn_upstream_span_{}; 119 : }; 120 : 121 : } // namespace Tracing 122 : } // namespace Envoy