Skip to content

Commit

Permalink
Add originIp and originHost attributes. (envoyproxy#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiwzhang authored Jan 27, 2017
1 parent cea8827 commit 6d7f0ee
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 36 deletions.
2 changes: 2 additions & 0 deletions contrib/endpoints/include/api_manager/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Request {
// Gets Client IP
// This will be used by service control Check() call.
virtual std::string GetClientIP() = 0;
// Gets Client Host.
virtual std::string GetClientHost() { return ""; }

// Get GRPC stats.
virtual int64_t GetGrpcRequestBytes() = 0;
Expand Down
3 changes: 2 additions & 1 deletion contrib/endpoints/src/api_manager/context/request_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ void RequestContext::FillOperationInfo(service_control::OperationInfo *info) {
info->producer_project_id = service_context()->project_id();
info->referer = http_referer_;
info->request_start_time = start_time_;
info->client_ip = request_->GetClientIP();
info->client_host = request_->GetClientHost();
}

void RequestContext::FillLocation(service_control::ReportRequestInfo *info) {
Expand Down Expand Up @@ -221,7 +223,6 @@ void RequestContext::FillLogMessage(service_control::ReportRequestInfo *info) {
void RequestContext::FillCheckRequestInfo(
service_control::CheckRequestInfo *info) {
FillOperationInfo(info);
info->client_ip = request_->GetClientIP();
info->allow_unregistered_calls = method()->allow_unregistered_calls();
}

Expand Down
107 changes: 74 additions & 33 deletions contrib/endpoints/src/api_manager/mixer/mixer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ namespace api_manager {
namespace mixer {
namespace {

const std::string kProxyPeerID = "Istio/Proxy";

const std::string kAttrNameServiceName = "serviceName";
const std::string kAttrNamePeerId = "peerId";
const std::string kAttrNameOperationId = "operationId";
const std::string kAttrNameOperationName = "operationName";
const std::string kAttrNameApiKey = "apiKey";
const std::string kAttrNameResponseCode = "responseCode";
Expand All @@ -35,6 +38,9 @@ const std::string kAttrNameApiMethod = "apiMethod";
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";

Attributes::Value StringValue(const std::string& str) {
Attributes::Value v;
Expand All @@ -50,37 +56,6 @@ Attributes::Value Int64Value(int64_t value) {
return v;
}

void FillCheckAttributes(const service_control::CheckRequestInfo& info,
const std::string& service_name,
::istio::mixer_client::Attributes* attr) {
attr->attributes[kAttrNameServiceName] = StringValue(service_name);
attr->attributes[kAttrNamePeerId] = StringValue("Proxy");
attr->attributes[kAttrNameOperationName] = StringValue(info.operation_name);
attr->attributes[kAttrNameApiKey] = StringValue(info.api_key);
}

void FillReportAttributes(const service_control::ReportRequestInfo& info,
const std::string& service_name,
::istio::mixer_client::Attributes* attr) {
attr->attributes[kAttrNameServiceName] = StringValue(service_name);
attr->attributes[kAttrNamePeerId] = StringValue("Proxy");
attr->attributes[kAttrNameOperationName] = StringValue(info.operation_name);
attr->attributes[kAttrNameApiKey] = StringValue(info.api_key);

attr->attributes[kAttrNameURL] = StringValue(info.url);
attr->attributes[kAttrNameLocation] = StringValue(info.location);

attr->attributes[kAttrNameApiName] = StringValue(info.api_name);
attr->attributes[kAttrNameApiVersion] = StringValue(info.api_version);
attr->attributes[kAttrNameApiMethod] = StringValue(info.api_method);

attr->attributes[kAttrNameLogMessage] = StringValue(info.log_message);

attr->attributes[kAttrNameResponseCode] = Int64Value(info.response_code);
attr->attributes[kAttrNameRequestSize] = Int64Value(info.request_size);
attr->attributes[kAttrNameResponseSize] = Int64Value(info.response_size);
}

} // namespace

Mixer::Mixer(ApiManagerEnvInterface* env, const Config* config)
Expand All @@ -98,9 +73,75 @@ Status Mixer::Init() {

Status Mixer::Close() { return Status::OK; }

void Mixer::FillCommonAttributes(const service_control::OperationInfo& info,
::istio::mixer_client::Attributes* attr) {
attr->attributes[kAttrNameServiceName] = StringValue(config_->service_name());
attr->attributes[kAttrNamePeerId] = StringValue(kProxyPeerID);

if (!info.operation_id.empty()) {
attr->attributes[kAttrNameOperationId] = StringValue(info.operation_id);
}
if (!info.operation_name.empty()) {
attr->attributes[kAttrNameOperationName] = StringValue(info.operation_name);
}
if (!info.api_key.empty()) {
attr->attributes[kAttrNameApiKey] = StringValue(info.api_key);
}
if (!info.client_ip.empty()) {
attr->attributes[kAttrNameOriginIp] = StringValue(info.client_ip);
}
if (!info.client_host.empty()) {
attr->attributes[kAttrNameOriginHost] = StringValue(info.client_host);
}
}

void Mixer::FillCheckAttributes(const service_control::CheckRequestInfo& info,
::istio::mixer_client::Attributes* attr) {
FillCommonAttributes(info, attr);
}

void Mixer::FillReportAttributes(const service_control::ReportRequestInfo& info,
::istio::mixer_client::Attributes* attr) {
FillCommonAttributes(info, attr);

if (!info.url.empty()) {
attr->attributes[kAttrNameURL] = StringValue(info.url);
}
if (!info.location.empty()) {
attr->attributes[kAttrNameLocation] = StringValue(info.location);
}

if (!info.api_name.empty()) {
attr->attributes[kAttrNameApiName] = StringValue(info.api_name);
}
if (!info.api_version.empty()) {
attr->attributes[kAttrNameApiVersion] = StringValue(info.api_version);
}
if (!info.api_method.empty()) {
attr->attributes[kAttrNameApiMethod] = StringValue(info.api_method);
}

if (!info.log_message.empty()) {
attr->attributes[kAttrNameLogMessage] = StringValue(info.log_message);
}

attr->attributes[kAttrNameResponseCode] = Int64Value(info.response_code);
if (info.request_size >= 0) {
attr->attributes[kAttrNameRequestSize] = Int64Value(info.request_size);
}
if (info.response_size >= 0) {
attr->attributes[kAttrNameResponseSize] = Int64Value(info.response_size);
}

if (info.latency.request_time_ms >= 0) {
attr->attributes[kAttrNameResponseTime] =
Int64Value(info.latency.request_time_ms);
}
}

Status Mixer::Report(const service_control::ReportRequestInfo& info) {
::istio::mixer_client::Attributes attributes;
FillReportAttributes(info, config_->service_name(), &attributes);
FillReportAttributes(info, &attributes);
env_->LogInfo("Send Report: ");
mixer_client_->Report(
attributes, [this](const ::google::protobuf::util::Status& status) {
Expand All @@ -120,7 +161,7 @@ void Mixer::Check(
std::function<void(Status, const service_control::CheckResponseInfo&)>
on_done) {
::istio::mixer_client::Attributes attributes;
FillCheckAttributes(info, config_->service_name(), &attributes);
FillCheckAttributes(info, &attributes);
env_->LogInfo("Send Check: ");
mixer_client_->Check(
attributes,
Expand Down
10 changes: 10 additions & 0 deletions contrib/endpoints/src/api_manager/mixer/mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ class Mixer : public service_control::Interface {
// The constructor.
Mixer(ApiManagerEnvInterface* env, const Config* config);

// Fill common attributes for both check and report.
void FillCommonAttributes(const service_control::OperationInfo& info,
::istio::mixer_client::Attributes* attr);
// Fill attributes for check.
void FillCheckAttributes(const service_control::CheckRequestInfo& info,
::istio::mixer_client::Attributes* attr);
// Fill attributes for report.
void FillReportAttributes(const service_control::ReportRequestInfo& info,
::istio::mixer_client::Attributes* attr);

// The Api Manager environment interface.
ApiManagerEnvInterface* env_;
// The config.
Expand Down
8 changes: 6 additions & 2 deletions contrib/endpoints/src/api_manager/service_control/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ struct OperationInfo {
// and Report.
std::chrono::system_clock::time_point request_start_time;

// The client IP address.
std::string client_ip;

// The client host name.
std::string client_host;

OperationInfo() {}
};

// Information to fill Check request protobuf.
struct CheckRequestInfo : public OperationInfo {
// The client IP address.
std::string client_ip;
// Whether the method allow unregistered calls.
bool allow_unregistered_calls;

Expand Down
1 change: 1 addition & 0 deletions src/envoy/prototype/api_manager_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Request : public google::api_manager::Request {
return header_map_.Path()->value().c_str();
}
virtual std::string GetClientIP() override { return ""; }
virtual std::string GetClientHost() override { return ""; }
virtual bool FindQuery(const std::string& name, std::string* query) override {
if (!query_parsed_) {
auto header = header_map_.Path();
Expand Down

0 comments on commit 6d7f0ee

Please sign in to comment.