Line data Source code
1 : #pragma once 2 : 3 : #include <string> 4 : 5 : #include "envoy/access_log/access_log.h" 6 : #include "envoy/config/typed_config.h" 7 : #include "envoy/server/filter_config.h" 8 : 9 : #include "source/common/protobuf/protobuf.h" 10 : 11 : namespace Envoy { 12 : namespace AccessLog { 13 : 14 : /** 15 : * Extension filter factory that reads from ExtensionFilter proto. 16 : */ 17 : template <class Context> class ExtensionFilterFactoryBase : public Config::TypedFactory { 18 : public: 19 2 : ExtensionFilterFactoryBase() : category_(categoryByType()) {} 20 : 21 0 : ~ExtensionFilterFactoryBase() override = default; 22 : 23 : /** 24 : * Create a particular extension filter implementation from a config proto. If the 25 : * implementation is unable to produce a filter with the provided parameters, it should throw an 26 : * EnvoyException. The returned pointer should never be nullptr. 27 : * @param config supplies the custom configuration for this filter type. 28 : * @param context supplies the factory context. 29 : * @return an instance of extension filter implementation from a config proto. 30 : */ 31 : virtual FilterBasePtr<Context> 32 : createFilter(const envoy::config::accesslog::v3::ExtensionFilter& config, 33 : Server::Configuration::FactoryContext& context) PURE; 34 : 35 4 : std::string category() const override { return category_; } 36 : 37 : private: 38 2 : std::string categoryByType() { 39 2 : if constexpr (std::is_same_v<Context, Formatter::HttpFormatterContext>) { 40 : // This is a special case for the HTTP formatter context to ensure backwards compatibility. 41 2 : return "envoy.access_loggers.extension_filters"; 42 2 : } else { 43 2 : return fmt::format("envoy.{}.access_loggers.extension_filters", Context::category()); 44 2 : } 45 2 : } 46 : 47 : const std::string category_; 48 : }; 49 : 50 : /** 51 : * Extension filter factory that reads from ExtensionFilter proto. 52 : */ 53 : using ExtensionFilterFactory = ExtensionFilterFactoryBase<Formatter::HttpFormatterContext>; 54 : 55 : /** 56 : * Implemented for each AccessLog::Instance and registered via Registry::registerFactory or the 57 : * convenience class RegisterFactory. 58 : */ 59 : template <class Context> class AccessLogInstanceFactoryBase : public Config::TypedFactory { 60 : public: 61 49 : AccessLogInstanceFactoryBase() : category_(categoryByType()) {} 62 : 63 0 : ~AccessLogInstanceFactoryBase() override = default; 64 : 65 : /** 66 : * Create a particular AccessLog::Instance implementation from a config proto. If the 67 : * implementation is unable to produce a factory with the provided parameters, it should throw an 68 : * EnvoyException. The returned pointer should never be nullptr. 69 : * @param config the custom configuration for this access log type. 70 : * @param filter filter to determine whether a particular request should be logged. If no filter 71 : * was specified in the configuration, argument will be nullptr. 72 : * @param context access log context through which persistent resources can be accessed. 73 : */ 74 : virtual AccessLog::InstanceBaseSharedPtr<Context> 75 : createAccessLogInstance(const Protobuf::Message& config, 76 : AccessLog::FilterBasePtr<Context>&& filter, 77 : Server::Configuration::FactoryContext& context) PURE; 78 : 79 64 : std::string category() const override { return category_; } 80 : 81 : private: 82 49 : std::string categoryByType() { 83 49 : if constexpr (std::is_same_v<Context, Formatter::HttpFormatterContext>) { 84 : // This is a special case for the HTTP formatter context to ensure backwards compatibility. 85 49 : return "envoy.access_loggers"; 86 49 : } else { 87 49 : return fmt::format("envoy.{}.access_loggers", Context::category()); 88 49 : } 89 49 : } 90 : 91 : const std::string category_; 92 : }; 93 : 94 : using AccessLogInstanceFactory = AccessLogInstanceFactoryBase<Formatter::HttpFormatterContext>; 95 : 96 : } // namespace AccessLog 97 : } // namespace Envoy