Skip to content

Commit 687821c

Browse files
committed
Add ability to give HTTP Filters names, and nameWithFallback utility to resolve it
Signed-off-by: Nick Shipilov <nick.shipilov.n@gmail.com>
1 parent c8e2c63 commit 687821c

4 files changed

Lines changed: 42 additions & 0 deletions

File tree

envoy/http/filter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,11 @@ class StreamFilterBase {
888888
public:
889889
virtual ~StreamFilterBase() = default;
890890

891+
/**
892+
* A name for the filter. Used for monitoring and disagnostics.
893+
*/
894+
virtual absl::string_view name() { return {}; }
895+
891896
/**
892897
* This routine is called before the access log handlers' final log() is called. Filters can use
893898
* this callback to enrich the data passed in to the log handlers.

source/common/http/utility.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,14 @@ std::string Utility::newUri(::Envoy::OptRef<const Utility::RedirectConfig> redir
16411641
return fmt::format("{}://{}{}{}", final_scheme, final_host, final_port, final_path);
16421642
}
16431643

1644+
absl::string_view Utility::nameWithFallback(StreamFilterBase& stream_filter) {
1645+
absl::string_view name = stream_filter.name();
1646+
if (!name.empty()) {
1647+
return name;
1648+
}
1649+
return typeid(stream_filter).name();
1650+
}
1651+
16441652
bool Utility::isValidRefererValue(absl::string_view value) {
16451653

16461654
// First, we try to parse it as an absolute URL and

source/common/http/utility.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@ bool schemeIsHttps(const absl::string_view scheme);
728728
std::string newUri(::Envoy::OptRef<const RedirectConfig> redirect_config,
729729
const Http::RequestHeaderMap& headers);
730730

731+
/*
732+
* Returns the filter's name, if empty, resolves it using RTTI.
733+
*/
734+
absl::string_view nameWithFallback(StreamFilterBase& stream_filter);
735+
731736
} // namespace Utility
732737
} // namespace Http
733738
} // namespace Envoy

test/common/http/utility_test.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,5 +2055,29 @@ TEST(HeaderIsValidTest, SchemeIsHttps) {
20552055
EXPECT_FALSE(Utility::schemeIsHttps("http"));
20562056
}
20572057

2058+
class ExampleFilterWithName : public PassThroughDecoderFilter {
2059+
public:
2060+
virtual ~ExampleFilterWithName() = default;
2061+
2062+
static constexpr absl::string_view filter_name{"example_filter"};
2063+
absl::string_view name() override { return ExampleFilterWithName::filter_name; }
2064+
};
2065+
2066+
class ExampleFilterWithoutName : public PassThroughDecoderFilter {
2067+
public:
2068+
virtual ~ExampleFilterWithoutName() = default;
2069+
};
2070+
2071+
TEST(TestNameWithFallback, ProvidesName) {
2072+
ExampleFilterWithName filter_with_name;
2073+
EXPECT_EQ(Utility::nameWithFallback(filter_with_name), "example_filter");
2074+
2075+
// Check that there is *some* name returned, for the fallback case.
2076+
// Different compilers handle this differently --
2077+
// some return mangled names directly, others return human readable names.
2078+
ExampleFilterWithoutName filter_without_name;
2079+
EXPECT_FALSE(Utility::nameWithFallback(filter_without_name).empty());
2080+
}
2081+
20582082
} // namespace Http
20592083
} // namespace Envoy

0 commit comments

Comments
 (0)