Skip to content

Commit

Permalink
dirty commit
Browse files Browse the repository at this point in the history
Signed-off-by: wbpcode <wbphub@live.com>
  • Loading branch information
wbpcode committed Nov 5, 2023
1 parent f0248a4 commit dd9d6f4
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 151 deletions.
21 changes: 5 additions & 16 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/substitution_formatter.h"

#include "source/common/protobuf/protobuf.h"

namespace Envoy {
namespace AccessLog {

using HttpLogContext = Envoy::Formatter::HttpFormatterContext;

class AccessLogFile {
public:
virtual ~AccessLogFile() = default;
Expand Down Expand Up @@ -92,23 +95,9 @@ template <class Context> class InstanceBase {
template <class Context> using InstanceBaseSharedPtr = std::shared_ptr<InstanceBase<Context>>;

/**
* Interface for access log filters.
* Interface for HTTP access log filters.
*/
class Filter {
public:
virtual ~Filter() = default;

/**
* Evaluate whether an access log should be written based on request and response data.
* @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;
};

using Filter = FilterBase<HttpLogContext>;
using FilterPtr = std::unique_ptr<Filter>;

/**
Expand Down
80 changes: 30 additions & 50 deletions source/common/access_log/access_log_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,23 @@ 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 AccessLog::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 AccessLog::HttpLogContext&,
const StreamInfo::StreamInfo& info) const {
if (!info.responseCode()) {
return compareAgainstValue(0ULL);
}

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 AccessLog::HttpLogContext&,
const StreamInfo::StreamInfo& info) const {
absl::optional<std::chrono::nanoseconds> duration = info.currentDuration();
if (!duration.has_value()) {
return false;
Expand All @@ -133,9 +130,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 AccessLog::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_) {
Expand Down Expand Up @@ -177,15 +173,11 @@ 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 AccessLog::HttpLogContext& log_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(log_context, info);

if (result) {
break;
Expand All @@ -195,15 +187,11 @@ 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 AccessLog::HttpLogContext& log_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(log_context, info);

if (!result) {
break;
Expand All @@ -213,20 +201,17 @@ 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 AccessLog::HttpLogContext&,
const StreamInfo::StreamInfo& info) const {
return !info.healthCheck();
}

HeaderFilter::HeaderFilter(const envoy::config::accesslog::v3::HeaderFilter& config)
: header_data_(std::make_unique<Http::HeaderUtility::HeaderData>(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 AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo&) const {
return Http::HeaderUtility::matchHeaders(log_context.requestHeaders(), *header_data_);
}

ResponseFlagFilter::ResponseFlagFilter(
Expand All @@ -240,9 +225,8 @@ ResponseFlagFilter::ResponseFlagFilter(
}
}

bool ResponseFlagFilter::evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap&,
const Http::ResponseHeaderMap&, const Http::ResponseTrailerMap&,
AccessLogType) const {
bool ResponseFlagFilter::evaluate(const AccessLog::HttpLogContext&,
const StreamInfo::StreamInfo& info) const {
if (configured_flags_ != 0) {
return info.intersectResponseFlags(configured_flags_);
}
Expand All @@ -257,14 +241,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 AccessLog::HttpLogContext& log_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);
const auto& optional_status = Grpc::Common::getGrpcStatus(log_context.responseTrailers(),
log_context.responseHeaders(), info);
if (optional_status.has_value()) {
status = optional_status.value();
}
Expand All @@ -286,10 +268,9 @@ 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 AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo&) const {
const bool found = types_.contains(log_context.accessLogType());
return exclude_ ? !found : found;
}

Expand All @@ -315,9 +296,8 @@ 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 AccessLog::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
Expand Down
60 changes: 24 additions & 36 deletions source/common/access_log/access_log_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ class StatusCodeFilter : public ComparisonFilter {
: 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;
bool evaluate(const AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;
};

/**
Expand All @@ -77,9 +76,8 @@ class DurationFilter : public ComparisonFilter {
: 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;
bool evaluate(const AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;
};

/**
Expand All @@ -104,9 +102,8 @@ class AndFilter : public OperatorFilter {
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;
bool evaluate(const AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;
};

/**
Expand All @@ -118,9 +115,8 @@ class OrFilter : public OperatorFilter {
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;
bool evaluate(const AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;
};

/**
Expand All @@ -131,9 +127,8 @@ class NotHealthCheckFilter : public Filter {
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;
bool evaluate(const AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;
};

/**
Expand All @@ -142,9 +137,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;
bool evaluate(const AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;
};

/**
Expand All @@ -156,9 +150,8 @@ class RuntimeFilter : public Filter {
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;
bool evaluate(const AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;

private:
Runtime::Loader& runtime_;
Expand All @@ -176,9 +169,8 @@ class HeaderFilter : public Filter {
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;
bool evaluate(const AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;

private:
const Http::HeaderUtility::HeaderDataPtr header_data_;
Expand All @@ -192,9 +184,8 @@ class ResponseFlagFilter : public Filter {
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;
bool evaluate(const AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;

private:
uint64_t configured_flags_{};
Expand All @@ -213,9 +204,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;
bool evaluate(const AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;

private:
GrpcStatusHashSet statuses_;
Expand All @@ -238,9 +228,8 @@ 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 AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;

private:
LogTypeHashSet types_;
Expand All @@ -254,9 +243,8 @@ 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 AccessLog::HttpLogContext& log_context,
const StreamInfo::StreamInfo& info) const override;

private:
Matchers::ValueMatcherConstSharedPtr present_matcher_;
Expand Down
9 changes: 6 additions & 3 deletions source/common/local_reply/local_reply.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ class ResponseMapper {
StreamInfo::StreamInfo& stream_info, Http::Code& code, std::string& body,
BodyFormatter*& final_formatter) const {
// If not matched, just bail out.
if (filter_ == nullptr ||
!filter_->evaluate(stream_info, request_headers, response_headers, response_trailers,
AccessLog::AccessLogType::NotSet)) {
if (filter_ == nullptr || !filter_->evaluate({&request_headers,
&response_headers,
&response_trailers,
{},
AccessLog::AccessLogType::NotSet},
stream_info)) {
return false;
}

Expand Down
6 changes: 4 additions & 2 deletions source/extensions/access_loggers/common/access_log_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ void ImplBase::log(const Http::RequestHeaderMap* request_headers,
const Http::ResponseTrailerMap* response_trailers,
const StreamInfo::StreamInfo& stream_info,
AccessLog::AccessLogType access_log_type) {
const AccessLog::HttpLogContext log_context{
request_headers, response_headers, response_trailers, {}, access_log_type};

if (!request_headers) {
request_headers = Http::StaticEmptyHeaders::get().request_headers.get();
}
Expand All @@ -22,8 +25,7 @@ void ImplBase::log(const Http::RequestHeaderMap* request_headers,
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)) {
if (filter_ && !filter_->evaluate(log_context, stream_info)) {
return;
}
return emitLog(*request_headers, *response_headers, *response_trailers, stream_info,
Expand Down
Loading

0 comments on commit dd9d6f4

Please sign in to comment.