aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikolay Shestakov <tesseract@ydb.tech>2024-03-07 13:33:21 +0500
committerGitHub <noreply@github.com>2024-03-07 13:33:21 +0500
commitdc06486dd445731c58acfddcbef8d24fdedb08e3 (patch)
tree99c6ddaddbb08a1a7cc0949b74b0f61a15cf3a57
parent3350515101957ad5718686837ef4f452d0cea5f4 (diff)
downloadydb-dc06486dd445731c58acfddcbef8d24fdedb08e3.tar.gz
Conditional logging of request body (#2520)
-rw-r--r--ydb/core/client/server/grpc_server.cpp24
-rw-r--r--ydb/core/grpc_streaming/grpc_streaming.h27
-rw-r--r--ydb/library/grpc/server/logger.h23
3 files changed, 28 insertions, 46 deletions
diff --git a/ydb/core/client/server/grpc_server.cpp b/ydb/core/client/server/grpc_server.cpp
index 95079d6bc7d..aab1569456b 100644
--- a/ydb/core/client/server/grpc_server.cpp
+++ b/ydb/core/client/server/grpc_server.cpp
@@ -11,8 +11,6 @@
#include <util/string/join.h>
-#include <google/protobuf/text_format.h>
-
#include <grpc++/resource_quota.h>
#include <grpc++/security/server_credentials.h>
#include <grpc++/server_builder.h>
@@ -266,15 +264,8 @@ private:
}
void Finish(const TOut& resp, ui32 status) {
- auto makeResponseString = [&] {
- TString x;
- google::protobuf::TextFormat::Printer printer;
- printer.SetSingleLineMode(true);
- printer.PrintToString(resp, &x);
- return x;
- };
LOG_DEBUG(ActorSystem, NKikimrServices::GRPC_SERVER, "[%p] issuing response Name# %s data# %s peer# %s", this,
- Name, makeResponseString().data(), GetPeerName().c_str());
+ Name, NYdbGrpc::FormatMessage(resp).data(), GetPeerName().c_str());
ResponseSize = resp.ByteSize();
ResponseStatus = status;
StateFunc = &TSimpleRequest::FinishDone;
@@ -300,19 +291,8 @@ private:
bool RequestDone(bool ok) {
OnAfterCall();
- auto makeRequestString = [&] {
- TString resp;
- if (ok) {
- google::protobuf::TextFormat::Printer printer;
- printer.SetSingleLineMode(true);
- printer.PrintToString(Request, &resp);
- } else {
- resp = "<not ok>";
- }
- return resp;
- };
LOG_DEBUG(ActorSystem, NKikimrServices::GRPC_SERVER, "[%p] received request Name# %s ok# %s data# %s peer# %s current inflight# %li", this,
- Name, ok ? "true" : "false", makeRequestString().data(), GetPeerName().c_str(), Server->GetCurrentInFlight());
+ Name, ok ? "true" : "false", NYdbGrpc::FormatMessage(Request, ok).data(), GetPeerName().c_str(), Server->GetCurrentInFlight());
if (Context.c_call() == nullptr) {
Y_ABORT_UNLESS(!ok);
diff --git a/ydb/core/grpc_streaming/grpc_streaming.h b/ydb/core/grpc_streaming/grpc_streaming.h
index 872ab75bda7..25b3a313188 100644
--- a/ydb/core/grpc_streaming/grpc_streaming.h
+++ b/ydb/core/grpc_streaming/grpc_streaming.h
@@ -11,7 +11,6 @@
#include <contrib/libs/grpc/include/grpcpp/support/async_stream.h>
#include <contrib/libs/grpc/include/grpcpp/support/async_unary_call.h>
-#include <google/protobuf/text_format.h>
#include <atomic>
@@ -347,22 +346,10 @@ private:
}
void OnReadDone(NYdbGrpc::EQueueEventStatus status) {
- auto dumpResultText = [&] {
- TString text;
- if (status == NYdbGrpc::EQueueEventStatus::OK) {
- google::protobuf::TextFormat::Printer printer;
- printer.SetSingleLineMode(true);
- printer.PrintToString(ReadInProgress->Record, &text);
- } else {
- text = "<not ok>";
- }
- return text;
- };
-
LOG_DEBUG(ActorSystem, LoggerServiceId, "[%p] read finished Name# %s ok# %s data# %s peer# %s",
this, Name,
status == NYdbGrpc::EQueueEventStatus::OK ? "true" : "false",
- dumpResultText().c_str(),
+ NYdbGrpc::FormatMessage(ReadInProgress->Record, status == NYdbGrpc::EQueueEventStatus::OK).c_str(),
this->GetPeerName().c_str());
// Take current in-progress read first
@@ -400,25 +387,17 @@ private:
}
bool Write(TOut&& message, const grpc::WriteOptions& options = { }, const grpc::Status* status = nullptr) {
- auto dumpMessageText = [&] {
- TString text;
- google::protobuf::TextFormat::Printer printer;
- printer.SetSingleLineMode(true);
- printer.PrintToString(message, &text);
- return text;
- };
-
if (status) {
LOG_DEBUG(ActorSystem, LoggerServiceId, "[%p] facade write Name# %s data# %s peer# %s grpc status# (%d) message# %s",
this, Name,
- dumpMessageText().c_str(),
+ NYdbGrpc::FormatMessage(message).c_str(),
this->GetPeerName().c_str(),
static_cast<int>(status->error_code()),
status->error_message().c_str());
} else {
LOG_DEBUG(ActorSystem, LoggerServiceId, "[%p] facade write Name# %s data# %s peer# %s",
this, Name,
- dumpMessageText().c_str(),
+ NYdbGrpc::FormatMessage(message).c_str(),
this->GetPeerName().c_str());
}
diff --git a/ydb/library/grpc/server/logger.h b/ydb/library/grpc/server/logger.h
index 1460afa0963..d2ff80fc71f 100644
--- a/ydb/library/grpc/server/logger.h
+++ b/ydb/library/grpc/server/logger.h
@@ -3,9 +3,15 @@
#include <library/cpp/logger/priority.h>
#include <util/generic/ptr.h>
+#include <util/system/env.h>
+
+#include <google/protobuf/text_format.h>
+
namespace NYdbGrpc {
+static bool LogBodyEnabled = "BODY" == GetEnv("YDB_GRPC_SERVER_LOGGING");
+
class TLogger: public TThrRefBase {
protected:
TLogger() = default;
@@ -40,4 +46,21 @@ using TLoggerPtr = TIntrusivePtr<TLogger>;
logger->Write(ELogPriority::TLOG_INFO, format, __VA_ARGS__); \
} else { }
+
+inline TString FormatMessage(const NProtoBuf::Message& message, bool ok = true) {
+ if (ok) {
+ if (LogBodyEnabled) {
+ TString text;
+ google::protobuf::TextFormat::Printer printer;
+ printer.SetSingleLineMode(true);
+ printer.PrintToString(message, &text);
+ return text;
+ } else {
+ return "<hidden>";
+ }
+ } else {
+ return "<not ok>";
+ }
+}
+
} // namespace NYdbGrpc