Skip to content

Commit

Permalink
first commit to use formatter context
Browse files Browse the repository at this point in the history
Signed-off-by: wbpcode <wbphub@live.com>
  • Loading branch information
wbpcode committed Nov 3, 2023
1 parent edf0552 commit c07536e
Show file tree
Hide file tree
Showing 40 changed files with 312 additions and 465 deletions.
1 change: 1 addition & 0 deletions envoy/access_log/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
25 changes: 11 additions & 14 deletions envoy/access_log/access_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Filter>;
Expand All @@ -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<Instance>;
Expand Down
37 changes: 37 additions & 0 deletions envoy/access_log/access_log_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,42 @@ template <class Context> 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
11 changes: 11 additions & 0 deletions envoy/formatter/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
115 changes: 115 additions & 0 deletions envoy/formatter/http_specific_formatter.h
Original file line number Diff line number Diff line change
@@ -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
102 changes: 1 addition & 101 deletions envoy/formatter/substitution_formatter.h
Original file line number Diff line number Diff line change
@@ -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<HttpFormatterContext>;
using FormatterPtr = std::unique_ptr<Formatter>;
using FormatterConstSharedPtr = std::shared_ptr<const Formatter>;
Expand Down
10 changes: 0 additions & 10 deletions envoy/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
Loading

0 comments on commit c07536e

Please sign in to comment.