From 78d9c26ad6e421e211aafeaf08638bca3befabfe Mon Sep 17 00:00:00 2001 From: Wayne Zhang Date: Fri, 24 Feb 2017 14:59:39 -0800 Subject: [PATCH 1/3] Send headers as string map. --- src/envoy/mixer/http_control.cc | 97 +++++++++++++++++++++----------- src/envoy/mixer/repositories.bzl | 2 +- 2 files changed, 65 insertions(+), 34 deletions(-) diff --git a/src/envoy/mixer/http_control.cc b/src/envoy/mixer/http_control.cc index 84e31de5528..13c7f734cb9 100644 --- a/src/envoy/mixer/http_control.cc +++ b/src/envoy/mixer/http_control.cc @@ -33,18 +33,22 @@ namespace { // Define lower case string for X-Forwarded-Host. const LowerCaseString kHeaderNameXFH("x-forwarded-host", false); -const std::string kRequestHeaderPrefix = "request.headers."; -const std::string kResponseHeaderPrefix = "response.headers."; - // Define attribute names -const std::string kAttrNameRequestPath = "request.path"; -const std::string kAttrNameRequestSize = "request.size"; -const std::string kAttrNameResponseSize = "response.size"; -const std::string kAttrNameResponseTime = "response.time"; -const std::string kAttrNameOriginIp = "origin.ip"; -const std::string kAttrNameOriginHost = "origin.host"; +const std::string kRequestPath = "request.path"; +const std::string kRequestHost = "request.host"; +const std::string kRequestSize = "request.size"; +const std::string kRequestTime = "request.time"; +const std::string kRequestHeaders = "request.headers"; + +const std::string kResponseHeaders = "response.headers"; +const std::string kResponseSize = "response.size"; +const std::string kResponseTime = "response.time"; +const std::string kResponseLatency = "response.latency"; const std::string kResponseHttpCode = "response.http.code"; +const std::string kOriginIp = "origin.ip"; +const std::string kOriginHost = "origin.host"; + Attributes::Value StringValue(const std::string& str) { Attributes::Value v; v.type = Attributes::Value::STRING; @@ -52,6 +56,14 @@ Attributes::Value StringValue(const std::string& str) { return v; } +Attributes::Value StringMapValue( + std::map&& string_map) { + Attributes::Value v; + v.type = Attributes::Value::STRING_MAP; + v.string_map_v.swap(string_map); + return v; +} + Attributes::Value Int64Value(int64_t value) { Attributes::Value v; v.type = Attributes::Value::INT64; @@ -59,6 +71,21 @@ Attributes::Value Int64Value(int64_t value) { return v; } +Attributes::Value TimeValue( + std::chrono::time_point value) { + Attributes::Value v; + v.type = Attributes::Value::TIME; + v.time_v = value; + return v; +} + +Attributes::Value DurationValue(std::chrono::nanoseconds value) { + Attributes::Value v; + v.type = Attributes::Value::DURATION; + v.duration_nanos_v = value; + return v; +} + void SetStringAttribute(const std::string& name, const std::string& value, Attributes* attr) { if (!value.empty()) { @@ -91,47 +118,50 @@ std::string GetLastForwardedHost(const HeaderMap& header_map) { return xff_list.back(); } -void FillRequestHeaderAttributes(const HeaderMap& header_map, - Attributes* attr) { - // Pass in all headers +std::map ExtractHeaders(const HeaderMap& header_map) { + std::map headers; header_map.iterate( [](const HeaderEntry& header, void* context) { - Attributes* attr = static_cast(context); - attr->attributes[kRequestHeaderPrefix + header.key().c_str()] = - StringValue(header.value().c_str()); + std::map* header_map = + static_cast*>(context); + (*header_map)[header.key().c_str()] = header.value().c_str(); }, - attr); + &headers); + return headers; +} - SetStringAttribute(kAttrNameRequestPath, header_map.Path()->value().c_str(), - attr); - SetStringAttribute(kAttrNameOriginIp, GetFirstForwardedFor(header_map), attr); - SetStringAttribute(kAttrNameOriginHost, GetLastForwardedHost(header_map), - attr); +void FillRequestHeaderAttributes(const HeaderMap& header_map, + Attributes* attr) { + SetStringAttribute(kRequestPath, header_map.Path()->value().c_str(), attr); + SetStringAttribute(kRequestHost, header_map.Host()->value().c_str(), attr); + attr->attributes[kRequestTime] = TimeValue(std::chrono::system_clock::now()); + SetStringAttribute(kOriginIp, GetFirstForwardedFor(header_map), attr); + SetStringAttribute(kOriginHost, GetLastForwardedHost(header_map), attr); + attr->attributes[kRequestHeaders] = + StringMapValue(ExtractHeaders(header_map)); } void FillResponseHeaderAttributes(const HeaderMap& header_map, Attributes* attr) { - header_map.iterate( - [](const HeaderEntry& header, void* context) { - Attributes* attr = static_cast(context); - attr->attributes[kResponseHeaderPrefix + header.key().c_str()] = - StringValue(header.value().c_str()); - }, - attr); + attr->attributes[kResponseHeaders] = + StringMapValue(ExtractHeaders(header_map)); + attr->attributes[kResponseTime] = TimeValue(std::chrono::system_clock::now()); } void FillRequestInfoAttributes(const AccessLog::RequestInfo& info, int check_status_code, Attributes* attr) { if (info.bytesReceived() >= 0) { - attr->attributes[kAttrNameRequestSize] = Int64Value(info.bytesReceived()); + attr->attributes[kRequestSize] = Int64Value(info.bytesReceived()); } if (info.bytesSent() >= 0) { - attr->attributes[kAttrNameResponseSize] = Int64Value(info.bytesSent()); + attr->attributes[kResponseSize] = Int64Value(info.bytesSent()); } - if (info.duration().count() >= 0) { - attr->attributes[kAttrNameResponseTime] = - Int64Value(info.duration().count()); + + if (info.duration().count() > 0) { + attr->attributes[kResponseLatency] = DurationValue( + std::chrono::duration_cast(info.duration())); } + if (info.responseCode().valid()) { attr->attributes[kResponseHttpCode] = Int64Value(info.responseCode().value()); @@ -184,6 +214,7 @@ void HttpControl::Report(HttpRequestDataPtr request_data, // Use all Check attributes for Report. // Add additional Report attributes. FillResponseHeaderAttributes(*response_headers, &request_data->attributes); + FillRequestInfoAttributes(request_info, check_status, &request_data->attributes); log().debug("Send Report: {}", request_data->attributes.DebugString()); diff --git a/src/envoy/mixer/repositories.bzl b/src/envoy/mixer/repositories.bzl index 1ec26f2a47c..76a4aa9a113 100644 --- a/src/envoy/mixer/repositories.bzl +++ b/src/envoy/mixer/repositories.bzl @@ -15,7 +15,7 @@ ################################################################################ # -MIXER_CLIENT = "7b8544d765f9d7d86d28770c8d27d69cbf9509ac" +MIXER_CLIENT = "d0c57ab0d74887feb11f8644fa98538208f14dbc" def mixer_client_repositories(bind=True): native.git_repository( From f77744adb66210c66e0229ff0fe1b67b5387547c Mon Sep 17 00:00:00 2001 From: Wayne Zhang Date: Fri, 24 Feb 2017 16:51:06 -0800 Subject: [PATCH 2/3] Remove origin.ip and origin.host. --- src/envoy/mixer/http_control.cc | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/src/envoy/mixer/http_control.cc b/src/envoy/mixer/http_control.cc index 13c7f734cb9..4d3c92025a3 100644 --- a/src/envoy/mixer/http_control.cc +++ b/src/envoy/mixer/http_control.cc @@ -30,8 +30,6 @@ namespace Http { namespace Mixer { namespace { -// Define lower case string for X-Forwarded-Host. -const LowerCaseString kHeaderNameXFH("x-forwarded-host", false); // Define attribute names const std::string kRequestPath = "request.path"; @@ -46,9 +44,6 @@ const std::string kResponseTime = "response.time"; const std::string kResponseLatency = "response.latency"; const std::string kResponseHttpCode = "response.http.code"; -const std::string kOriginIp = "origin.ip"; -const std::string kOriginHost = "origin.host"; - Attributes::Value StringValue(const std::string& str) { Attributes::Value v; v.type = Attributes::Value::STRING; @@ -93,31 +88,6 @@ void SetStringAttribute(const std::string& name, const std::string& value, } } -std::string GetFirstForwardedFor(const HeaderMap& header_map) { - if (!header_map.ForwardedFor()) { - return ""; - } - std::vector xff_address_list = - StringUtil::split(header_map.ForwardedFor()->value().c_str(), ','); - if (xff_address_list.empty()) { - return ""; - } - return xff_address_list.front(); -} - -std::string GetLastForwardedHost(const HeaderMap& header_map) { - const HeaderEntry* entry = header_map.get(kHeaderNameXFH); - if (entry == nullptr) { - return ""; - } - std::vector xff_list = - StringUtil::split(entry->value().c_str(), ','); - if (xff_list.empty()) { - return ""; - } - return xff_list.back(); -} - std::map ExtractHeaders(const HeaderMap& header_map) { std::map headers; header_map.iterate( @@ -135,8 +105,6 @@ void FillRequestHeaderAttributes(const HeaderMap& header_map, SetStringAttribute(kRequestPath, header_map.Path()->value().c_str(), attr); SetStringAttribute(kRequestHost, header_map.Host()->value().c_str(), attr); attr->attributes[kRequestTime] = TimeValue(std::chrono::system_clock::now()); - SetStringAttribute(kOriginIp, GetFirstForwardedFor(header_map), attr); - SetStringAttribute(kOriginHost, GetLastForwardedHost(header_map), attr); attr->attributes[kRequestHeaders] = StringMapValue(ExtractHeaders(header_map)); } From 5ab8a2c1395850ef6abcaac6cb2aca35e48f191b Mon Sep 17 00:00:00 2001 From: Wayne Zhang Date: Fri, 24 Feb 2017 17:01:13 -0800 Subject: [PATCH 3/3] Fix format --- src/envoy/mixer/http_control.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/envoy/mixer/http_control.cc b/src/envoy/mixer/http_control.cc index 4d3c92025a3..677a8a9a3f3 100644 --- a/src/envoy/mixer/http_control.cc +++ b/src/envoy/mixer/http_control.cc @@ -30,7 +30,6 @@ namespace Http { namespace Mixer { namespace { - // Define attribute names const std::string kRequestPath = "request.path"; const std::string kRequestHost = "request.host";