From cded6f32bdd58173ca15829d139ee17a4bab5620 Mon Sep 17 00:00:00 2001 From: Qian Sun Date: Thu, 6 Apr 2017 10:35:22 -0700 Subject: [PATCH 1/4] Send delta metrics for intermediate reports. --- contrib/endpoints/src/api_manager/request_handler.cc | 7 +++++++ contrib/endpoints/src/api_manager/request_handler.h | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/contrib/endpoints/src/api_manager/request_handler.cc b/contrib/endpoints/src/api_manager/request_handler.cc index ac56f02ce46..20a8715d54d 100644 --- a/contrib/endpoints/src/api_manager/request_handler.cc +++ b/contrib/endpoints/src/api_manager/request_handler.cc @@ -65,6 +65,10 @@ void RequestHandler::AttemptIntermediateReport() { info.is_final_report = false; context_->FillReportRequestInfo(NULL, &info); + // Make sure we send delta metrics for intermediate reports. + info.request_bytes -= last_request_bytes; + info.response_bytes -= last_response_bytes; + // Calling service_control Report. Status status = context_->service_context()->service_control()->Report(info); if (!status.ok()) { @@ -74,6 +78,9 @@ void RequestHandler::AttemptIntermediateReport() { context_->set_first_report(false); } context_->set_last_report_time(std::chrono::steady_clock::now()); + + last_request_bytes += info.request_bytes; + last_response_bytes += info.response_bytes; } // Sends a report. diff --git a/contrib/endpoints/src/api_manager/request_handler.h b/contrib/endpoints/src/api_manager/request_handler.h index 96c18b4e1a5..42f73394626 100644 --- a/contrib/endpoints/src/api_manager/request_handler.h +++ b/contrib/endpoints/src/api_manager/request_handler.h @@ -31,7 +31,9 @@ class RequestHandler : public RequestHandlerInterface { std::unique_ptr request_data) : context_(new context::RequestContext(service_context, std::move(request_data))), - check_workflow_(check_workflow) {} + check_workflow_(check_workflow), + last_request_bytes(0), + last_response_bytes(0) {} virtual ~RequestHandler(){}; @@ -60,6 +62,10 @@ class RequestHandler : public RequestHandlerInterface { std::shared_ptr context_; std::shared_ptr check_workflow_; + + int64_t last_request_bytes; + + int64_t last_response_bytes; }; } // namespace api_manager From 3366f85c1fa92583681dca1f093898f361992f6b Mon Sep 17 00:00:00 2001 From: Qian Sun Date: Thu, 6 Apr 2017 11:07:53 -0700 Subject: [PATCH 2/4] Move last_request_bytes/last_response_bytes to RequestContext. --- .../src/api_manager/context/request_context.cc | 12 +++++++++--- .../src/api_manager/context/request_context.h | 4 ++++ contrib/endpoints/src/api_manager/request_handler.cc | 7 ------- contrib/endpoints/src/api_manager/request_handler.h | 8 +------- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/contrib/endpoints/src/api_manager/context/request_context.cc b/contrib/endpoints/src/api_manager/context/request_context.cc index 2c0a2985927..aae9e60926c 100644 --- a/contrib/endpoints/src/api_manager/context/request_context.cc +++ b/contrib/endpoints/src/api_manager/context/request_context.cc @@ -81,7 +81,9 @@ RequestContext::RequestContext(std::shared_ptr service_context, std::unique_ptr request) : service_context_(service_context), request_(std::move(request)), - is_first_report_(true) { + is_first_report_(true), + last_request_bytes(0), + last_response_bytes(0) { start_time_ = std::chrono::system_clock::now(); last_report_time_ = std::chrono::steady_clock::now(); operation_id_ = GenerateUUID(); @@ -267,8 +269,12 @@ void RequestContext::FillReportRequestInfo( info->auth_audience = auth_audience_; if (!info->is_final_report) { - info->request_bytes = request_->GetGrpcRequestBytes(); - info->response_bytes = request_->GetGrpcResponseBytes(); + // Make sure we send delta metrics for intermediate reports. + info->request_bytes = request_->GetGrpcRequestBytes() - last_request_bytes; + info->response_bytes = + request_->GetGrpcResponseBytes() - last_response_bytes; + last_request_bytes += info->request_bytes; + last_response_bytes += info->response_bytes; } else { info->request_size = response->GetRequestSize(); info->response_size = response->GetResponseSize(); diff --git a/contrib/endpoints/src/api_manager/context/request_context.h b/contrib/endpoints/src/api_manager/context/request_context.h index 8832b38fc3e..af4e4c46de2 100644 --- a/contrib/endpoints/src/api_manager/context/request_context.h +++ b/contrib/endpoints/src/api_manager/context/request_context.h @@ -180,6 +180,10 @@ class RequestContext { // The time point of last intermediate report std::chrono::steady_clock::time_point last_report_time_; + + // The accumulated data sent till last intermediate report + int64_t last_request_bytes; + int64_t last_response_bytes; }; } // namespace context diff --git a/contrib/endpoints/src/api_manager/request_handler.cc b/contrib/endpoints/src/api_manager/request_handler.cc index 20a8715d54d..ac56f02ce46 100644 --- a/contrib/endpoints/src/api_manager/request_handler.cc +++ b/contrib/endpoints/src/api_manager/request_handler.cc @@ -65,10 +65,6 @@ void RequestHandler::AttemptIntermediateReport() { info.is_final_report = false; context_->FillReportRequestInfo(NULL, &info); - // Make sure we send delta metrics for intermediate reports. - info.request_bytes -= last_request_bytes; - info.response_bytes -= last_response_bytes; - // Calling service_control Report. Status status = context_->service_context()->service_control()->Report(info); if (!status.ok()) { @@ -78,9 +74,6 @@ void RequestHandler::AttemptIntermediateReport() { context_->set_first_report(false); } context_->set_last_report_time(std::chrono::steady_clock::now()); - - last_request_bytes += info.request_bytes; - last_response_bytes += info.response_bytes; } // Sends a report. diff --git a/contrib/endpoints/src/api_manager/request_handler.h b/contrib/endpoints/src/api_manager/request_handler.h index 42f73394626..96c18b4e1a5 100644 --- a/contrib/endpoints/src/api_manager/request_handler.h +++ b/contrib/endpoints/src/api_manager/request_handler.h @@ -31,9 +31,7 @@ class RequestHandler : public RequestHandlerInterface { std::unique_ptr request_data) : context_(new context::RequestContext(service_context, std::move(request_data))), - check_workflow_(check_workflow), - last_request_bytes(0), - last_response_bytes(0) {} + check_workflow_(check_workflow) {} virtual ~RequestHandler(){}; @@ -62,10 +60,6 @@ class RequestHandler : public RequestHandlerInterface { std::shared_ptr context_; std::shared_ptr check_workflow_; - - int64_t last_request_bytes; - - int64_t last_response_bytes; }; } // namespace api_manager From bf59cddab57458240e70d6c72415a3bbff5728c0 Mon Sep 17 00:00:00 2001 From: Qian Sun Date: Thu, 6 Apr 2017 12:54:25 -0700 Subject: [PATCH 3/4] Handle final report. --- .../src/api_manager/context/request_context.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/contrib/endpoints/src/api_manager/context/request_context.cc b/contrib/endpoints/src/api_manager/context/request_context.cc index aae9e60926c..f6b066dcce5 100644 --- a/contrib/endpoints/src/api_manager/context/request_context.cc +++ b/contrib/endpoints/src/api_manager/context/request_context.cc @@ -278,8 +278,14 @@ void RequestContext::FillReportRequestInfo( } else { info->request_size = response->GetRequestSize(); info->response_size = response->GetResponseSize(); - info->request_bytes = info->request_size; - info->response_bytes = info->response_size; + info->request_bytes = info->request_size - last_request_bytes; + if (info->request_bytes < 0) { + info->request_bytes = 0; + } + info->response_bytes = info->response_size - last_response_bytes; + if (info->response_bytes < 0) { + info->response_bytes = 0; + } info->streaming_request_message_counts = request_->GetGrpcRequestMessageCounts(); From 16d4091dbab8c4cc023730b00ec2b8b5dc800bc8 Mon Sep 17 00:00:00 2001 From: Qian Sun Date: Fri, 7 Apr 2017 09:37:04 -0700 Subject: [PATCH 4/4] Address comment. --- .../src/api_manager/context/request_context.cc | 16 ++++++++-------- .../src/api_manager/context/request_context.h | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/contrib/endpoints/src/api_manager/context/request_context.cc b/contrib/endpoints/src/api_manager/context/request_context.cc index f6b066dcce5..4703e2e5ee7 100644 --- a/contrib/endpoints/src/api_manager/context/request_context.cc +++ b/contrib/endpoints/src/api_manager/context/request_context.cc @@ -82,8 +82,8 @@ RequestContext::RequestContext(std::shared_ptr service_context, : service_context_(service_context), request_(std::move(request)), is_first_report_(true), - last_request_bytes(0), - last_response_bytes(0) { + last_request_bytes_(0), + last_response_bytes_(0) { start_time_ = std::chrono::system_clock::now(); last_report_time_ = std::chrono::steady_clock::now(); operation_id_ = GenerateUUID(); @@ -270,19 +270,19 @@ void RequestContext::FillReportRequestInfo( if (!info->is_final_report) { // Make sure we send delta metrics for intermediate reports. - info->request_bytes = request_->GetGrpcRequestBytes() - last_request_bytes; + info->request_bytes = request_->GetGrpcRequestBytes() - last_request_bytes_; info->response_bytes = - request_->GetGrpcResponseBytes() - last_response_bytes; - last_request_bytes += info->request_bytes; - last_response_bytes += info->response_bytes; + request_->GetGrpcResponseBytes() - last_response_bytes_; + last_request_bytes_ += info->request_bytes; + last_response_bytes_ += info->response_bytes; } else { info->request_size = response->GetRequestSize(); info->response_size = response->GetResponseSize(); - info->request_bytes = info->request_size - last_request_bytes; + info->request_bytes = info->request_size - last_request_bytes_; if (info->request_bytes < 0) { info->request_bytes = 0; } - info->response_bytes = info->response_size - last_response_bytes; + info->response_bytes = info->response_size - last_response_bytes_; if (info->response_bytes < 0) { info->response_bytes = 0; } diff --git a/contrib/endpoints/src/api_manager/context/request_context.h b/contrib/endpoints/src/api_manager/context/request_context.h index af4e4c46de2..aef0eecf24d 100644 --- a/contrib/endpoints/src/api_manager/context/request_context.h +++ b/contrib/endpoints/src/api_manager/context/request_context.h @@ -182,8 +182,8 @@ class RequestContext { std::chrono::steady_clock::time_point last_report_time_; // The accumulated data sent till last intermediate report - int64_t last_request_bytes; - int64_t last_response_bytes; + int64_t last_request_bytes_; + int64_t last_response_bytes_; }; } // namespace context