Skip to content

Commit 0f17438

Browse files
nshestakovildar-khisambeev
authored andcommitted
Conditional logging of request body (#2520)
1 parent e2f14c4 commit 0f17438

File tree

3 files changed

+28
-46
lines changed

3 files changed

+28
-46
lines changed

ydb/core/client/server/grpc_server.cpp

+2-22
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
#include <util/string/join.h>
1313

14-
#include <google/protobuf/text_format.h>
15-
1614
#include <grpc++/resource_quota.h>
1715
#include <grpc++/security/server_credentials.h>
1816
#include <grpc++/server_builder.h>
@@ -266,15 +264,8 @@ class TSimpleRequest
266264
}
267265

268266
void Finish(const TOut& resp, ui32 status) {
269-
auto makeResponseString = [&] {
270-
TString x;
271-
google::protobuf::TextFormat::Printer printer;
272-
printer.SetSingleLineMode(true);
273-
printer.PrintToString(resp, &x);
274-
return x;
275-
};
276267
LOG_DEBUG(ActorSystem, NKikimrServices::GRPC_SERVER, "[%p] issuing response Name# %s data# %s peer# %s", this,
277-
Name, makeResponseString().data(), GetPeerName().c_str());
268+
Name, NYdbGrpc::FormatMessage(resp).data(), GetPeerName().c_str());
278269
ResponseSize = resp.ByteSize();
279270
ResponseStatus = status;
280271
StateFunc = &TSimpleRequest::FinishDone;
@@ -300,19 +291,8 @@ class TSimpleRequest
300291
bool RequestDone(bool ok) {
301292
OnAfterCall();
302293

303-
auto makeRequestString = [&] {
304-
TString resp;
305-
if (ok) {
306-
google::protobuf::TextFormat::Printer printer;
307-
printer.SetSingleLineMode(true);
308-
printer.PrintToString(Request, &resp);
309-
} else {
310-
resp = "<not ok>";
311-
}
312-
return resp;
313-
};
314294
LOG_DEBUG(ActorSystem, NKikimrServices::GRPC_SERVER, "[%p] received request Name# %s ok# %s data# %s peer# %s current inflight# %li", this,
315-
Name, ok ? "true" : "false", makeRequestString().data(), GetPeerName().c_str(), Server->GetCurrentInFlight());
295+
Name, ok ? "true" : "false", NYdbGrpc::FormatMessage(Request, ok).data(), GetPeerName().c_str(), Server->GetCurrentInFlight());
316296

317297
if (Context.c_call() == nullptr) {
318298
Y_ABORT_UNLESS(!ok);

ydb/core/grpc_streaming/grpc_streaming.h

+3-24
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <contrib/libs/grpc/include/grpcpp/support/async_stream.h>
1313
#include <contrib/libs/grpc/include/grpcpp/support/async_unary_call.h>
14-
#include <google/protobuf/text_format.h>
1514

1615
#include <atomic>
1716

@@ -347,22 +346,10 @@ class TGRpcStreamingRequest final
347346
}
348347

349348
void OnReadDone(NYdbGrpc::EQueueEventStatus status) {
350-
auto dumpResultText = [&] {
351-
TString text;
352-
if (status == NYdbGrpc::EQueueEventStatus::OK) {
353-
google::protobuf::TextFormat::Printer printer;
354-
printer.SetSingleLineMode(true);
355-
printer.PrintToString(ReadInProgress->Record, &text);
356-
} else {
357-
text = "<not ok>";
358-
}
359-
return text;
360-
};
361-
362349
LOG_DEBUG(ActorSystem, LoggerServiceId, "[%p] read finished Name# %s ok# %s data# %s peer# %s",
363350
this, Name,
364351
status == NYdbGrpc::EQueueEventStatus::OK ? "true" : "false",
365-
dumpResultText().c_str(),
352+
NYdbGrpc::FormatMessage(ReadInProgress->Record, status == NYdbGrpc::EQueueEventStatus::OK).c_str(),
366353
this->GetPeerName().c_str());
367354

368355
// Take current in-progress read first
@@ -400,25 +387,17 @@ class TGRpcStreamingRequest final
400387
}
401388

402389
bool Write(TOut&& message, const grpc::WriteOptions& options = { }, const grpc::Status* status = nullptr) {
403-
auto dumpMessageText = [&] {
404-
TString text;
405-
google::protobuf::TextFormat::Printer printer;
406-
printer.SetSingleLineMode(true);
407-
printer.PrintToString(message, &text);
408-
return text;
409-
};
410-
411390
if (status) {
412391
LOG_DEBUG(ActorSystem, LoggerServiceId, "[%p] facade write Name# %s data# %s peer# %s grpc status# (%d) message# %s",
413392
this, Name,
414-
dumpMessageText().c_str(),
393+
NYdbGrpc::FormatMessage(message).c_str(),
415394
this->GetPeerName().c_str(),
416395
static_cast<int>(status->error_code()),
417396
status->error_message().c_str());
418397
} else {
419398
LOG_DEBUG(ActorSystem, LoggerServiceId, "[%p] facade write Name# %s data# %s peer# %s",
420399
this, Name,
421-
dumpMessageText().c_str(),
400+
NYdbGrpc::FormatMessage(message).c_str(),
422401
this->GetPeerName().c_str());
423402
}
424403

ydb/library/grpc/server/logger.h

+23
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
#include <library/cpp/logger/priority.h>
44

55
#include <util/generic/ptr.h>
6+
#include <util/system/env.h>
7+
8+
#include <google/protobuf/text_format.h>
9+
610

711
namespace NYdbGrpc {
812

13+
static bool LogBodyEnabled = "BODY" == GetEnv("YDB_GRPC_SERVER_LOGGING");
14+
915
class TLogger: public TThrRefBase {
1016
protected:
1117
TLogger() = default;
@@ -40,4 +46,21 @@ using TLoggerPtr = TIntrusivePtr<TLogger>;
4046
logger->Write(ELogPriority::TLOG_INFO, format, __VA_ARGS__); \
4147
} else { }
4248

49+
50+
inline TString FormatMessage(const NProtoBuf::Message& message, bool ok = true) {
51+
if (ok) {
52+
if (LogBodyEnabled) {
53+
TString text;
54+
google::protobuf::TextFormat::Printer printer;
55+
printer.SetSingleLineMode(true);
56+
printer.PrintToString(message, &text);
57+
return text;
58+
} else {
59+
return "<hidden>";
60+
}
61+
} else {
62+
return "<not ok>";
63+
}
64+
}
65+
4366
} // namespace NYdbGrpc

0 commit comments

Comments
 (0)