From c07536e8c745b069452b9c91372072c68339cd31 Mon Sep 17 00:00:00 2001 From: wbpcode Date: Fri, 3 Nov 2023 09:53:29 +0000 Subject: [PATCH] first commit to use formatter context Signed-off-by: wbpcode --- envoy/access_log/BUILD | 1 + envoy/access_log/access_log.h | 25 ++-- envoy/access_log/access_log_config.h | 37 ++++++ envoy/formatter/BUILD | 11 ++ envoy/formatter/http_specific_formatter.h | 115 ++++++++++++++++++ envoy/formatter/substitution_formatter.h | 102 +--------------- envoy/server/BUILD | 10 -- envoy/server/access_log_config.h | 54 -------- source/common/access_log/BUILD | 1 - source/common/access_log/access_log_impl.cc | 73 ++++------- source/common/access_log/access_log_impl.h | 91 ++++---------- .../access_loggers/common/access_log_base.cc | 21 +--- .../access_loggers/common/access_log_base.h | 20 +-- .../common/file_access_log_impl.cc | 12 +- .../common/file_access_log_impl.h | 7 +- .../common/stream_access_log_common_impl.h | 2 +- .../extensions/access_loggers/file/config.cc | 2 +- .../extensions/access_loggers/file/config.h | 4 +- .../access_loggers/filters/cel/cel.cc | 11 +- .../access_loggers/filters/cel/cel.h | 6 +- .../access_loggers/grpc/http_config.cc | 2 +- .../access_loggers/grpc/http_config.h | 4 +- .../grpc/http_grpc_access_log_impl.cc | 19 +-- .../grpc/http_grpc_access_log_impl.h | 7 +- .../access_loggers/grpc/tcp_config.cc | 2 +- .../access_loggers/grpc/tcp_config.h | 4 +- .../grpc/tcp_grpc_access_log_impl.cc | 12 +- .../grpc/tcp_grpc_access_log_impl.h | 7 +- .../open_telemetry/access_log_impl.h | 7 +- .../access_loggers/open_telemetry/config.cc | 5 +- .../access_loggers/open_telemetry/config.h | 4 +- .../open_telemetry/substitution_formatter.cc | 26 ++-- .../open_telemetry/substitution_formatter.h | 15 +-- .../access_loggers/stream/config.cc | 4 +- .../extensions/access_loggers/stream/config.h | 6 +- .../wasm/wasm_access_log_impl.h | 15 +-- source/extensions/common/wasm/context.cc | 12 +- source/extensions/common/wasm/context.h | 6 +- source/extensions/common/wasm/wasm.cc | 9 +- source/extensions/common/wasm/wasm.h | 6 +- 40 files changed, 312 insertions(+), 465 deletions(-) create mode 100644 envoy/formatter/http_specific_formatter.h delete mode 100644 envoy/server/access_log_config.h diff --git a/envoy/access_log/BUILD b/envoy/access_log/BUILD index 207c63e5a25e..7358373e999b 100644 --- a/envoy/access_log/BUILD +++ b/envoy/access_log/BUILD @@ -18,6 +18,7 @@ envoy_cc_library( "//envoy/stream_info:stream_info_interface", "//source/common/protobuf", "@envoy_api//envoy/data/accesslog/v3:pkg_cc_proto", + "//envoy/formatter:substitution_formatter_interface", ], ) diff --git a/envoy/access_log/access_log.h b/envoy/access_log/access_log.h index 6bc75b9c398c..02665ef4ecaa 100644 --- a/envoy/access_log/access_log.h +++ b/envoy/access_log/access_log.h @@ -8,12 +8,15 @@ #include "envoy/filesystem/filesystem.h" #include "envoy/http/header_map.h" #include "envoy/stream_info/stream_info.h" +#include "envoy/formatter/http_specific_formatter.h" #include "source/common/protobuf/protobuf.h" namespace Envoy { namespace AccessLog { +using HttpLogContext = Formatter::HttpFormatterContext; + class AccessLogFile { public: virtual ~AccessLogFile() = default; @@ -100,13 +103,13 @@ class Filter { /** * Evaluate whether an access log should be written based on request and response data. + * @param context supplies the context for the evaluation. + * @param stream_info supplies additional information about the request not + * contained in the context. * @return TRUE if the log should be written. */ - virtual bool evaluate(const StreamInfo::StreamInfo& info, - const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - AccessLogType access_log_type) const PURE; + virtual bool evaluate(const HttpLogContext& context, + const StreamInfo::StreamInfo& info) const PURE; }; using FilterPtr = std::unique_ptr; @@ -120,18 +123,12 @@ class Instance { /** * Log a completed request. - * @param request_headers supplies the incoming request headers after filtering. - * @param response_headers supplies response headers. - * @param response_trailers supplies response trailers. + * @param context supplies the context for the logging. * @param stream_info supplies additional information about the request not - * contained in the request headers. - * @param access_log_type supplies additional information about the type of the + * contained in the context. * log record, i.e the location in the code which recorded the log. */ - virtual void log(const Http::RequestHeaderMap* request_headers, - const Http::ResponseHeaderMap* response_headers, - const Http::ResponseTrailerMap* response_trailers, - const StreamInfo::StreamInfo& stream_info, AccessLogType access_log_type) PURE; + virtual void log(const HttpLogContext& context, const StreamInfo::StreamInfo& info) PURE; }; using InstanceSharedPtr = std::shared_ptr; diff --git a/envoy/access_log/access_log_config.h b/envoy/access_log/access_log_config.h index ee0810aa6e30..2e27bd85b472 100644 --- a/envoy/access_log/access_log_config.h +++ b/envoy/access_log/access_log_config.h @@ -84,5 +84,42 @@ template class AccessLogInstanceFactoryBase : public Config::Typ const std::string category_; }; +/** + * Implemented for each AccessLog::Instance and registered via Registry::registerFactory or the + * convenience class RegisterFactory. + */ +class AccessLogInstanceFactory : public Config::TypedFactory { +public: + ~AccessLogInstanceFactory() override = default; + + /** + * Create a particular AccessLog::Instance implementation from a config proto. If the + * implementation is unable to produce a factory with the provided parameters, it should throw an + * EnvoyException. The returned pointer should never be nullptr. + * @param config the custom configuration for this access log type. + * @param filter filter to determine whether a particular request should be logged. If no filter + * was specified in the configuration, argument will be nullptr. + * @param context access log context through which persistent resources can be accessed. + */ + virtual AccessLog::InstanceSharedPtr + createAccessLogInstance(const Protobuf::Message& config, AccessLog::FilterPtr&& filter, + Server::Configuration::ListenerAccessLogFactoryContext& context) PURE; + + /** + * Create a particular AccessLog::Instance implementation from a config proto. If the + * implementation is unable to produce a factory with the provided parameters, it should throw an + * EnvoyException. The returned pointer should never be nullptr. + * @param config the custom configuration for this access log type. + * @param filter filter to determine whether a particular request should be logged. If no filter + * was specified in the configuration, argument will be nullptr. + * @param context general filter context through which persistent resources can be accessed. + */ + virtual AccessLog::InstanceSharedPtr + createAccessLogInstance(const Protobuf::Message& config, AccessLog::FilterPtr&& filter, + Server::Configuration::CommonFactoryContext& context) PURE; + + std::string category() const override { return "envoy.access_loggers"; } +}; + } // namespace AccessLog } // namespace Envoy diff --git a/envoy/formatter/BUILD b/envoy/formatter/BUILD index 86bb31ad37b7..be13140c1afb 100644 --- a/envoy/formatter/BUILD +++ b/envoy/formatter/BUILD @@ -8,6 +8,17 @@ licenses(["notice"]) # Apache 2 envoy_package() +envoy_cc_library( + name = "http_specific_formatter_interface", + hdrs = [ + "http_specific_formatter.h", + ], + deps = [ + "@envoy_api//envoy/data/accesslog/v3:pkg_cc_proto", + "//envoy/http:header_map_interface", + ], +) + envoy_cc_library( name = "substitution_formatter_interface", hdrs = [ diff --git a/envoy/formatter/http_specific_formatter.h b/envoy/formatter/http_specific_formatter.h new file mode 100644 index 000000000000..f181a263ec3a --- /dev/null +++ b/envoy/formatter/http_specific_formatter.h @@ -0,0 +1,115 @@ + + +#pragma once + +#include "envoy/data/accesslog/v3/accesslog.pb.h" +#include "envoy/http/header_map.h" + +namespace Envoy { +namespace Formatter { + +using AccessLogType = envoy::data::accesslog::v3::AccessLogType; + +/** + * HTTP specific substitution formatter context for HTTP access logs or formatters. + */ +class HttpFormatterContext { +public: + /** + * Constructor that uses the provided request/response headers, response trailers, local reply + * body, and access log type. Any of the parameters can be nullptr/empty. + * + * @param request_headers supplies the request headers. + * @param response_headers supplies the response headers. + * @param response_trailers supplies the response trailers. + * @param local_reply_body supplies the local reply body. + * @param log_type supplies the access log type. + */ + HttpFormatterContext(const Http::RequestHeaderMap* request_headers = nullptr, + const Http::ResponseHeaderMap* response_headers = nullptr, + const Http::ResponseTrailerMap* response_trailers = nullptr, + absl::string_view local_reply_body = {}, + AccessLogType log_type = AccessLogType::NotSet); + /** + * Set or overwrite the request headers. + * @param request_headers supplies the request headers. + */ + HttpFormatterContext& setRequestHeaders(const Http::RequestHeaderMap& request_headers) { + request_headers_ = &request_headers; + return *this; + } + /** + * Set or overwrite the response headers. + * @param response_headers supplies the response headers. + */ + HttpFormatterContext& setResponseHeaders(const Http::ResponseHeaderMap& response_headers) { + response_headers_ = &response_headers; + return *this; + } + + /** + * Set or overwrite the response trailers. + * @param response_trailers supplies the response trailers. + */ + HttpFormatterContext& setResponseTrailers(const Http::ResponseTrailerMap& response_trailers) { + response_trailers_ = &response_trailers; + return *this; + } + + /** + * Set or overwrite the local reply body. + * @param local_reply_body supplies the local reply body. + */ + HttpFormatterContext& setLocalReplyBody(absl::string_view local_reply_body) { + local_reply_body_ = local_reply_body; + return *this; + } + + /** + * Set or overwrite the access log type. + * @param log_type supplies the access log type. + */ + HttpFormatterContext& setAccessLogType(AccessLogType log_type) { + log_type_ = log_type; + return *this; + } + + /** + * @return const Http::RequestHeaderMap& the request headers. Empty request header map if no + * request headers are available. + */ + const Http::RequestHeaderMap& requestHeaders() const; + + /** + * @return const Http::ResponseHeaderMap& the response headers. Empty respnose header map if + * no response headers are available. + */ + const Http::ResponseHeaderMap& responseHeaders() const; + + /** + * @return const Http::ResponseTrailerMap& the response trailers. Empty response trailer map + * if no response trailers are available. + */ + const Http::ResponseTrailerMap& responseTrailers() const; + + /** + * @return absl::string_view the local reply body. Empty if no local reply body. + */ + absl::string_view localReplyBody() const; + + /** + * @return AccessLog::AccessLogType the type of access log. NotSet if this is not used for + * access logging. + */ + AccessLogType accessLogType() const; + +private: + const Http::RequestHeaderMap* request_headers_{}; + const Http::ResponseHeaderMap* response_headers_{}; + const Http::ResponseTrailerMap* response_trailers_{}; + absl::string_view local_reply_body_{}; + AccessLogType log_type_{AccessLogType::NotSet}; +}; + +} // namespace Formatter +} // namespace Envoy diff --git a/envoy/formatter/substitution_formatter.h b/envoy/formatter/substitution_formatter.h index 01ffae286eee..87fbf29c6d7b 100644 --- a/envoy/formatter/substitution_formatter.h +++ b/envoy/formatter/substitution_formatter.h @@ -1,112 +1,12 @@ #pragma once +#include "envoy/formatter/http_specific_formatter.h" #include "envoy/formatter/substitution_formatter_base.h" #include "envoy/http/header_map.h" namespace Envoy { namespace Formatter { -/** - * HTTP specific substitution formatter context for HTTP access logs or formatters. - */ -class HttpFormatterContext { -public: - /** - * Constructor that uses the provided request/response headers, response trailers, local reply - * body, and access log type. Any of the parameters can be nullptr/empty. - * - * @param request_headers supplies the request headers. - * @param response_headers supplies the response headers. - * @param response_trailers supplies the response trailers. - * @param local_reply_body supplies the local reply body. - * @param log_type supplies the access log type. - */ - HttpFormatterContext(const Http::RequestHeaderMap* request_headers = nullptr, - const Http::ResponseHeaderMap* response_headers = nullptr, - const Http::ResponseTrailerMap* response_trailers = nullptr, - absl::string_view local_reply_body = {}, - AccessLog::AccessLogType log_type = AccessLog::AccessLogType::NotSet); - /** - * Set or overwrite the request headers. - * @param request_headers supplies the request headers. - */ - HttpFormatterContext& setRequestHeaders(const Http::RequestHeaderMap& request_headers) { - request_headers_ = &request_headers; - return *this; - } - /** - * Set or overwrite the response headers. - * @param response_headers supplies the response headers. - */ - HttpFormatterContext& setResponseHeaders(const Http::ResponseHeaderMap& response_headers) { - response_headers_ = &response_headers; - return *this; - } - - /** - * Set or overwrite the response trailers. - * @param response_trailers supplies the response trailers. - */ - HttpFormatterContext& setResponseTrailers(const Http::ResponseTrailerMap& response_trailers) { - response_trailers_ = &response_trailers; - return *this; - } - - /** - * Set or overwrite the local reply body. - * @param local_reply_body supplies the local reply body. - */ - HttpFormatterContext& setLocalReplyBody(absl::string_view local_reply_body) { - local_reply_body_ = local_reply_body; - return *this; - } - - /** - * Set or overwrite the access log type. - * @param log_type supplies the access log type. - */ - HttpFormatterContext& setAccessLogType(AccessLog::AccessLogType log_type) { - log_type_ = log_type; - return *this; - } - - /** - * @return const Http::RequestHeaderMap& the request headers. Empty request header map if no - * request headers are available. - */ - const Http::RequestHeaderMap& requestHeaders() const; - - /** - * @return const Http::ResponseHeaderMap& the response headers. Empty respnose header map if - * no response headers are available. - */ - const Http::ResponseHeaderMap& responseHeaders() const; - - /** - * @return const Http::ResponseTrailerMap& the response trailers. Empty response trailer map - * if no response trailers are available. - */ - const Http::ResponseTrailerMap& responseTrailers() const; - - /** - * @return absl::string_view the local reply body. Empty if no local reply body. - */ - absl::string_view localReplyBody() const; - - /** - * @return AccessLog::AccessLogType the type of access log. NotSet if this is not used for - * access logging. - */ - AccessLog::AccessLogType accessLogType() const; - -private: - const Http::RequestHeaderMap* request_headers_{}; - const Http::ResponseHeaderMap* response_headers_{}; - const Http::ResponseTrailerMap* response_trailers_{}; - absl::string_view local_reply_body_{}; - AccessLog::AccessLogType log_type_{AccessLog::AccessLogType::NotSet}; -}; - using Formatter = FormatterBase; using FormatterPtr = std::unique_ptr; using FormatterConstSharedPtr = std::shared_ptr; diff --git a/envoy/server/BUILD b/envoy/server/BUILD index cb5c06ec04ed..a412c4e164dd 100644 --- a/envoy/server/BUILD +++ b/envoy/server/BUILD @@ -8,16 +8,6 @@ licenses(["notice"]) # Apache 2 envoy_package() -envoy_cc_library( - name = "access_log_config_interface", - hdrs = ["access_log_config.h"], - deps = [ - ":filter_config_interface", - "//envoy/access_log:access_log_interface", - "//source/common/protobuf", - ], -) - envoy_cc_library( name = "admin_interface", hdrs = ["admin.h"], diff --git a/envoy/server/access_log_config.h b/envoy/server/access_log_config.h deleted file mode 100644 index 0d07554a7d8b..000000000000 --- a/envoy/server/access_log_config.h +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once - -#include - -#include "envoy/access_log/access_log.h" -#include "envoy/config/typed_config.h" -#include "envoy/server/filter_config.h" - -#include "source/common/protobuf/protobuf.h" - -namespace Envoy { -namespace Server { -namespace Configuration { - -/** - * Implemented for each AccessLog::Instance and registered via Registry::registerFactory or the - * convenience class RegisterFactory. - */ -class AccessLogInstanceFactory : public Config::TypedFactory { -public: - ~AccessLogInstanceFactory() override = default; - - /** - * Create a particular AccessLog::Instance implementation from a config proto. If the - * implementation is unable to produce a factory with the provided parameters, it should throw an - * EnvoyException. The returned pointer should never be nullptr. - * @param config the custom configuration for this access log type. - * @param filter filter to determine whether a particular request should be logged. If no filter - * was specified in the configuration, argument will be nullptr. - * @param context access log context through which persistent resources can be accessed. - */ - virtual AccessLog::InstanceSharedPtr - createAccessLogInstance(const Protobuf::Message& config, AccessLog::FilterPtr&& filter, - ListenerAccessLogFactoryContext& context) PURE; - - /** - * Create a particular AccessLog::Instance implementation from a config proto. If the - * implementation is unable to produce a factory with the provided parameters, it should throw an - * EnvoyException. The returned pointer should never be nullptr. - * @param config the custom configuration for this access log type. - * @param filter filter to determine whether a particular request should be logged. If no filter - * was specified in the configuration, argument will be nullptr. - * @param context general filter context through which persistent resources can be accessed. - */ - virtual AccessLog::InstanceSharedPtr createAccessLogInstance(const Protobuf::Message& config, - AccessLog::FilterPtr&& filter, - CommonFactoryContext& context) PURE; - - std::string category() const override { return "envoy.access_loggers"; } -}; - -} // namespace Configuration -} // namespace Server -} // namespace Envoy diff --git a/source/common/access_log/BUILD b/source/common/access_log/BUILD index 92959e853067..c7aa9a6d8675 100644 --- a/source/common/access_log/BUILD +++ b/source/common/access_log/BUILD @@ -22,7 +22,6 @@ envoy_cc_library( "//envoy/filesystem:filesystem_interface", "//envoy/http:header_map_interface", "//envoy/runtime:runtime_interface", - "//envoy/server:access_log_config_interface", "//envoy/upstream:upstream_interface", "//source/common/common:assert_lib", "//source/common/common:utility_lib", diff --git a/source/common/access_log/access_log_impl.cc b/source/common/access_log/access_log_impl.cc index 7df906595462..5ce7ffea6ea6 100644 --- a/source/common/access_log/access_log_impl.cc +++ b/source/common/access_log/access_log_impl.cc @@ -98,16 +98,13 @@ FilterPtr FilterFactory::fromProto(const envoy::config::accesslog::v3::AccessLog return nullptr; } -bool TraceableRequestFilter::evaluate(const StreamInfo::StreamInfo& info, - const Http::RequestHeaderMap&, const Http::ResponseHeaderMap&, - const Http::ResponseTrailerMap&, AccessLogType) const { +bool TraceableRequestFilter::evaluate(const HttpLogContext&, + const StreamInfo::StreamInfo& info) const { const Tracing::Decision decision = Tracing::TracerUtility::shouldTraceRequest(info); return decision.traced && decision.reason == Tracing::Reason::ServiceForced; } -bool StatusCodeFilter::evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap&, - const Http::ResponseHeaderMap&, const Http::ResponseTrailerMap&, - AccessLogType) const { +bool StatusCodeFilter::evaluate(const HttpLogContext&, const StreamInfo::StreamInfo& info) const { if (!info.responseCode()) { return compareAgainstValue(0ULL); } @@ -115,9 +112,7 @@ bool StatusCodeFilter::evaluate(const StreamInfo::StreamInfo& info, const Http:: return compareAgainstValue(info.responseCode().value()); } -bool DurationFilter::evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap&, - const Http::ResponseHeaderMap&, const Http::ResponseTrailerMap&, - AccessLogType) const { +bool DurationFilter::evaluate(const HttpLogContext&, const StreamInfo::StreamInfo& info) const { absl::optional duration = info.currentDuration(); if (!duration.has_value()) { return false; @@ -133,9 +128,8 @@ RuntimeFilter::RuntimeFilter(const envoy::config::accesslog::v3::RuntimeFilter& percent_(config.percent_sampled()), use_independent_randomness_(config.use_independent_randomness()) {} -bool RuntimeFilter::evaluate(const StreamInfo::StreamInfo& stream_info, - const Http::RequestHeaderMap&, const Http::ResponseHeaderMap&, - const Http::ResponseTrailerMap&, AccessLogType) const { +bool RuntimeFilter::evaluate(const HttpLogContext&, + const StreamInfo::StreamInfo& stream_info) const { // This code is verbose to avoid preallocating a random number that is not needed. uint64_t random_value; if (use_independent_randomness_) { @@ -177,15 +171,10 @@ AndFilter::AndFilter(const envoy::config::accesslog::v3::AndFilter& config, Server::Configuration::CommonFactoryContext& context) : OperatorFilter(config.filters(), context) {} -bool OrFilter::evaluate(const StreamInfo::StreamInfo& info, - const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - AccessLogType access_log_type) const { +bool OrFilter::evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const { bool result = false; for (auto& filter : filters_) { - result |= filter->evaluate(info, request_headers, response_headers, response_trailers, - access_log_type); + result |= filter->evaluate(context, info); if (result) { break; @@ -195,15 +184,10 @@ bool OrFilter::evaluate(const StreamInfo::StreamInfo& info, return result; } -bool AndFilter::evaluate(const StreamInfo::StreamInfo& info, - const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - AccessLogType access_log_type) const { +bool AndFilter::evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const { bool result = true; for (auto& filter : filters_) { - result &= filter->evaluate(info, request_headers, response_headers, response_trailers, - access_log_type); + result &= filter->evaluate(context, info); if (!result) { break; @@ -213,20 +197,16 @@ bool AndFilter::evaluate(const StreamInfo::StreamInfo& info, return result; } -bool NotHealthCheckFilter::evaluate(const StreamInfo::StreamInfo& info, - const Http::RequestHeaderMap&, const Http::ResponseHeaderMap&, - const Http::ResponseTrailerMap&, AccessLogType) const { +bool NotHealthCheckFilter::evaluate(const HttpLogContext&, + const StreamInfo::StreamInfo& info) const { return !info.healthCheck(); } HeaderFilter::HeaderFilter(const envoy::config::accesslog::v3::HeaderFilter& config) : header_data_(std::make_unique(config.header())) {} -bool HeaderFilter::evaluate(const StreamInfo::StreamInfo&, - const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap&, const Http::ResponseTrailerMap&, - AccessLogType) const { - return Http::HeaderUtility::matchHeaders(request_headers, *header_data_); +bool HeaderFilter::evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo&) const { + return Http::HeaderUtility::matchHeaders(context.requestHeaders(), *header_data_); } ResponseFlagFilter::ResponseFlagFilter( @@ -240,9 +220,7 @@ ResponseFlagFilter::ResponseFlagFilter( } } -bool ResponseFlagFilter::evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap&, - const Http::ResponseHeaderMap&, const Http::ResponseTrailerMap&, - AccessLogType) const { +bool ResponseFlagFilter::evaluate(const HttpLogContext&, const StreamInfo::StreamInfo& info) const { if (configured_flags_ != 0) { return info.intersectResponseFlags(configured_flags_); } @@ -257,14 +235,12 @@ GrpcStatusFilter::GrpcStatusFilter(const envoy::config::accesslog::v3::GrpcStatu exclude_ = config.exclude(); } -bool GrpcStatusFilter::evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap&, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - AccessLogType) const { +bool GrpcStatusFilter::evaluate(const HttpLogContext& context, + const StreamInfo::StreamInfo& info) const { Grpc::Status::GrpcStatus status = Grpc::Status::WellKnownGrpcStatus::Unknown; const auto& optional_status = - Grpc::Common::getGrpcStatus(response_trailers, response_headers, info); + Grpc::Common::getGrpcStatus(context.responseTrailers(), context.responseHeaders(), info); if (optional_status.has_value()) { status = optional_status.value(); } @@ -286,10 +262,8 @@ LogTypeFilter::LogTypeFilter(const envoy::config::accesslog::v3::LogTypeFilter& exclude_ = config.exclude(); } -bool LogTypeFilter::evaluate(const StreamInfo::StreamInfo&, const Http::RequestHeaderMap&, - const Http::ResponseHeaderMap&, const Http::ResponseTrailerMap&, - AccessLogType access_log_type) const { - const bool found = types_.contains(access_log_type); +bool LogTypeFilter::evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo&) const { + const bool found = types_.contains(context.accessLogType()); return exclude_ ? !found : found; } @@ -315,9 +289,7 @@ MetadataFilter::MetadataFilter(const envoy::config::accesslog::v3::MetadataFilte present_matcher_ = Matchers::ValueMatcher::create(present_val); } -bool MetadataFilter::evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap&, - const Http::ResponseHeaderMap&, const Http::ResponseTrailerMap&, - AccessLogType) const { +bool MetadataFilter::evaluate(const HttpLogContext&, const StreamInfo::StreamInfo& info) const { const auto& value = Envoy::Config::Metadata::metadataValue(&info.dynamicMetadata(), filter_, path_); // If the key corresponds to a set value in dynamic metadata, return true if the value matches the @@ -341,8 +313,7 @@ InstanceSharedPtr makeAccessLogInstance(const envoy::config::accesslog::v3::Acce filter = FilterFactory::fromProto(config.filter(), context); } - auto& factory = - Config::Utility::getAndCheckFactory(config); + auto& factory = Config::Utility::getAndCheckFactory(config); ProtobufTypes::MessagePtr message = Config::Utility::translateToFactoryConfig( config, context.messageValidationVisitor(), factory); diff --git a/source/common/access_log/access_log_impl.h b/source/common/access_log/access_log_impl.h index a60a09bbcb56..b2fb6d1c5a98 100644 --- a/source/common/access_log/access_log_impl.h +++ b/source/common/access_log/access_log_impl.h @@ -10,7 +10,6 @@ #include "envoy/config/accesslog/v3/accesslog.pb.h" #include "envoy/config/typed_config.h" #include "envoy/runtime/runtime.h" -#include "envoy/server/access_log_config.h" #include "envoy/type/v3/percent.pb.h" #include "source/common/common/matchers.h" @@ -19,6 +18,7 @@ #include "source/common/grpc/status.h" #include "source/common/http/header_utility.h" #include "source/common/protobuf/protobuf.h" +#include "source/common/formatter/http_specific_formatter.h" #include "absl/container/node_hash_set.h" #include "absl/hash/hash.h" @@ -61,10 +61,8 @@ class StatusCodeFilter : public ComparisonFilter { Runtime::Loader& runtime) : ComparisonFilter(config.comparison(), runtime) {} - // AccessLog::Filter - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + // AccessLog::HttpFilter + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; }; /** @@ -76,10 +74,8 @@ class DurationFilter : public ComparisonFilter { Runtime::Loader& runtime) : ComparisonFilter(config.comparison(), runtime) {} - // AccessLog::Filter - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + // AccessLog::HttpFilter + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; }; /** @@ -103,10 +99,8 @@ class AndFilter : public OperatorFilter { AndFilter(const envoy::config::accesslog::v3::AndFilter& config, Server::Configuration::CommonFactoryContext& context); - // AccessLog::Filter - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + // AccessLog::HttpFilter + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; }; /** @@ -117,10 +111,8 @@ class OrFilter : public OperatorFilter { OrFilter(const envoy::config::accesslog::v3::OrFilter& config, Server::Configuration::CommonFactoryContext& context); - // AccessLog::Filter - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + // AccessLog::HttpFilter + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; }; /** @@ -130,10 +122,8 @@ class NotHealthCheckFilter : public Filter { public: NotHealthCheckFilter() = default; - // AccessLog::Filter - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + // AccessLog::HttpFilter + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; }; /** @@ -141,10 +131,8 @@ class NotHealthCheckFilter : public Filter { */ class TraceableRequestFilter : public Filter { public: - // AccessLog::Filter - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + // AccessLog::HttpFilter + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; }; /** @@ -155,10 +143,8 @@ class RuntimeFilter : public Filter { RuntimeFilter(const envoy::config::accesslog::v3::RuntimeFilter& config, Runtime::Loader& runtime, Random::RandomGenerator& random); - // AccessLog::Filter - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + // AccessLog::HttpFilter + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; private: Runtime::Loader& runtime_; @@ -175,10 +161,8 @@ class HeaderFilter : public Filter { public: HeaderFilter(const envoy::config::accesslog::v3::HeaderFilter& config); - // AccessLog::Filter - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + // AccessLog::HttpFilter + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; private: const Http::HeaderUtility::HeaderDataPtr header_data_; @@ -191,10 +175,8 @@ class ResponseFlagFilter : public Filter { public: ResponseFlagFilter(const envoy::config::accesslog::v3::ResponseFlagFilter& config); - // AccessLog::Filter - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + // AccessLog::HttpFilter + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; private: uint64_t configured_flags_{}; @@ -212,10 +194,8 @@ class GrpcStatusFilter : public Filter { GrpcStatusFilter(const envoy::config::accesslog::v3::GrpcStatusFilter& config); - // AccessLog::Filter - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + // AccessLog::HttpFilter + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; private: GrpcStatusHashSet statuses_; @@ -238,9 +218,7 @@ class LogTypeFilter : public Filter { LogTypeFilter(const envoy::config::accesslog::v3::LogTypeFilter& filter_config); - bool evaluate(const StreamInfo::StreamInfo&, const Http::RequestHeaderMap&, - const Http::ResponseHeaderMap&, const Http::ResponseTrailerMap&, - AccessLogType access_log_type) const override; + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; private: LogTypeHashSet types_; @@ -254,9 +232,7 @@ class MetadataFilter : public Filter { public: MetadataFilter(const envoy::config::accesslog::v3::MetadataFilter& filter_config); - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, AccessLogType) const override; + bool evaluate(const HttpLogContext& context, const StreamInfo::StreamInfo& info) const override; private: Matchers::ValueMatcherConstSharedPtr present_matcher_; @@ -268,27 +244,6 @@ class MetadataFilter : public Filter { const std::string filter_; }; -/** - * Extension filter factory that reads from ExtensionFilter proto. - */ -class ExtensionFilterFactory : public Config::TypedFactory { -public: - ~ExtensionFilterFactory() override = default; - - /** - * Create a particular extension filter implementation from a config proto. If the - * implementation is unable to produce a filter with the provided parameters, it should throw an - * EnvoyException. The returned pointer should never be nullptr. - * @param config supplies the custom configuration for this filter type. - * @param context supplies the server factory context. - * @return an instance of extension filter implementation from a config proto. - */ - virtual FilterPtr createFilter(const envoy::config::accesslog::v3::ExtensionFilter& config, - Server::Configuration::CommonFactoryContext& context) PURE; - - std::string category() const override { return "envoy.access_loggers.extension_filters"; } -}; - /** * Access log factory that reads the configuration from proto. */ diff --git a/source/extensions/access_loggers/common/access_log_base.cc b/source/extensions/access_loggers/common/access_log_base.cc index f90781a86125..5d4db6a45243 100644 --- a/source/extensions/access_loggers/common/access_log_base.cc +++ b/source/extensions/access_loggers/common/access_log_base.cc @@ -8,26 +8,11 @@ namespace Extensions { namespace AccessLoggers { namespace Common { -void ImplBase::log(const Http::RequestHeaderMap* request_headers, - const Http::ResponseHeaderMap* response_headers, - const Http::ResponseTrailerMap* response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) { - if (!request_headers) { - request_headers = Http::StaticEmptyHeaders::get().request_headers.get(); - } - if (!response_headers) { - response_headers = Http::StaticEmptyHeaders::get().response_headers.get(); - } - if (!response_trailers) { - response_trailers = Http::StaticEmptyHeaders::get().response_trailers.get(); - } - if (filter_ && !filter_->evaluate(stream_info, *request_headers, *response_headers, - *response_trailers, access_log_type)) { +void ImplBase::log(const AccessLog::HttpLogContext& context, const StreamInfo::StreamInfo& info) { + if (filter_ && !filter_->evaluate(context, info)) { return; } - return emitLog(*request_headers, *response_headers, *response_trailers, stream_info, - access_log_type); + return emitLog(context, info); } } // namespace Common diff --git a/source/extensions/access_loggers/common/access_log_base.h b/source/extensions/access_loggers/common/access_log_base.h index c15b6003c310..bca3f90d7886 100644 --- a/source/extensions/access_loggers/common/access_log_base.h +++ b/source/extensions/access_loggers/common/access_log_base.h @@ -6,10 +6,10 @@ #include "envoy/access_log/access_log.h" #include "envoy/runtime/runtime.h" -#include "envoy/server/access_log_config.h" #include "source/common/http/header_utility.h" #include "source/common/protobuf/protobuf.h" +#include "source/common/access_log/access_log_impl.h" namespace Envoy { namespace Extensions { @@ -26,26 +26,18 @@ class ImplBase : public AccessLog::Instance { /** * Log a completed request if the underlying AccessLog `filter_` allows it. */ - void log(const Http::RequestHeaderMap* request_headers, - const Http::ResponseHeaderMap* response_headers, - const Http::ResponseTrailerMap* response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) override; + void log(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& stream_info) override; private: /** * Log a completed request. - * @param request_headers supplies the incoming request headers after filtering. - * @param response_headers supplies response headers. - * @param response_trailers supplies response trailers. + * @param context supplies the necessary context to log. * @param stream_info supplies additional information about the request not * contained in the request headers. */ - virtual void emitLog(const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) PURE; + virtual void emitLog(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& stream_info) PURE; AccessLog::FilterPtr filter_; }; diff --git a/source/extensions/access_loggers/common/file_access_log_impl.cc b/source/extensions/access_loggers/common/file_access_log_impl.cc index c06fff4fa8ee..637d09149325 100644 --- a/source/extensions/access_loggers/common/file_access_log_impl.cc +++ b/source/extensions/access_loggers/common/file_access_log_impl.cc @@ -12,15 +12,9 @@ FileAccessLog::FileAccessLog(const Filesystem::FilePathAndType& access_log_file_ log_file_ = log_manager.createAccessLog(access_log_file_info); } -void FileAccessLog::emitLog(const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) { - log_file_->write( - formatter_->formatWithContext({&request_headers, &response_headers, &response_trailers, - absl::string_view(), access_log_type}, - stream_info)); +void FileAccessLog::emitLog(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& stream_info) { + log_file_->write(formatter_->formatWithContext(context, stream_info)); } } // namespace File diff --git a/source/extensions/access_loggers/common/file_access_log_impl.h b/source/extensions/access_loggers/common/file_access_log_impl.h index 4b866879fbbb..edbc7e1b5a2b 100644 --- a/source/extensions/access_loggers/common/file_access_log_impl.h +++ b/source/extensions/access_loggers/common/file_access_log_impl.h @@ -19,11 +19,8 @@ class FileAccessLog : public Common::ImplBase { private: // Common::ImplBase - void emitLog(const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) override; + void emitLog(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& stream_info) override; AccessLog::AccessLogFileSharedPtr log_file_; Formatter::FormatterPtr formatter_; diff --git a/source/extensions/access_loggers/common/stream_access_log_common_impl.h b/source/extensions/access_loggers/common/stream_access_log_common_impl.h index 635a3ce6604e..f717e71769f3 100644 --- a/source/extensions/access_loggers/common/stream_access_log_common_impl.h +++ b/source/extensions/access_loggers/common/stream_access_log_common_impl.h @@ -1,6 +1,6 @@ #pragma once -#include "envoy/server/access_log_config.h" +#include "envoy/access_log/access_log_config.h" #include "source/common/formatter/substitution_format_string.h" #include "source/common/formatter/substitution_formatter.h" diff --git a/source/extensions/access_loggers/file/config.cc b/source/extensions/access_loggers/file/config.cc index 008b715489ea..0f98633223f4 100644 --- a/source/extensions/access_loggers/file/config.cc +++ b/source/extensions/access_loggers/file/config.cc @@ -81,7 +81,7 @@ std::string FileAccessLogFactory::name() const { return "envoy.access_loggers.fi /** * Static registration for the file access log. @see RegisterFactory. */ -LEGACY_REGISTER_FACTORY(FileAccessLogFactory, Server::Configuration::AccessLogInstanceFactory, +LEGACY_REGISTER_FACTORY(FileAccessLogFactory, AccessLog::AccessLogInstanceFactory, "envoy.file_access_log"); } // namespace File diff --git a/source/extensions/access_loggers/file/config.h b/source/extensions/access_loggers/file/config.h index 8a18d2903f31..6d0c40be4c15 100644 --- a/source/extensions/access_loggers/file/config.h +++ b/source/extensions/access_loggers/file/config.h @@ -1,6 +1,6 @@ #pragma once -#include "envoy/server/access_log_config.h" +#include "envoy/access_log/access_log_config.h" namespace Envoy { namespace Extensions { @@ -10,7 +10,7 @@ namespace File { /** * Config registration for the file access log. @see AccessLogInstanceFactory. */ -class FileAccessLogFactory : public Server::Configuration::AccessLogInstanceFactory { +class FileAccessLogFactory : public AccessLog::AccessLogInstanceFactory { public: AccessLog::InstanceSharedPtr createAccessLogInstance(const Protobuf::Message& config, AccessLog::FilterPtr&& filter, diff --git a/source/extensions/access_loggers/filters/cel/cel.cc b/source/extensions/access_loggers/filters/cel/cel.cc index 25684a54afdd..d15478f34c03 100644 --- a/source/extensions/access_loggers/filters/cel/cel.cc +++ b/source/extensions/access_loggers/filters/cel/cel.cc @@ -14,14 +14,11 @@ CELAccessLogExtensionFilter::CELAccessLogExtensionFilter( compiled_expr_ = Expr::createExpression(builder_->builder(), parsed_expr_); } -bool CELAccessLogExtensionFilter::evaluate(const StreamInfo::StreamInfo& stream_info, - const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - AccessLog::AccessLogType) const { +bool CELAccessLogExtensionFilter::evaluate(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& info) const { Protobuf::Arena arena; - auto eval_status = Expr::evaluate(*compiled_expr_, arena, stream_info, &request_headers, - &response_headers, &response_trailers); + auto eval_status = Expr::evaluate(*compiled_expr_, arena, info, &context.requestHeaders(), + &context.responseHeaders(), &context.responseTrailers()); if (!eval_status.has_value() || eval_status.value().IsError()) { return false; } diff --git a/source/extensions/access_loggers/filters/cel/cel.h b/source/extensions/access_loggers/filters/cel/cel.h index 2cce2d68b44e..b6745762cc1e 100644 --- a/source/extensions/access_loggers/filters/cel/cel.h +++ b/source/extensions/access_loggers/filters/cel/cel.h @@ -22,10 +22,8 @@ class CELAccessLogExtensionFilter : public AccessLog::Filter { CELAccessLogExtensionFilter(Extensions::Filters::Common::Expr::BuilderInstanceSharedPtr, const google::api::expr::v1alpha1::Expr&); - bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - AccessLog::AccessLogType access_log_type) const override; + bool evaluate(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& info) const override; private: Extensions::Filters::Common::Expr::BuilderInstanceSharedPtr builder_; diff --git a/source/extensions/access_loggers/grpc/http_config.cc b/source/extensions/access_loggers/grpc/http_config.cc index 7b2e1fbedab9..ce7d055b6aac 100644 --- a/source/extensions/access_loggers/grpc/http_config.cc +++ b/source/extensions/access_loggers/grpc/http_config.cc @@ -49,7 +49,7 @@ std::string HttpGrpcAccessLogFactory::name() const { return "envoy.access_logger /** * Static registration for the HTTP gRPC access log. @see RegisterFactory. */ -LEGACY_REGISTER_FACTORY(HttpGrpcAccessLogFactory, Server::Configuration::AccessLogInstanceFactory, +LEGACY_REGISTER_FACTORY(HttpGrpcAccessLogFactory, AccessLog::AccessLogInstanceFactory, "envoy.http_grpc_access_log"); } // namespace HttpGrpc diff --git a/source/extensions/access_loggers/grpc/http_config.h b/source/extensions/access_loggers/grpc/http_config.h index 066f0d495bb2..83555e5852ef 100644 --- a/source/extensions/access_loggers/grpc/http_config.h +++ b/source/extensions/access_loggers/grpc/http_config.h @@ -2,7 +2,7 @@ #include -#include "envoy/server/access_log_config.h" +#include "envoy/access_log/access_log_config.h" namespace Envoy { namespace Extensions { @@ -12,7 +12,7 @@ namespace HttpGrpc { /** * Config registration for the HTTP gRPC access log. @see AccessLogInstanceFactory. */ -class HttpGrpcAccessLogFactory : public Server::Configuration::AccessLogInstanceFactory { +class HttpGrpcAccessLogFactory : public AccessLog::AccessLogInstanceFactory { public: AccessLog::InstanceSharedPtr createAccessLogInstance(const Protobuf::Message& config, AccessLog::FilterPtr&& filter, diff --git a/source/extensions/access_loggers/grpc/http_grpc_access_log_impl.cc b/source/extensions/access_loggers/grpc/http_grpc_access_log_impl.cc index b89f2cab0134..41fded2f12f6 100644 --- a/source/extensions/access_loggers/grpc/http_grpc_access_log_impl.cc +++ b/source/extensions/access_loggers/grpc/http_grpc_access_log_impl.cc @@ -50,17 +50,17 @@ HttpGrpcAccessLog::HttpGrpcAccessLog(AccessLog::FilterPtr&& filter, }); } -void HttpGrpcAccessLog::emitLog(const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) { +void HttpGrpcAccessLog::emitLog(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& stream_info) { // Common log properties. // TODO(mattklein123): Populate sample_rate field. envoy::data::accesslog::v3::HTTPAccessLogEntry log_entry; - GrpcCommon::Utility::extractCommonAccessLogProperties(*log_entry.mutable_common_properties(), - request_headers, stream_info, - config_->common_config(), access_log_type); + + const auto& request_headers = context.requestHeaders(); + + GrpcCommon::Utility::extractCommonAccessLogProperties( + *log_entry.mutable_common_properties(), request_headers, stream_info, + config_->common_config(), context.accessLogType()); if (stream_info.protocol()) { switch (stream_info.protocol().value()) { @@ -135,6 +135,9 @@ void HttpGrpcAccessLog::emitLog(const Http::RequestHeaderMap& request_headers, } // HTTP response properties. + const auto& response_headers = context.responseHeaders(); + const auto& response_trailers = context.responseTrailers(); + auto* response_properties = log_entry.mutable_response(); if (stream_info.responseCode()) { response_properties->mutable_response_code()->set_value(stream_info.responseCode().value()); diff --git a/source/extensions/access_loggers/grpc/http_grpc_access_log_impl.h b/source/extensions/access_loggers/grpc/http_grpc_access_log_impl.h index f11d22cbd61c..f418d85944c6 100644 --- a/source/extensions/access_loggers/grpc/http_grpc_access_log_impl.h +++ b/source/extensions/access_loggers/grpc/http_grpc_access_log_impl.h @@ -44,11 +44,8 @@ class HttpGrpcAccessLog : public Common::ImplBase { }; // Common::ImplBase - void emitLog(const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) override; + void emitLog(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& info) override; const HttpGrpcAccessLogConfigConstSharedPtr config_; const ThreadLocal::SlotPtr tls_slot_; diff --git a/source/extensions/access_loggers/grpc/tcp_config.cc b/source/extensions/access_loggers/grpc/tcp_config.cc index 9f1fdb55a737..44089eee34c4 100644 --- a/source/extensions/access_loggers/grpc/tcp_config.cc +++ b/source/extensions/access_loggers/grpc/tcp_config.cc @@ -48,7 +48,7 @@ std::string TcpGrpcAccessLogFactory::name() const { return "envoy.access_loggers /** * Static registration for the TCP gRPC access log. @see RegisterFactory. */ -LEGACY_REGISTER_FACTORY(TcpGrpcAccessLogFactory, Server::Configuration::AccessLogInstanceFactory, +LEGACY_REGISTER_FACTORY(TcpGrpcAccessLogFactory, AccessLog::AccessLogInstanceFactory, "envoy.tcp_grpc_access_log"); } // namespace TcpGrpc diff --git a/source/extensions/access_loggers/grpc/tcp_config.h b/source/extensions/access_loggers/grpc/tcp_config.h index 1e3481e5fea2..a9be520affbb 100644 --- a/source/extensions/access_loggers/grpc/tcp_config.h +++ b/source/extensions/access_loggers/grpc/tcp_config.h @@ -2,7 +2,7 @@ #include -#include "envoy/server/access_log_config.h" +#include "envoy/access_log/access_log_config.h" namespace Envoy { namespace Extensions { @@ -12,7 +12,7 @@ namespace TcpGrpc { /** * Config registration for the TCP gRPC access log. @see AccessLogInstanceFactory. */ -class TcpGrpcAccessLogFactory : public Server::Configuration::AccessLogInstanceFactory { +class TcpGrpcAccessLogFactory : public AccessLog::AccessLogInstanceFactory { public: AccessLog::InstanceSharedPtr createAccessLogInstance(const Protobuf::Message& config, AccessLog::FilterPtr&& filter, diff --git a/source/extensions/access_loggers/grpc/tcp_grpc_access_log_impl.cc b/source/extensions/access_loggers/grpc/tcp_grpc_access_log_impl.cc index 10f9caa14a5d..3f96a389ac01 100644 --- a/source/extensions/access_loggers/grpc/tcp_grpc_access_log_impl.cc +++ b/source/extensions/access_loggers/grpc/tcp_grpc_access_log_impl.cc @@ -32,15 +32,13 @@ TcpGrpcAccessLog::TcpGrpcAccessLog(AccessLog::FilterPtr&& filter, }); } -void TcpGrpcAccessLog::emitLog(const Http::RequestHeaderMap& request_header, - const Http::ResponseHeaderMap&, const Http::ResponseTrailerMap&, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) { +void TcpGrpcAccessLog::emitLog(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& stream_info) { // Common log properties. envoy::data::accesslog::v3::TCPAccessLogEntry log_entry; - GrpcCommon::Utility::extractCommonAccessLogProperties(*log_entry.mutable_common_properties(), - request_header, stream_info, - config_->common_config(), access_log_type); + GrpcCommon::Utility::extractCommonAccessLogProperties( + *log_entry.mutable_common_properties(), context.requestHeaders(), stream_info, + config_->common_config(), context.accessLogType()); envoy::data::accesslog::v3::ConnectionProperties& connection_properties = *log_entry.mutable_connection_properties(); diff --git a/source/extensions/access_loggers/grpc/tcp_grpc_access_log_impl.h b/source/extensions/access_loggers/grpc/tcp_grpc_access_log_impl.h index 71b5dfd81f9b..0e482fc8c155 100644 --- a/source/extensions/access_loggers/grpc/tcp_grpc_access_log_impl.h +++ b/source/extensions/access_loggers/grpc/tcp_grpc_access_log_impl.h @@ -43,11 +43,8 @@ class TcpGrpcAccessLog : public Common::ImplBase { }; // Common::ImplBase - void emitLog(const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) override; + void emitLog(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& info) override; const TcpGrpcAccessLogConfigConstSharedPtr config_; const ThreadLocal::SlotPtr tls_slot_; diff --git a/source/extensions/access_loggers/open_telemetry/access_log_impl.h b/source/extensions/access_loggers/open_telemetry/access_log_impl.h index be5456d08c8d..f848ef4827d1 100644 --- a/source/extensions/access_loggers/open_telemetry/access_log_impl.h +++ b/source/extensions/access_loggers/open_telemetry/access_log_impl.h @@ -49,11 +49,8 @@ class AccessLog : public Common::ImplBase { }; // Common::ImplBase - void emitLog(const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - const StreamInfo::StreamInfo& stream_info, - Envoy::AccessLog::AccessLogType access_log_type) override; + void emitLog(const Envoy::AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& info) override; const ThreadLocal::SlotPtr tls_slot_; const GrpcAccessLoggerCacheSharedPtr access_logger_cache_; diff --git a/source/extensions/access_loggers/open_telemetry/config.cc b/source/extensions/access_loggers/open_telemetry/config.cc index fd3477689fc2..d4fc6fe76f9f 100644 --- a/source/extensions/access_loggers/open_telemetry/config.cc +++ b/source/extensions/access_loggers/open_telemetry/config.cc @@ -3,7 +3,6 @@ #include "envoy/extensions/access_loggers/open_telemetry/v3/logs_service.pb.h" #include "envoy/extensions/access_loggers/open_telemetry/v3/logs_service.pb.validate.h" #include "envoy/registry/registry.h" -#include "envoy/server/access_log_config.h" #include "envoy/server/filter_config.h" #include "source/common/common/assert.h" @@ -63,8 +62,8 @@ std::string AccessLogFactory::name() const { return "envoy.access_loggers.open_t /** * Static registration for the OpenTelemetry (gRPC) access log. @see RegisterFactory. */ -REGISTER_FACTORY(AccessLogFactory, Server::Configuration::AccessLogInstanceFactory){ - "envoy.open_telemetry_access_log"}; +REGISTER_FACTORY(AccessLogFactory, + AccessLog::AccessLogInstanceFactory){"envoy.open_telemetry_access_log"}; } // namespace OpenTelemetry } // namespace AccessLoggers diff --git a/source/extensions/access_loggers/open_telemetry/config.h b/source/extensions/access_loggers/open_telemetry/config.h index f71c541316d2..3d2f634a5070 100644 --- a/source/extensions/access_loggers/open_telemetry/config.h +++ b/source/extensions/access_loggers/open_telemetry/config.h @@ -2,7 +2,7 @@ #include -#include "envoy/server/access_log_config.h" +#include "envoy/access_log/access_log_config.h" namespace Envoy { namespace Extensions { @@ -12,7 +12,7 @@ namespace OpenTelemetry { /** * Config registration for the OpenTelemetry (gRPC) access log. @see AccessLogInstanceFactory. */ -class AccessLogFactory : public Server::Configuration::AccessLogInstanceFactory { +class AccessLogFactory : public AccessLog::AccessLogInstanceFactory { public: ::Envoy::AccessLog::InstanceSharedPtr createAccessLogInstance(const Protobuf::Message& config, ::Envoy::AccessLog::FilterPtr&& filter, diff --git a/source/extensions/access_loggers/open_telemetry/substitution_formatter.cc b/source/extensions/access_loggers/open_telemetry/substitution_formatter.cc index 2d3c76544469..f7e6843b691a 100644 --- a/source/extensions/access_loggers/open_telemetry/substitution_formatter.cc +++ b/source/extensions/access_loggers/open_telemetry/substitution_formatter.cc @@ -82,21 +82,17 @@ OpenTelemetryFormatter::FormatBuilder::toFormatStringValue(const std::string& st ::opentelemetry::proto::common::v1::AnyValue OpenTelemetryFormatter::providersCallback( const std::vector& providers, - const Http::RequestHeaderMap& request_headers, const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, const StreamInfo::StreamInfo& stream_info, - absl::string_view local_reply_body, AccessLog::AccessLogType access_log_type) const { + const Formatter::HttpFormatterContext& context, const StreamInfo::StreamInfo& info) const { ASSERT(!providers.empty()); ::opentelemetry::proto::common::v1::AnyValue output; std::vector bits(providers.size()); - const Formatter::HttpFormatterContext formatter_context{ - &request_headers, &response_headers, &response_trailers, local_reply_body, access_log_type}; + std::transform( + providers.begin(), providers.end(), bits.begin(), + [&](const Formatter::FormatterProviderPtr& provider) { + return provider->formatWithContext(context, info).value_or(DefaultUnspecifiedValueString); + }); - std::transform(providers.begin(), providers.end(), bits.begin(), - [&](const Formatter::FormatterProviderPtr& provider) { - return provider->formatWithContext(formatter_context, stream_info) - .value_or(DefaultUnspecifiedValueString); - }); output.set_string_value(absl::StrJoin(bits, "")); return output; } @@ -128,14 +124,12 @@ OpenTelemetryFormatter::openTelemetryFormatListCallback( return output; } -::opentelemetry::proto::common::v1::KeyValueList OpenTelemetryFormatter::format( - const Http::RequestHeaderMap& request_headers, const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, const StreamInfo::StreamInfo& stream_info, - absl::string_view local_reply_body, AccessLog::AccessLogType access_log_type) const { +::opentelemetry::proto::common::v1::KeyValueList +OpenTelemetryFormatter::format(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& info) const { OpenTelemetryFormatMapVisitor visitor{ [&](const std::vector& providers) { - return providersCallback(providers, request_headers, response_headers, response_trailers, - stream_info, local_reply_body, access_log_type); + return providersCallback(providers, context, info); }, [&, this](const OpenTelemetryFormatter::OpenTelemetryFormatMapWrapper& format_map) { return openTelemetryFormatMapCallback(format_map, visitor); diff --git a/source/extensions/access_loggers/open_telemetry/substitution_formatter.h b/source/extensions/access_loggers/open_telemetry/substitution_formatter.h index 3ae2188f8461..cb4dcb92b124 100644 --- a/source/extensions/access_loggers/open_telemetry/substitution_formatter.h +++ b/source/extensions/access_loggers/open_telemetry/substitution_formatter.h @@ -28,12 +28,8 @@ class OpenTelemetryFormatter { public: OpenTelemetryFormatter(const ::opentelemetry::proto::common::v1::KeyValueList& format_mapping); - ::opentelemetry::proto::common::v1::KeyValueList - format(const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - const StreamInfo::StreamInfo& stream_info, absl::string_view local_reply_body, - AccessLog::AccessLogType access_log_type) const; + ::opentelemetry::proto::common::v1::KeyValueList format(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& info) const; private: struct OpenTelemetryFormatMapWrapper; @@ -77,11 +73,8 @@ class OpenTelemetryFormatter { // Methods for doing the actual formatting. ::opentelemetry::proto::common::v1::AnyValue providersCallback(const std::vector& providers, - const Http::RequestHeaderMap& request_headers, - const Http::ResponseHeaderMap& response_headers, - const Http::ResponseTrailerMap& response_trailers, - const StreamInfo::StreamInfo& stream_info, absl::string_view local_reply_body, - AccessLog::AccessLogType access_log_type) const; + const Formatter::HttpFormatterContext& context, + const StreamInfo::StreamInfo& info) const; ::opentelemetry::proto::common::v1::AnyValue openTelemetryFormatMapCallback( const OpenTelemetryFormatter::OpenTelemetryFormatMapWrapper& format_map, const OpenTelemetryFormatMapVisitor& visitor) const; diff --git a/source/extensions/access_loggers/stream/config.cc b/source/extensions/access_loggers/stream/config.cc index e9159e6f3556..b607e9284df0 100644 --- a/source/extensions/access_loggers/stream/config.cc +++ b/source/extensions/access_loggers/stream/config.cc @@ -46,7 +46,7 @@ std::string StdoutAccessLogFactory::name() const { return "envoy.access_loggers. /** * Static registration for the file access log. @see RegisterFactory. */ -LEGACY_REGISTER_FACTORY(StdoutAccessLogFactory, Server::Configuration::AccessLogInstanceFactory, +LEGACY_REGISTER_FACTORY(StdoutAccessLogFactory, AccessLog::AccessLogInstanceFactory, "envoy.stdout_access_log"); AccessLog::InstanceSharedPtr StderrAccessLogFactory::createAccessLogInstance( @@ -75,7 +75,7 @@ std::string StderrAccessLogFactory::name() const { return "envoy.access_loggers. /** * Static registration for the `stderr` access log. @see RegisterFactory. */ -LEGACY_REGISTER_FACTORY(StderrAccessLogFactory, Server::Configuration::AccessLogInstanceFactory, +LEGACY_REGISTER_FACTORY(StderrAccessLogFactory, AccessLog::AccessLogInstanceFactory, "envoy.stderr_access_log"); } // namespace File diff --git a/source/extensions/access_loggers/stream/config.h b/source/extensions/access_loggers/stream/config.h index 6fb9522fd229..492e90acbb37 100644 --- a/source/extensions/access_loggers/stream/config.h +++ b/source/extensions/access_loggers/stream/config.h @@ -1,6 +1,6 @@ #pragma once -#include "envoy/server/access_log_config.h" +#include "envoy/access_log/access_log_config.h" namespace Envoy { namespace Extensions { @@ -10,7 +10,7 @@ namespace File { /** * Config registration for the standard output access log. @see AccessLogInstanceFactory. */ -class StdoutAccessLogFactory : public Server::Configuration::AccessLogInstanceFactory { +class StdoutAccessLogFactory : public AccessLog::AccessLogInstanceFactory { public: AccessLog::InstanceSharedPtr createAccessLogInstance(const Protobuf::Message& config, AccessLog::FilterPtr&& filter, @@ -28,7 +28,7 @@ class StdoutAccessLogFactory : public Server::Configuration::AccessLogInstanceFa /** * Config registration for the standard error access log. @see AccessLogInstanceFactory. */ -class StderrAccessLogFactory : public Server::Configuration::AccessLogInstanceFactory { +class StderrAccessLogFactory : public AccessLog::AccessLogInstanceFactory { public: AccessLog::InstanceSharedPtr createAccessLogInstance(const Protobuf::Message& config, AccessLog::FilterPtr&& filter, diff --git a/source/extensions/access_loggers/wasm/wasm_access_log_impl.h b/source/extensions/access_loggers/wasm/wasm_access_log_impl.h index 21dd30273eeb..23aa79e013c1 100644 --- a/source/extensions/access_loggers/wasm/wasm_access_log_impl.h +++ b/source/extensions/access_loggers/wasm/wasm_access_log_impl.h @@ -20,14 +20,10 @@ class WasmAccessLog : public AccessLog::Instance { AccessLog::FilterPtr filter) : plugin_(plugin), tls_slot_(std::move(tls_slot)), filter_(std::move(filter)) {} - void log(const Http::RequestHeaderMap* request_headers, - const Http::ResponseHeaderMap* response_headers, - const Http::ResponseTrailerMap* response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) override { - if (filter_ && request_headers && response_headers && response_trailers) { - if (!filter_->evaluate(stream_info, *request_headers, *response_headers, *response_trailers, - access_log_type)) { + void log(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& stream_info) override { + if (filter_) { + if (!filter_->evaluate(context, stream_info)) { return; } } @@ -40,8 +36,7 @@ class WasmAccessLog : public AccessLog::Instance { return; } if (handle->wasmHandle()) { - handle->wasmHandle()->wasm()->log(plugin_, request_headers, response_headers, - response_trailers, stream_info, access_log_type); + handle->wasmHandle()->wasm()->log(plugin_, context, stream_info); } } diff --git a/source/extensions/common/wasm/context.cc b/source/extensions/common/wasm/context.cc index e43f296255e9..bf0196366c28 100644 --- a/source/extensions/common/wasm/context.cc +++ b/source/extensions/common/wasm/context.cc @@ -1476,10 +1476,8 @@ void Context::initializeWriteFilterCallbacks(Network::WriteFilterCallbacks& call network_write_filter_callbacks_ = &callbacks; } -void Context::log(const Http::RequestHeaderMap* request_headers, - const Http::ResponseHeaderMap* response_headers, - const Http::ResponseTrailerMap* response_trailers, - const StreamInfo::StreamInfo& stream_info, AccessLog::AccessLogType) { +void Context::log(const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& stream_info) { // `log` may be called multiple times due to mid-request logging -- we only want to run on the // last call. if (!stream_info.requestComplete().has_value()) { @@ -1495,10 +1493,10 @@ void Context::log(const Http::RequestHeaderMap* request_headers, } access_log_phase_ = true; - access_log_request_headers_ = request_headers; + access_log_request_headers_ = &context.requestHeaders(); // ? request_trailers ? - access_log_response_headers_ = response_headers; - access_log_response_trailers_ = response_trailers; + access_log_response_headers_ = &context.responseHeaders(); + access_log_response_trailers_ = &context.responseTrailers(); access_log_stream_info_ = &stream_info; onLog(); diff --git a/source/extensions/common/wasm/context.h b/source/extensions/common/wasm/context.h index e755d26a0871..c689920f4314 100644 --- a/source/extensions/common/wasm/context.h +++ b/source/extensions/common/wasm/context.h @@ -148,11 +148,7 @@ class Context : public proxy_wasm::ContextBase, const std::shared_ptr& plugin); // deprecated // AccessLog::Instance - void log(const Http::RequestHeaderMap* request_headers, - const Http::ResponseHeaderMap* response_headers, - const Http::ResponseTrailerMap* response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) override; + void log(const AccessLog::HttpLogContext& context, const StreamInfo::StreamInfo& info) override; uint32_t getLogLevel() override; diff --git a/source/extensions/common/wasm/wasm.cc b/source/extensions/common/wasm/wasm.cc index 17d40d003a9c..09a701d8d84b 100644 --- a/source/extensions/common/wasm/wasm.cc +++ b/source/extensions/common/wasm/wasm.cc @@ -226,13 +226,10 @@ ContextBase* Wasm::createRootContext(const std::shared_ptr& plugin) ContextBase* Wasm::createVmContext() { return new Context(this); } -void Wasm::log(const PluginSharedPtr& plugin, const Http::RequestHeaderMap* request_headers, - const Http::ResponseHeaderMap* response_headers, - const Http::ResponseTrailerMap* response_trailers, - const StreamInfo::StreamInfo& stream_info, - AccessLog::AccessLogType access_log_type) { +void Wasm::log(const PluginSharedPtr& plugin, const AccessLog::HttpLogContext& log_context, + const StreamInfo::StreamInfo& info) { auto context = getRootContext(plugin, true); - context->log(request_headers, response_headers, response_trailers, stream_info, access_log_type); + context->log(log_context, info); } void Wasm::onStatsUpdate(const PluginSharedPtr& plugin, Envoy::Stats::MetricSnapshot& snapshot) { diff --git a/source/extensions/common/wasm/wasm.h b/source/extensions/common/wasm/wasm.h index dc2b5d704d16..76889793d673 100644 --- a/source/extensions/common/wasm/wasm.h +++ b/source/extensions/common/wasm/wasm.h @@ -66,10 +66,8 @@ class Wasm : public WasmBase, Logger::Loggable { void getFunctions() override; // AccessLog::Instance - void log(const PluginSharedPtr& plugin, const Http::RequestHeaderMap* request_headers, - const Http::ResponseHeaderMap* response_headers, - const Http::ResponseTrailerMap* response_trailers, - const StreamInfo::StreamInfo& stream_info, AccessLog::AccessLogType access_log_type); + void log(const PluginSharedPtr& plugin, const AccessLog::HttpLogContext& context, + const StreamInfo::StreamInfo& info); void onStatsUpdate(const PluginSharedPtr& plugin, Envoy::Stats::MetricSnapshot& snapshot);