-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Integrate mixer client directly with envoy. #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Copyright 2016 Google Inc. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| ################################################################################ | ||
| # | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| cc_binary( | ||
| name = "envoy_esp", | ||
| srcs = [ | ||
| "http_control.cc", | ||
| "http_control.h", | ||
| "http_filter.cc", | ||
| ], | ||
| linkstatic = 1, | ||
| deps = [ | ||
| "//external:mixer_client_lib", | ||
| "@envoy_git//:envoy-common", | ||
| "@envoy_git//:envoy-main", | ||
| ], | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| { | ||
| "listeners": [ | ||
| { | ||
| "port": 9090, | ||
| "bind_to_port": true, | ||
| "filters": [ | ||
| { | ||
| "type": "read", | ||
| "name": "http_connection_manager", | ||
| "config": { | ||
| "codec_type": "auto", | ||
| "stat_prefix": "ingress_http", | ||
| "route_config": { | ||
| "virtual_hosts": [ | ||
| { | ||
| "name": "backend", | ||
| "domains": ["*"], | ||
| "routes": [ | ||
| { | ||
| "timeout_ms": 0, | ||
| "prefix": "/", | ||
| "cluster": "service1" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }, | ||
| "access_log": [ | ||
| { | ||
| "path": "/dev/stdout" | ||
| } | ||
| ], | ||
| "filters": [ | ||
| { | ||
| "type": "both", | ||
| "name": "esp", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to "mixerclient"
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed to mixer. |
||
| "config": { | ||
| "mixer_server": "localhost:9091" | ||
| } | ||
| }, | ||
| { | ||
| "type": "decoder", | ||
| "name": "router", | ||
| "config": {} | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "admin": { | ||
| "access_log_path": "/dev/stdout", | ||
| "port": 9001 | ||
| }, | ||
| "cluster_manager": { | ||
| "clusters": [ | ||
| { | ||
| "name": "service1", | ||
| "connect_timeout_ms": 5000, | ||
| "type": "strict_dns", | ||
| "lb_type": "round_robin", | ||
| "hosts": [ | ||
| { | ||
| "url": "tcp://localhost:8080" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "src/envoy/mixer/http_control.h" | ||
| #include "common/common/utility.h" | ||
| #include "common/http/utility.h" | ||
|
|
||
| using ::google::protobuf::util::Status; | ||
| using ::istio::mixer_client::Attributes; | ||
| using ::istio::mixer_client::DoneFunc; | ||
|
|
||
| namespace Http { | ||
| namespace Mixer { | ||
| namespace { | ||
|
|
||
| const std::string kProxyPeerID = "Istio/Proxy"; | ||
| const std::string kEnvNameTargetService = "TARGET_SERVICE"; | ||
|
|
||
| // Define lower case string for X-Forwarded-Host. | ||
| const LowerCaseString kHeaderNameXFH("x-forwarded-host", false); | ||
|
|
||
| // Define attribute names | ||
| const std::string kAttrNamePeerId = "peerId"; | ||
| const std::string kAttrNameURL = "url"; | ||
| const std::string kAttrNameHttpMethod = "httpMethod"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add x-request-id as an attribute since it's very useful for understanding dataflow.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added all http headers. It looks like these HTTP-request-header::authority: (STRING): localhost:9090
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mandarjog, @geeknoid is there some namespace for HTTP header attributes? Or should they be translated to some canonical attribute name? |
||
| const std::string kAttrNameRequestSize = "requestSize"; | ||
| const std::string kAttrNameResponseSize = "responseSize"; | ||
| const std::string kAttrNameLogMessage = "logMessage"; | ||
| const std::string kAttrNameResponseTime = "responseTime"; | ||
| const std::string kAttrNameOriginIp = "originIp"; | ||
| const std::string kAttrNameOriginHost = "originHost"; | ||
| const std::string kAttrNameTargetService = "targetService"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to source and destination?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let us keep it as target for now. |
||
|
|
||
| Attributes::Value StringValue(const std::string& str) { | ||
| Attributes::Value v; | ||
| v.type = Attributes::Value::STRING; | ||
| v.str_v = str; | ||
| return v; | ||
| } | ||
|
|
||
| Attributes::Value Int64Value(int64_t value) { | ||
| Attributes::Value v; | ||
| v.type = Attributes::Value::INT64; | ||
| v.value.int64_v = value; | ||
| return v; | ||
| } | ||
|
|
||
| void SetStringAttribute(const std::string& name, const std::string& value, | ||
| Attributes* attr) { | ||
| if (!value.empty()) { | ||
| attr->attributes[name] = StringValue(value); | ||
| } | ||
| } | ||
|
|
||
| std::string GetFirstForwardedFor(const HeaderMap& header_map) { | ||
| if (!header_map.ForwardedFor()) { | ||
| return ""; | ||
| } | ||
| std::vector<std::string> 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 ""; | ||
| } | ||
| auto xff_list = StringUtil::split(entry->value().c_str(), ','); | ||
| if (xff_list.empty()) { | ||
| return ""; | ||
| } | ||
| return xff_list.back(); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| HttpControl::HttpControl(const std::string& mixer_server) { | ||
| ::istio::mixer_client::MixerClientOptions options; | ||
| options.mixer_server = mixer_server; | ||
| mixer_client_ = ::istio::mixer_client::CreateMixerClient(options); | ||
|
|
||
| auto target_service = getenv(kEnvNameTargetService.c_str()); | ||
| if (target_service) { | ||
| target_service_ = target_service; | ||
| } | ||
| } | ||
|
|
||
| void HttpControl::FillCheckAttributes(const HeaderMap& header_map, | ||
| Attributes* attr) { | ||
| // Pass in all headers | ||
| header_map.iterate( | ||
| [](const HeaderEntry& header, void* context) -> void { | ||
| auto attr = static_cast<Attributes*>(context); | ||
| attr->attributes[header.key().c_str()] = | ||
| StringValue(header.value().c_str()); | ||
| }, | ||
| attr); | ||
|
|
||
| // Pass in all Query parameters. | ||
| auto path = header_map.Path(); | ||
| if (path != nullptr) { | ||
| for (const auto& it : Utility::parseQueryString(path->value().c_str())) { | ||
| attr->attributes[it.first] = StringValue(it.second); | ||
| } | ||
| } | ||
|
|
||
| SetStringAttribute(kAttrNameOriginIp, GetFirstForwardedFor(header_map), attr); | ||
| SetStringAttribute(kAttrNameOriginHost, GetLastForwardedHost(header_map), | ||
| attr); | ||
|
|
||
| SetStringAttribute(kAttrNameTargetService, target_service_, attr); | ||
| attr->attributes[kAttrNamePeerId] = StringValue(kProxyPeerID); | ||
| } | ||
|
|
||
| void HttpControl::FillReportAttributes(const AccessLog::RequestInfo& info, | ||
| Attributes* attr) { | ||
| if (info.bytesReceived() >= 0) { | ||
| attr->attributes[kAttrNameRequestSize] = Int64Value(info.bytesReceived()); | ||
| } | ||
| if (info.bytesSent() >= 0) { | ||
| attr->attributes[kAttrNameResponseSize] = Int64Value(info.bytesSent()); | ||
| } | ||
| if (info.duration().count() >= 0) { | ||
| attr->attributes[kAttrNameResponseTime] = | ||
| Int64Value(info.duration().count()); | ||
| } | ||
| } | ||
|
|
||
| void HttpControl::Check(HttpRequestDataPtr request_data, HeaderMap& headers, | ||
| DoneFunc on_done) { | ||
| FillCheckAttributes(headers, &request_data->attributes); | ||
| log().debug("Send Check: {}", request_data->attributes.DebugString()); | ||
| mixer_client_->Check(request_data->attributes, on_done); | ||
| } | ||
|
|
||
| void HttpControl::Report(HttpRequestDataPtr request_data, | ||
| const AccessLog::RequestInfo& request_info, | ||
| DoneFunc on_done) { | ||
| // Use all Check attributes for Report. | ||
| // Add additional Report attributes. | ||
| FillReportAttributes(request_info, &request_data->attributes); | ||
| log().debug("Send Report: {}", request_data->attributes.DebugString()); | ||
| mixer_client_->Report(request_data->attributes, on_done); | ||
| } | ||
|
|
||
| } // namespace Mixer | ||
| } // namespace Http | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "precompiled/precompiled.h" | ||
|
|
||
| #include "common/common/logger.h" | ||
| #include "common/http/headers.h" | ||
| #include "envoy/http/access_log.h" | ||
| #include "include/client.h" | ||
|
|
||
| namespace Http { | ||
| namespace Mixer { | ||
|
|
||
| // Store data from Check to report | ||
| struct HttpRequestData { | ||
| ::istio::mixer_client::Attributes attributes; | ||
| }; | ||
| typedef std::shared_ptr<HttpRequestData> HttpRequestDataPtr; | ||
|
|
||
| // The mixer client class to control HTTP requests. | ||
| // It has Check() to validate if a request can be processed. | ||
| // At the end of request, call Report(). | ||
| class HttpControl final : public Logger::Loggable<Logger::Id::http> { | ||
| public: | ||
| // The constructor. | ||
| HttpControl(const std::string& mixer_server); | ||
|
|
||
| // Make mixer check call. | ||
| void Check(HttpRequestDataPtr request_data, HeaderMap& headers, | ||
| ::istio::mixer_client::DoneFunc on_done); | ||
|
|
||
| // Make mixer report call. | ||
| void Report(HttpRequestDataPtr request_data, | ||
| const AccessLog::RequestInfo& request_info, | ||
| ::istio::mixer_client::DoneFunc on_done); | ||
|
|
||
| private: | ||
| void FillCheckAttributes(const HeaderMap& header_map, | ||
| ::istio::mixer_client::Attributes* attr); | ||
| void FillReportAttributes(const AccessLog::RequestInfo& info, | ||
| ::istio::mixer_client::Attributes* attr); | ||
|
|
||
| // The mixer client | ||
| std::unique_ptr<::istio::mixer_client::MixerClient> mixer_client_; | ||
| // Target service | ||
| std::string target_service_; | ||
| }; | ||
|
|
||
| } // namespace Mixer | ||
| } // namespace Http |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not suffix _esp?
it would be nice to export a cc_library, and a cc_binary so it can be linked together.