Skip to content

Commit

Permalink
Use envoy new access_log handler for sending Report. (istio#60)
Browse files Browse the repository at this point in the history
* use access_log handler.

* Not to use Loggable base class.
  • Loading branch information
qiwzhang authored Feb 1, 2017
1 parent 2796209 commit 8218f92
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
61 changes: 38 additions & 23 deletions src/envoy/prototype/api_manager_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,30 @@ class Request : public google::api_manager::Request {
};

class Response : public google::api_manager::Response {
const AccessLog::RequestInfo& request_info_;

public:
Response(const AccessLog::RequestInfo& request_info)
: request_info_(request_info) {}

google::api_manager::utils::Status GetResponseStatus() {
return google::api_manager::utils::Status::OK;
}

std::size_t GetRequestSize() { return 0; }
std::size_t GetRequestSize() { return request_info_.bytesReceived(); }

std::size_t GetResponseSize() { return 0; }
std::size_t GetResponseSize() { return request_info_.bytesSent(); }

google::api_manager::utils::Status GetLatencyInfo(
google::api_manager::service_control::LatencyInfo* info) {
info->request_time_ms = request_info_.duration().count();
return google::api_manager::utils::Status::OK;
}
};

const Http::HeaderMapImpl BadRequest{{Http::Headers::get().Status, "400"}};

class Instance : public Http::StreamFilter,
public Logger::Loggable<Logger::Id::http> {
class Instance : public Http::StreamFilter, public Http::AccessLog::Instance {
private:
std::shared_ptr<google::api_manager::ApiManager> api_manager_;
std::unique_ptr<google::api_manager::RequestHandlerInterface>
Expand All @@ -207,12 +213,12 @@ class Instance : public Http::StreamFilter,
: api_manager_(config->api_manager()),
state_(NotStarted),
initiating_call_(false) {
log().debug("Called ApiManager::Instance : {}", __func__);
Log().debug("Called ApiManager::Instance : {}", __func__);
}

FilterHeadersStatus decodeHeaders(HeaderMap& headers,
bool end_stream) override {
log().debug("Called ApiManager::Instance : {}", __func__);
Log().debug("Called ApiManager::Instance : {}", __func__);
std::unique_ptr<google::api_manager::Request> request(
new Request(headers, decoder_callbacks_->downstreamAddress(),
getRouteVirtualHost(headers)));
Expand All @@ -227,13 +233,13 @@ class Instance : public Http::StreamFilter,
if (state_ == Complete) {
return FilterHeadersStatus::Continue;
}
log().debug("Called ApiManager::Instance : {} Stop", __func__);
Log().debug("Called ApiManager::Instance : {} Stop", __func__);
return FilterHeadersStatus::StopIteration;
}

FilterDataStatus decodeData(Buffer::Instance& data,
bool end_stream) override {
log().debug("Called ApiManager::Instance : {} ({}, {})", __func__,
Log().debug("Called ApiManager::Instance : {} ({}, {})", __func__,
data.length(), end_stream);
if (state_ == Calling) {
return FilterDataStatus::StopIterationAndBuffer;
Expand All @@ -242,21 +248,21 @@ class Instance : public Http::StreamFilter,
}

FilterTrailersStatus decodeTrailers(HeaderMap& trailers) override {
log().debug("Called ApiManager::Instance : {}", __func__);
Log().debug("Called ApiManager::Instance : {}", __func__);
if (state_ == Calling) {
return FilterTrailersStatus::StopIteration;
}
return FilterTrailersStatus::Continue;
}
void setDecoderFilterCallbacks(
StreamDecoderFilterCallbacks& callbacks) override {
log().debug("Called ApiManager::Instance : {}", __func__);
Log().debug("Called ApiManager::Instance : {}", __func__);
decoder_callbacks_ = &callbacks;
decoder_callbacks_->addResetStreamCallback(
[this]() { state_ = Responded; });
}
void completeCheck(const google::api_manager::utils::Status& status) {
log().debug("Called ApiManager::Instance : check complete {}",
Log().debug("Called ApiManager::Instance : check complete {}",
status.ToJson());
if (!status.ok() && state_ != Responded) {
state_ = Responded;
Expand All @@ -275,30 +281,37 @@ class Instance : public Http::StreamFilter,

virtual FilterHeadersStatus encodeHeaders(HeaderMap& headers,
bool end_stream) override {
log().debug("Called ApiManager::Instance : {}", __func__);
Log().debug("Called ApiManager::Instance : {}", __func__);
return FilterHeadersStatus::Continue;
}
virtual FilterDataStatus encodeData(Buffer::Instance& data,
bool end_stream) override {
log().debug("Called ApiManager::Instance : {}", __func__);
Log().debug("Called ApiManager::Instance : {}", __func__);
return FilterDataStatus::Continue;
}
virtual FilterTrailersStatus encodeTrailers(HeaderMap& trailers) override {
log().debug("Called ApiManager::Instance : {}", __func__);
Log().debug("Called ApiManager::Instance : {}", __func__);
return FilterTrailersStatus::Continue;
}
virtual void setEncoderFilterCallbacks(
StreamEncoderFilterCallbacks& callbacks) override {
log().debug("Called ApiManager::Instance : {}", __func__);
Log().debug("Called ApiManager::Instance : {}", __func__);
encoder_callbacks_ = &callbacks;
}

// note: cannot extend ~ActiveStream for access log, placing it here
~Instance() {
log().debug("Called ApiManager::Instance : {}", __func__);
std::unique_ptr<google::api_manager::Response> response(new Response());
request_handler_->Report(std::move(response),
[this]() { log().debug("Report returns"); });
virtual void log(const HeaderMap* request_headers,
const HeaderMap* response_headers,
const AccessLog::RequestInfo& request_info) override {
Log().debug("Called ApiManager::Instance : {}", __func__);
std::unique_ptr<google::api_manager::Response> response(
new Response(request_info));
request_handler_->Report(std::move(response), []() {});
}

spdlog::logger& Log() {
static spdlog::logger& instance =
Logger::Registry::getLog(Logger::Id::http);
return instance;
}
};
}
Expand All @@ -320,8 +333,10 @@ class ApiManagerConfig : public HttpFilterConfigFactory {
new Http::ApiManager::Config(config, server));
return [api_manager_config](
Http::FilterChainFactoryCallbacks& callbacks) -> void {
auto instance = new Http::ApiManager::Instance(api_manager_config);
callbacks.addStreamFilter(Http::StreamFilterPtr{instance});
std::shared_ptr<Http::ApiManager::Instance> instance(
new Http::ApiManager::Instance(api_manager_config));
callbacks.addStreamFilter(Http::StreamFilterPtr(instance));
callbacks.addAccessLogHandler(Http::AccessLog::InstancePtr(instance));
};
}
};
Expand Down
3 changes: 1 addition & 2 deletions src/envoy/prototype/envoy-esp.conf
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,5 @@
]
}
]
},
"tracing_enabled": "true"
}
}
2 changes: 1 addition & 1 deletion src/envoy/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,6 @@ cc_test(
native.new_git_repository(
name = "envoy_git",
remote = "https://github.com/lyft/envoy.git",
commit = "39f42378fa41c10996d4c3ffba534951de30ceb8",
commit = "0bac7508c6803ec315c2228672728281b99149bd",
build_file_content = BUILD,
)

0 comments on commit 8218f92

Please sign in to comment.