1414
1515#include " tensorstore/internal/grpc/logging_interceptor.h"
1616
17+ #include < map>
18+ #include < string>
1719#include < string_view>
1820
1921#include " absl/base/attributes.h"
2022#include " absl/log/absl_log.h"
23+ #include " absl/strings/str_cat.h"
2124#include " grpcpp/support/client_interceptor.h" // third_party
2225#include " grpcpp/support/interceptor.h" // third_party
2326#include " google/protobuf/message.h"
@@ -34,7 +37,7 @@ namespace tensorstore {
3437namespace internal_grpc {
3538namespace {
3639
37- ABSL_CONST_INIT internal_log::VerboseFlag grpc_logging (" grpc_channel" );
40+ ABSL_CONST_INIT internal_log::VerboseFlag verbose_logging (" grpc_channel" );
3841
3942// Implements detailed logging for gRPC calls to GCS.
4043class LoggingInterceptor : public Interceptor {
@@ -44,46 +47,69 @@ class LoggingInterceptor : public Interceptor {
4447 std::string_view method_name () const { return info_->method (); }
4548
4649 void Intercept (InterceptorBatchMethods* methods) override {
47- if (!grpc_logging ) {
50+ if (!verbose_logging ) {
4851 methods->Proceed ();
4952 return ;
5053 }
54+
55+ std::string log;
56+
57+ std::multimap<std::string, std::string>* metadata = nullptr ;
58+ if (methods->QueryInterceptionHookPoint (
59+ InterceptionHookPoints::PRE_SEND_INITIAL_METADATA)) {
60+ metadata = methods->GetSendInitialMetadata ();
61+ }
62+ if (methods->QueryInterceptionHookPoint (
63+ InterceptionHookPoints::PRE_SEND_STATUS)) {
64+ metadata = methods->GetSendTrailingMetadata ();
65+ }
66+ if (metadata != nullptr ) {
67+ absl::StrAppend (&log, " Metadata:" );
68+ for (const auto & [key, value] : *metadata) {
69+ absl::StrAppend (&log, " " , key, " : " , value);
70+ }
71+ }
72+
5173 if (methods->QueryInterceptionHookPoint (
5274 InterceptionHookPoints::PRE_SEND_MESSAGE)) {
5375 auto * message =
5476 static_cast <const google::protobuf::Message*>(methods->GetSendMessage ());
55- bool is_first_message = !started_;
56- started_ = true ;
57- if (grpc_logging.Level (2 )) {
58- ABSL_LOG (INFO) << " Begin: " << method_name () << " "
59- << ConciseDebugString (*message);
60- } else if (is_first_message) {
61- ABSL_LOG (INFO) << " Begin: " << method_name ();
77+ if (verbose_logging.Level (2 ) && message != nullptr ) {
78+ absl::StrAppend (&log, log.empty () ? " " : " \n " ,
79+ " Request: " , ConciseDebugString (*message));
6280 }
6381 }
64- if (grpc_logging.Level (2 ) &&
65- methods->QueryInterceptionHookPoint (
82+ if (methods->QueryInterceptionHookPoint (
6683 InterceptionHookPoints::POST_RECV_MESSAGE)) {
67- ABSL_LOG (INFO) << method_name () << " "
68- << ConciseDebugString (*static_cast <const google::protobuf::Message*>(
69- methods->GetRecvMessage ()));
84+ auto * message =
85+ static_cast <const google::protobuf::Message*>(methods->GetRecvMessage ());
86+ if (verbose_logging.Level (2 ) && message != nullptr ) {
87+ absl::StrAppend (&log, log.empty () ? " " : " \n " ,
88+ " Response: " , ConciseDebugString (*message));
89+ }
90+ }
91+
92+ if (methods->QueryInterceptionHookPoint (
93+ InterceptionHookPoints::PRE_SEND_STATUS)) {
94+ if (auto status = methods->GetSendStatus (); !status.ok ()) {
95+ absl::StrAppend (&log, log.empty () ? " " : " \n " , " Send Status: " ,
96+ internal::GrpcStatusToAbslStatus (status));
97+ }
7098 }
99+
71100 if (methods->QueryInterceptionHookPoint (
72101 InterceptionHookPoints::POST_RECV_STATUS)) {
73- if (auto * status = methods->GetRecvStatus ();
74- status != nullptr && !status->ok ()) {
75- ABSL_LOG (INFO) << " Error: " << method_name () << " "
76- << internal::GrpcStatusToAbslStatus (*status);
77- } else {
78- ABSL_LOG (INFO) << " End: " << method_name ();
102+ if (auto * status = methods->GetRecvStatus (); status != nullptr ) {
103+ absl::StrAppend (&log, log.empty () ? " " : " \n " , " Recv Status: " ,
104+ internal::GrpcStatusToAbslStatus (*status));
79105 }
80106 }
107+ ABSL_LOG_IF (INFO, !log.empty ()) << method_name () << " " << log;
81108 methods->Proceed ();
82109 }
83110
84111 private:
85112 const ClientRpcInfo* info_;
86- bool started_ = false ;
87113};
88114
89115} // namespace
0 commit comments