Skip to content

Commit 68e64b4

Browse files
wbpcodewangbaiping(wbpcode)
andauthored
formatter: no template is necessary for logger/formatter now (envoyproxy#38196)
Commit Message: formatter: no template is necessary now Additional Description: continuous work after envoyproxy#38164. Risk Level: low. no any logic change. Testing: n/a. Docs Changes: n/a. Release Notes: n/a. Platform Specific Features: n/a. --------- Signed-off-by: wangbaiping(wbpcode) <[email protected]> Signed-off-by: wangbaiping(wbpcode) <[email protected]> Co-authored-by: wangbaiping(wbpcode) <[email protected]>
1 parent 46f23ed commit 68e64b4

File tree

27 files changed

+608
-777
lines changed

27 files changed

+608
-777
lines changed

envoy/access_log/access_log.h

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
namespace Envoy {
1616
namespace AccessLog {
1717

18+
using LogContext = Formatter::Context;
19+
1820
class AccessLogFile {
1921
public:
2022
virtual ~AccessLogFile() = default;
@@ -59,49 +61,35 @@ using AccessLogManagerPtr = std::unique_ptr<AccessLogManager>;
5961
using AccessLogType = envoy::data::accesslog::v3::AccessLogType;
6062

6163
/**
62-
* Templated interface for access log filters.
64+
* Interface for access log filters.
6365
*/
64-
template <class Context> class FilterBase {
66+
class Filter {
6567
public:
66-
virtual ~FilterBase() = default;
68+
virtual ~Filter() = default;
6769

6870
/**
6971
* Evaluate whether an access log should be written based on request and response data.
7072
* @return TRUE if the log should be written.
7173
*/
72-
virtual bool evaluate(const Context& context, const StreamInfo::StreamInfo& info) const PURE;
74+
virtual bool evaluate(const LogContext& context, const StreamInfo::StreamInfo& info) const PURE;
7375
};
74-
template <class Context> using FilterBasePtr = std::unique_ptr<FilterBase<Context>>;
76+
using FilterPtr = std::unique_ptr<Filter>;
7577

7678
/**
77-
* Templated interface for access log instances.
78-
* TODO(wbpcode): refactor existing access log instances and related other interfaces to use this
79-
* interface. See https://github.com/envoyproxy/envoy/issues/28773.
79+
* Interface for access log instances.
8080
*/
81-
template <class Context> class InstanceBase {
81+
class Instance {
8282
public:
83-
virtual ~InstanceBase() = default;
83+
virtual ~Instance() = default;
8484

8585
/**
8686
* Log a completed request.
8787
* @param context supplies the context for the log.
8888
* @param stream_info supplies additional information about the request not
8989
* contained in the request headers.
9090
*/
91-
virtual void log(const Context& context, const StreamInfo::StreamInfo& stream_info) PURE;
91+
virtual void log(const LogContext& context, const StreamInfo::StreamInfo& stream_info) PURE;
9292
};
93-
template <class Context> using InstanceBaseSharedPtr = std::shared_ptr<InstanceBase<Context>>;
94-
95-
/**
96-
* Interface for HTTP access log filters.
97-
*/
98-
using Filter = FilterBase<Formatter::HttpFormatterContext>;
99-
using FilterPtr = std::unique_ptr<Filter>;
100-
101-
/**
102-
* Abstract access logger for HTTP requests and TCP connections.
103-
*/
104-
using Instance = InstanceBase<Formatter::HttpFormatterContext>;
10593
using InstanceSharedPtr = std::shared_ptr<Instance>;
10694
using InstanceSharedPtrVector = std::vector<InstanceSharedPtr>;
10795

envoy/access_log/access_log_config.h

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ namespace AccessLog {
1515
/**
1616
* Extension filter factory that reads from ExtensionFilter proto.
1717
*/
18-
template <class Context> class ExtensionFilterFactoryBase : public Config::TypedFactory {
18+
class ExtensionFilterFactory : public Config::TypedFactory {
1919
public:
20-
ExtensionFilterFactoryBase() : category_(categoryByType()) {}
21-
22-
~ExtensionFilterFactoryBase() override = default;
23-
2420
/**
2521
* Create a particular extension filter implementation from a config proto. If the
2622
* implementation is unable to produce a filter with the provided parameters, it should throw an
@@ -29,40 +25,18 @@ template <class Context> class ExtensionFilterFactoryBase : public Config::Typed
2925
* @param context supplies the factory context.
3026
* @return an instance of extension filter implementation from a config proto.
3127
*/
32-
virtual FilterBasePtr<Context>
33-
createFilter(const envoy::config::accesslog::v3::ExtensionFilter& config,
34-
Server::Configuration::FactoryContext& context) PURE;
35-
36-
std::string category() const override { return category_; }
37-
38-
private:
39-
std::string categoryByType() {
40-
if constexpr (std::is_same_v<Context, Formatter::HttpFormatterContext>) {
41-
// This is a special case for the HTTP formatter context to ensure backwards compatibility.
42-
return "envoy.access_loggers.extension_filters";
43-
} else {
44-
return fmt::format("envoy.{}.access_loggers.extension_filters", Context::category());
45-
}
46-
}
28+
virtual FilterPtr createFilter(const envoy::config::accesslog::v3::ExtensionFilter& config,
29+
Server::Configuration::FactoryContext& context) PURE;
4730

48-
const std::string category_;
31+
std::string category() const override { return "envoy.access_loggers.extension_filters"; }
4932
};
5033

51-
/**
52-
* Extension filter factory that reads from ExtensionFilter proto.
53-
*/
54-
using ExtensionFilterFactory = ExtensionFilterFactoryBase<Formatter::HttpFormatterContext>;
55-
5634
/**
5735
* Implemented for each AccessLog::Instance and registered via Registry::registerFactory or the
5836
* convenience class RegisterFactory.
5937
*/
60-
template <class Context> class AccessLogInstanceFactoryBase : public Config::TypedFactory {
38+
class AccessLogInstanceFactory : public Config::TypedFactory {
6139
public:
62-
AccessLogInstanceFactoryBase() : category_(categoryByType()) {}
63-
64-
~AccessLogInstanceFactoryBase() override = default;
65-
6640
/**
6741
* Create a particular AccessLog::Instance implementation from a config proto. If the
6842
* implementation is unable to produce a factory with the provided parameters, it should throw an
@@ -74,27 +48,13 @@ template <class Context> class AccessLogInstanceFactoryBase : public Config::Typ
7448
* @param command_parsers vector of command parsers that provide by the caller to be used for
7549
* parsing custom substitution commands.
7650
*/
77-
virtual AccessLog::InstanceBaseSharedPtr<Context> createAccessLogInstance(
78-
const Protobuf::Message& config, AccessLog::FilterBasePtr<Context>&& filter,
79-
Server::Configuration::FactoryContext& context,
80-
std::vector<Formatter::CommandParserBasePtr<Context>>&& command_parsers = {}) PURE;
51+
virtual AccessLog::InstanceSharedPtr
52+
createAccessLogInstance(const Protobuf::Message& config, AccessLog::FilterPtr&& filter,
53+
Server::Configuration::FactoryContext& context,
54+
std::vector<Formatter::CommandParserPtr>&& command_parsers = {}) PURE;
8155

82-
std::string category() const override { return category_; }
83-
84-
private:
85-
std::string categoryByType() {
86-
if constexpr (std::is_same_v<Context, Formatter::HttpFormatterContext>) {
87-
// This is a special case for the HTTP formatter context to ensure backwards compatibility.
88-
return "envoy.access_loggers";
89-
} else {
90-
return fmt::format("envoy.{}.access_loggers", Context::category());
91-
}
92-
}
93-
94-
const std::string category_;
56+
std::string category() const override { return "envoy.access_loggers"; }
9557
};
9658

97-
using AccessLogInstanceFactory = AccessLogInstanceFactoryBase<Formatter::HttpFormatterContext>;
98-
9959
} // namespace AccessLog
10060
} // namespace Envoy

envoy/formatter/substitution_formatter.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,5 @@
55
#include "envoy/http/header_map.h"
66

77
namespace Envoy {
8-
namespace Formatter {
9-
10-
using Formatter = FormatterBase<HttpFormatterContext>;
11-
using FormatterPtr = std::unique_ptr<Formatter>;
12-
using FormatterConstSharedPtr = std::shared_ptr<const Formatter>;
13-
14-
using FormatterProvider = FormatterProviderBase<HttpFormatterContext>;
15-
using FormatterProviderPtr = std::unique_ptr<FormatterProvider>;
16-
17-
using CommandParser = CommandParserBase<HttpFormatterContext>;
18-
using CommandParserPtr = std::unique_ptr<CommandParser>;
19-
using CommandParserFactory = CommandParserFactoryBase<HttpFormatterContext>;
20-
21-
} // namespace Formatter
8+
namespace Formatter {} // namespace Formatter
229
} // namespace Envoy

envoy/formatter/substitution_formatter_base.h

Lines changed: 27 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "envoy/access_log/access_log.h"
77
#include "envoy/common/pure.h"
88
#include "envoy/config/typed_config.h"
9+
#include "envoy/formatter/http_formatter_context.h"
910
#include "envoy/registry/registry.h"
1011
#include "envoy/server/factory_context.h"
1112
#include "envoy/stream_info/stream_info.h"
@@ -14,31 +15,31 @@ namespace Envoy {
1415
namespace Formatter {
1516

1617
/**
17-
* Template interface for multiple protocols/modules formatters.
18+
* Interface for multiple protocols/modules formatters.
1819
*/
19-
template <class FormatterContext> class FormatterBase {
20+
class Formatter {
2021
public:
21-
virtual ~FormatterBase() = default;
22+
virtual ~Formatter() = default;
2223

2324
/**
2425
* Return a formatted substitution line.
2526
* @param context supplies the formatter context.
2627
* @param stream_info supplies the stream info.
2728
* @return std::string string containing the complete formatted substitution line.
2829
*/
29-
virtual std::string formatWithContext(const FormatterContext& context,
30+
virtual std::string formatWithContext(const Context& context,
3031
const StreamInfo::StreamInfo& stream_info) const PURE;
3132
};
3233

33-
template <class FormatterContext>
34-
using FormatterBasePtr = std::unique_ptr<FormatterBase<FormatterContext>>;
34+
using FormatterPtr = std::unique_ptr<Formatter>;
35+
using FormatterConstSharedPtr = std::shared_ptr<const Formatter>;
3536

3637
/**
37-
* Template interface for multiple protocols/modules formatter providers.
38+
* Interface for multiple protocols/modules formatter providers.
3839
*/
39-
template <class FormatterContext> class FormatterProviderBase {
40+
class FormatterProvider {
4041
public:
41-
virtual ~FormatterProviderBase() = default;
42+
virtual ~FormatterProvider() = default;
4243

4344
/**
4445
* Format the value with the given context and stream info.
@@ -48,8 +49,7 @@ template <class FormatterContext> class FormatterProviderBase {
4849
* the given context and stream info.
4950
*/
5051
virtual absl::optional<std::string>
51-
formatWithContext(const FormatterContext& context,
52-
const StreamInfo::StreamInfo& stream_info) const PURE;
52+
formatWithContext(const Context& context, const StreamInfo::StreamInfo& stream_info) const PURE;
5353

5454
/**
5555
* Format the value with the given context and stream info.
@@ -59,16 +59,15 @@ template <class FormatterContext> class FormatterProviderBase {
5959
* context and stream info.
6060
*/
6161
virtual ProtobufWkt::Value
62-
formatValueWithContext(const FormatterContext& context,
62+
formatValueWithContext(const Context& context,
6363
const StreamInfo::StreamInfo& stream_info) const PURE;
6464
};
6565

66-
template <class FormatterContext>
67-
using FormatterProviderBasePtr = std::unique_ptr<FormatterProviderBase<FormatterContext>>;
66+
using FormatterProviderPtr = std::unique_ptr<FormatterProvider>;
6867

69-
template <class FormatterContext> class CommandParserBase {
68+
class CommandParser {
7069
public:
71-
virtual ~CommandParserBase() = default;
70+
virtual ~CommandParser() = default;
7271

7372
/**
7473
* Return a FormatterProviderBasePtr if command arg and max_length are correct for the formatter
@@ -80,15 +79,13 @@ template <class FormatterContext> class CommandParserBase {
8079
*
8180
* @return FormattterProviderPtr substitution provider for the parsed command.
8281
*/
83-
virtual FormatterProviderBasePtr<FormatterContext>
84-
parse(absl::string_view command, absl::string_view command_arg,
85-
absl::optional<size_t> max_length) const PURE;
82+
virtual FormatterProviderPtr parse(absl::string_view command, absl::string_view command_arg,
83+
absl::optional<size_t> max_length) const PURE;
8684
};
8785

88-
template <class FormatterContext>
89-
using CommandParserBasePtr = std::unique_ptr<CommandParserBase<FormatterContext>>;
86+
using CommandParserPtr = std::unique_ptr<CommandParser>;
9087

91-
template <class FormatterContext> class CommandParserFactoryBase : public Config::TypedFactory {
88+
class CommandParserFactory : public Config::TypedFactory {
9289
public:
9390
/**
9491
* Creates a particular CommandParser implementation.
@@ -98,40 +95,30 @@ template <class FormatterContext> class CommandParserFactoryBase : public Config
9895
* @return CommandParserPtr the CommandParser which will be used in
9996
* SubstitutionFormatParser::parse() when evaluating an access log format string.
10097
*/
101-
virtual CommandParserBasePtr<FormatterContext>
98+
virtual CommandParserPtr
10299
createCommandParserFromProto(const Protobuf::Message& config,
103100
Server::Configuration::GenericFactoryContext& context) PURE;
104101

105-
std::string category() const override {
106-
static constexpr absl::string_view HttpContextCategory = "http";
107-
if constexpr (FormatterContext::category() == HttpContextCategory) {
108-
return "envoy.formatter"; // Backward compatibility for HTTP.
109-
} else {
110-
return fmt::format("envoy.formatters.{}", FormatterContext::category());
111-
}
112-
}
102+
std::string category() const override { return "envoy.formatter"; }
113103
};
114104

115-
template <class FormatterContext>
116-
class BuiltInCommandParserFactoryBase : public Config::UntypedFactory {
105+
class BuiltInCommandParserFactory : public Config::UntypedFactory {
117106
public:
118-
std::string category() const override {
119-
return fmt::format("envoy.built_in_formatters.{}", FormatterContext::category());
120-
}
107+
std::string category() const override { return "envoy.built_in_formatters"; }
121108

122109
/**
123110
* Creates a particular CommandParser implementation.
124111
*/
125-
virtual CommandParserBasePtr<FormatterContext> createCommandParser() const PURE;
112+
virtual CommandParserPtr createCommandParser() const PURE;
126113
};
127114

128115
/**
129116
* Helper class to get all built-in command parsers for a given formatter context.
130117
*/
131-
template <class FormatterContext> class BuiltInCommandParserFactoryHelper {
118+
class BuiltInCommandParserFactoryHelper {
132119
public:
133-
using Factory = BuiltInCommandParserFactoryBase<FormatterContext>;
134-
using Parsers = std::vector<CommandParserBasePtr<FormatterContext>>;
120+
using Factory = BuiltInCommandParserFactory;
121+
using Parsers = std::vector<CommandParserPtr>;
135122

136123
/**
137124
* Get all built-in command parsers for a given formatter context.
@@ -153,46 +140,5 @@ template <class FormatterContext> class BuiltInCommandParserFactoryHelper {
153140
}
154141
};
155142

156-
/**
157-
* Type placeholder for formatter providers that only require StreamInfo.
158-
*/
159-
struct StreamInfoOnlyFormatterContext {
160-
static constexpr absl::string_view category() { return "stream_info"; }
161-
};
162-
163-
/**
164-
* Template specialization for formatter providers that only require StreamInfo.
165-
* The only difference from the main template is the providers inheriting from this class
166-
* will only take StreamInfo as input parameter.
167-
*/
168-
template <> class FormatterProviderBase<StreamInfoOnlyFormatterContext> {
169-
public:
170-
virtual ~FormatterProviderBase() = default;
171-
172-
/**
173-
* Format the value with the given stream info.
174-
* @param stream_info supplies the stream info.
175-
* @return absl::optional<std::string> optional string containing a single value extracted from
176-
* the given stream info.
177-
*/
178-
virtual absl::optional<std::string> format(const StreamInfo::StreamInfo& stream_info) const PURE;
179-
180-
/**
181-
* Format the value with the given stream info.
182-
* @param stream_info supplies the stream info.
183-
* @return ProtobufWkt::Value containing a single value extracted from the given stream info.
184-
*/
185-
virtual ProtobufWkt::Value formatValue(const StreamInfo::StreamInfo& stream_info) const PURE;
186-
};
187-
188-
using StreamInfoFormatterProvider = FormatterProviderBase<StreamInfoOnlyFormatterContext>;
189-
using StreamInfoFormatterProviderPtr = FormatterProviderBasePtr<StreamInfoOnlyFormatterContext>;
190-
using StreamInfoCommandParser = CommandParserBase<StreamInfoOnlyFormatterContext>;
191-
using StreamInfoCommandParserPtr = CommandParserBasePtr<StreamInfoOnlyFormatterContext>;
192-
using BuiltInStreamInfoCommandParserFactory =
193-
BuiltInCommandParserFactoryBase<StreamInfoOnlyFormatterContext>;
194-
using BuiltInStreamInfoCommandParserFactoryHelper =
195-
BuiltInCommandParserFactoryHelper<StreamInfoOnlyFormatterContext>;
196-
197143
} // namespace Formatter
198144
} // namespace Envoy

source/common/formatter/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ envoy_cc_library(
3535

3636
envoy_cc_library(
3737
name = "substitution_format_string_lib",
38+
srcs = ["substitution_format_string.cc"],
3839
hdrs = ["substitution_format_string.h"],
3940
deps = [
4041
":substitution_formatter_lib",

0 commit comments

Comments
 (0)