Skip to content

Commit

Permalink
Use Abseil logging for gRPC v1.65.0 and above.
Browse files Browse the repository at this point in the history
Original logging method is now deprecated and results in error message
on Nginx startup.
  • Loading branch information
p-pautov committed Jul 23, 2024
1 parent 4c24716 commit 4c841c1
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/grpc_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include "grpc_log.hpp"

#include <grpc/support/log.h>
#include <google/protobuf/stubs/common.h>
#include <grpcpp/grpcpp.h>

#if GOOGLE_PROTOBUF_VERSION < 4022000

Expand Down Expand Up @@ -36,17 +36,17 @@ class ProtobufLog {
#include <absl/log/initialize.h>
#include <absl/log/log_sink_registry.h>

class ProtobufLog : absl::LogSink {
class NgxLogSink : absl::LogSink {
public:
ProtobufLog()
NgxLogSink()
{
absl::InitializeLog();
absl::AddLogSink(this);
// Disable logging to stderr
absl::SetStderrThreshold(static_cast<absl::LogSeverity>(100));
}

~ProtobufLog() override { absl::RemoveLogSink(this); }
~NgxLogSink() override { absl::RemoveLogSink(this); }

void Send(const absl::LogEntry& entry) override
{
Expand All @@ -61,12 +61,19 @@ class ProtobufLog : absl::LogSink {
ngx_str_t message { entry.text_message().size(),
(u_char*)entry.text_message().data() };

ngx_log_error(level, ngx_cycle->log, 0, "OTel/protobuf: %V", &message);
ngx_log_error(level, ngx_cycle->log, 0, "OTel/grpc: %V", &message);
}
};

typedef NgxLogSink ProtobufLog;

#endif

#if (GRPC_CPP_VERSION_MAJOR < 1) || \
(GRPC_CPP_VERSION_MAJOR == 1 && GRPC_CPP_VERSION_MINOR < 65)

#include <grpc/support/log.h>

class GrpcLog {
public:
GrpcLog() { gpr_set_log_function(grpcLogHandler); }
Expand All @@ -87,6 +94,13 @@ class GrpcLog {
ProtobufLog protoLog;
};

#else

// newer gRPC implies newer protobuf, and both use Abseil for logging
typedef NgxLogSink GrpcLog;

#endif

void initGrpcLog()
{
static GrpcLog init;
Expand Down

0 comments on commit 4c841c1

Please sign in to comment.