Skip to content

Commit bdfd1e9

Browse files
authored
generic proxy: supported custom substitution format specifier for tracing custom tags (#41842)
Commit Message: generic proxy: supported custom substitution format specifier for tracing custom tags Additional Description: Supported custom substitution format specifier for tracing custom tags in the generic proxy. Risk Level: low. Testing: unit. Docs Changes: added. Release Notes: n/a. Platform Specific Features: n/a. --------- Signed-off-by: WangBaiping <[email protected]>
1 parent ac003d7 commit bdfd1e9

File tree

12 files changed

+57
-12
lines changed

12 files changed

+57
-12
lines changed

changelogs/current.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ new_features:
163163
Added new :ref:`value <envoy_v3_api_field_type.tracing.v3.CustomTag.value>` field and
164164
the :ref:`substitution format specifier <config_access_log_format>` could be used to
165165
extract values from various parts of the request/response for custom tags.
166+
- area: generic_proxy
167+
change: |
168+
Added custom substitution format specifiers support in the tracing custom tags of the
169+
:ref:`generic_proxy
170+
filter <envoy_v3_api_msg_extensions.filters.network.generic_proxy.v3.GenericProxy>`.
171+
Now the ``%REQUEST_PROPERTY%``, ``%RESPONSE_PROPERTY%`` etc. can be used in the
172+
:ref:`value <envoy_v3_api_field_type.tracing.v3.CustomTag.value>` field for generic proxy.
166173
- area: lua
167174
change: |
168175
Added ``drainConnectionUponCompletion()`` to the Lua filter stream info API. This allows Lua scripts

docs/root/configuration/listeners/network_filters/generic_proxy_filter.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ are supported in the access log format. In addition, the generic proxy also supp
153153
* ``%RESPONSE_PROPERTY(X)%``: The response property of generic response. The ``X`` is the property name. The value value depends on the
154154
application codec implementation.
155155

156+
The above custom substitution format specifiers could be used in the access log format and also the tracing custom tags value field.
156157

157158
Generic proxy statistics
158159
------------------------

envoy/formatter/substitution_formatter_base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class CommandParser {
8383
};
8484

8585
using CommandParserPtr = std::unique_ptr<CommandParser>;
86+
using CommandParserPtrVector = std::vector<CommandParserPtr>;
8687

8788
class CommandParserFactory : public Config::TypedFactory {
8889
public:

source/common/tracing/custom_tag_impl.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ MetadataCustomTag::metadata(const CustomTagContext& ctx) const {
146146
}
147147
}
148148

149-
FormatterCustomTag::FormatterCustomTag(absl::string_view tag, absl::string_view value) : tag_(tag) {
150-
auto formatter_or = Formatter::FormatterImpl::create(value, true);
149+
FormatterCustomTag::FormatterCustomTag(absl::string_view tag, absl::string_view value,
150+
const Formatter::CommandParserPtrVector& command_parsers)
151+
: tag_(tag) {
152+
auto formatter_or = Formatter::FormatterImpl::create(value, true, command_parsers);
151153
THROW_IF_NOT_OK_REF(formatter_or.status());
152154
formatter_ = std::move(formatter_or.value());
153155
}
@@ -171,7 +173,8 @@ void FormatterCustomTag::applyLog(envoy::data::accesslog::v3::AccessLogCommon& e
171173
}
172174

173175
CustomTagConstSharedPtr
174-
CustomTagUtility::createCustomTag(const envoy::type::tracing::v3::CustomTag& tag) {
176+
CustomTagUtility::createCustomTag(const envoy::type::tracing::v3::CustomTag& tag,
177+
const Formatter::CommandParserPtrVector& command_parsers) {
175178
switch (tag.type_case()) {
176179
case envoy::type::tracing::v3::CustomTag::TypeCase::kLiteral:
177180
return std::make_shared<const Tracing::LiteralCustomTag>(tag.tag(), tag.literal());
@@ -182,7 +185,8 @@ CustomTagUtility::createCustomTag(const envoy::type::tracing::v3::CustomTag& tag
182185
case envoy::type::tracing::v3::CustomTag::TypeCase::kMetadata:
183186
return std::make_shared<const Tracing::MetadataCustomTag>(tag.tag(), tag.metadata());
184187
case envoy::type::tracing::v3::CustomTag::TypeCase::kValue:
185-
return std::make_shared<const Tracing::FormatterCustomTag>(tag.tag(), tag.value());
188+
return std::make_shared<const Tracing::FormatterCustomTag>(tag.tag(), tag.value(),
189+
command_parsers);
186190
case envoy::type::tracing::v3::CustomTag::TypeCase::TYPE_NOT_SET:
187191
break; // Panic below.
188192
}

source/common/tracing/custom_tag_impl.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class MetadataCustomTag : public CustomTagBase {
7878

7979
class FormatterCustomTag : public CustomTag {
8080
public:
81-
FormatterCustomTag(absl::string_view tag, absl::string_view value);
81+
FormatterCustomTag(absl::string_view tag, absl::string_view value,
82+
const Formatter::CommandParserPtrVector& command_parsers = {});
8283

8384
absl::string_view tag() const override { return tag_; }
8485
void applySpan(Span& span, const CustomTagContext& ctx) const override;
@@ -96,7 +97,9 @@ class CustomTagUtility {
9697
* Create a custom tag according to the configuration.
9798
* @param tag a tracing custom tag configuration.
9899
*/
99-
static CustomTagConstSharedPtr createCustomTag(const envoy::type::tracing::v3::CustomTag& tag);
100+
static CustomTagConstSharedPtr
101+
createCustomTag(const envoy::type::tracing::v3::CustomTag& tag,
102+
const Formatter::CommandParserPtrVector& command_parsers = {});
100103
};
101104

102105
} // namespace Tracing

source/common/tracing/tracer_config_impl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class TracerFactoryContextImpl : public Server::Configuration::TracerFactoryCont
3333
class ConnectionManagerTracingConfig {
3434
public:
3535
ConnectionManagerTracingConfig(envoy::config::core::v3::TrafficDirection traffic_direction,
36-
const ConnectionManagerTracingConfigProto& tracing_config) {
36+
const ConnectionManagerTracingConfigProto& tracing_config,
37+
const Formatter::CommandParserPtrVector& command_parsers = {}) {
3738

3839
// Listener level traffic direction overrides the operation name
3940
switch (traffic_direction) {
@@ -54,7 +55,8 @@ class ConnectionManagerTracingConfig {
5455
PROTOBUF_GET_WRAPPED_OR_DEFAULT(tracing_config, spawn_upstream_span, false);
5556

5657
for (const auto& tag : tracing_config.custom_tags()) {
57-
custom_tags_.emplace(tag.tag(), Tracing::CustomTagUtility::createCustomTag(tag));
58+
custom_tags_.emplace(tag.tag(),
59+
Tracing::CustomTagUtility::createCustomTag(tag, command_parsers));
5860
}
5961

6062
client_sampling_.set_numerator(

source/extensions/filters/network/generic_proxy/config.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ Factory::createFilterFactoryFromProtoTyped(const ProxyConfig& proto_config,
118118
if (proto_config.tracing().has_provider()) {
119119
tracer = tracer_manager->getOrCreateTracer(&proto_config.tracing().provider());
120120
}
121+
std::vector<Formatter::CommandParserPtr> command_parsers;
122+
command_parsers.push_back(createGenericProxyCommandParser());
121123
tracing_config = std::make_unique<Tracing::ConnectionManagerTracingConfig>(
122-
context.listenerInfo().direction(), proto_config.tracing());
124+
context.listenerInfo().direction(), proto_config.tracing(), command_parsers);
123125
}
124126

125127
// Access log configuration.

source/extensions/filters/network/generic_proxy/proxy.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ void ActiveStream::modifySpan(Tracing::Span& span) const {
9696
const FormatterContextExtension context_extension(request_header_frame_.get(),
9797
response_header_frame_.get());
9898
Formatter::Context context;
99+
context.setExtension(context_extension);
99100
const Tracing::CustomTagContext ctx{trace_context, stream_info_, context};
100101

101102
for (const auto& it : conn_manager_tracing_config_->getCustomTags()) {

test/common/tracing/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ envoy_cc_test(
8787
],
8888
rbe_pool = "6gig",
8989
deps = [
90+
"//source/common/formatter:formatter_extension_lib",
9091
"//source/common/tracing:tracer_config_lib",
9192
],
9293
)

test/common/tracing/tracer_config_impl_test.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ TEST(ConnectionManagerTracingConfigImplTest, SimpleTest) {
4444
auto* custom_tag = tracing_config.add_custom_tags();
4545
custom_tag->set_tag("foo");
4646
custom_tag->mutable_literal()->set_value("bar");
47+
auto* custom_tag2 = tracing_config.add_custom_tags();
48+
custom_tag2->set_tag("dynamic_foo");
49+
custom_tag2->set_value("%REQ(X-FOO)%");
4750

4851
ConnectionManagerTracingConfig config(traffic_direction, tracing_config);
4952

5053
EXPECT_EQ(Tracing::OperationName::Egress, config.operationName());
5154
EXPECT_EQ(true, config.verbose());
5255
EXPECT_EQ(256, config.maxPathTagLength());
53-
EXPECT_EQ(1, config.getCustomTags().size());
56+
EXPECT_EQ(2, config.getCustomTags().size());
5457

5558
EXPECT_EQ(100, config.getClientSampling().numerator());
5659
EXPECT_EQ(10000, config.getRandomSampling().numerator());

0 commit comments

Comments
 (0)