Move gRPC/Protobuf logs handling to a dedicated file.
This commit is contained in:
parent
aac5678def
commit
b54c65005a
5 changed files with 66 additions and 31 deletions
|
|
@ -104,6 +104,7 @@ add_compile_options(-Wall -Wtype-limits -Werror)
|
||||||
|
|
||||||
add_library(ngx_otel_module MODULE
|
add_library(ngx_otel_module MODULE
|
||||||
src/http_module.cpp
|
src/http_module.cpp
|
||||||
|
src/grpc_log.cpp
|
||||||
src/modules.c
|
src/modules.c
|
||||||
${PROTO_SOURCES})
|
${PROTO_SOURCES})
|
||||||
|
|
||||||
|
|
|
||||||
52
src/grpc_log.cpp
Normal file
52
src/grpc_log.cpp
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
#include "ngx.hpp"
|
||||||
|
|
||||||
|
#include "grpc_log.hpp"
|
||||||
|
|
||||||
|
#include <grpc/support/log.h>
|
||||||
|
#include <google/protobuf/stubs/logging.h>
|
||||||
|
|
||||||
|
class ProtobufLog {
|
||||||
|
public:
|
||||||
|
ProtobufLog() { google::protobuf::SetLogHandler(protobufLogHandler); }
|
||||||
|
~ProtobufLog() { google::protobuf::SetLogHandler(NULL); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void protobufLogHandler(google::protobuf::LogLevel logLevel,
|
||||||
|
const char* filename, int line, const std::string& msg)
|
||||||
|
{
|
||||||
|
using namespace google::protobuf;
|
||||||
|
|
||||||
|
ngx_uint_t level = logLevel == LOGLEVEL_FATAL ? NGX_LOG_EMERG :
|
||||||
|
logLevel == LOGLEVEL_ERROR ? NGX_LOG_ERR :
|
||||||
|
logLevel == LOGLEVEL_WARNING ? NGX_LOG_WARN :
|
||||||
|
/*LOGLEVEL_INFO*/ NGX_LOG_INFO;
|
||||||
|
|
||||||
|
ngx_log_error(level, ngx_cycle->log, 0, "OTel/protobuf: %s",
|
||||||
|
msg.c_str());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class GrpcLog {
|
||||||
|
public:
|
||||||
|
GrpcLog() { gpr_set_log_function(grpcLogHandler); }
|
||||||
|
~GrpcLog() { gpr_set_log_function(NULL); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void grpcLogHandler(gpr_log_func_args* args)
|
||||||
|
{
|
||||||
|
ngx_uint_t level =
|
||||||
|
args->severity == GPR_LOG_SEVERITY_ERROR ? NGX_LOG_ERR :
|
||||||
|
args->severity == GPR_LOG_SEVERITY_INFO ? NGX_LOG_INFO :
|
||||||
|
/*GPR_LOG_SEVERITY_DEBUG*/ NGX_LOG_DEBUG;
|
||||||
|
|
||||||
|
ngx_log_error(level, ngx_cycle->log, 0, "OTel/grpc: %s",
|
||||||
|
args->message);
|
||||||
|
}
|
||||||
|
|
||||||
|
ProtobufLog protoLog;
|
||||||
|
};
|
||||||
|
|
||||||
|
void initGrpcLog()
|
||||||
|
{
|
||||||
|
static GrpcLog init;
|
||||||
|
}
|
||||||
3
src/grpc_log.hpp
Normal file
3
src/grpc_log.hpp
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
void initGrpcLog();
|
||||||
|
|
@ -1,11 +1,6 @@
|
||||||
extern "C" {
|
#include "ngx.hpp"
|
||||||
#include <ngx_config.h>
|
|
||||||
#include <ngx_core.h>
|
|
||||||
#include <ngx_http.h>
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <grpc/support/log.h>
|
#include "grpc_log.hpp"
|
||||||
#include <google/protobuf/stubs/logging.h>
|
|
||||||
|
|
||||||
#include "str_view.hpp"
|
#include "str_view.hpp"
|
||||||
#include "trace_context.hpp"
|
#include "trace_context.hpp"
|
||||||
|
|
@ -504,28 +499,6 @@ ngx_int_t onRequestEnd(ngx_http_request_t* r)
|
||||||
return NGX_DECLINED;
|
return NGX_DECLINED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void grpcLogHandler(gpr_log_func_args* args)
|
|
||||||
{
|
|
||||||
ngx_uint_t level = args->severity == GPR_LOG_SEVERITY_ERROR ? NGX_LOG_ERR :
|
|
||||||
args->severity == GPR_LOG_SEVERITY_INFO ? NGX_LOG_INFO :
|
|
||||||
/*GPR_LOG_SEVERITY_DEBUG*/ NGX_LOG_DEBUG;
|
|
||||||
|
|
||||||
ngx_log_error(level, ngx_cycle->log, 0, "OTel/grpc: %s", args->message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void protobufLogHandler(google::protobuf::LogLevel logLevel,
|
|
||||||
const char* filename, int line, const std::string& msg)
|
|
||||||
{
|
|
||||||
using namespace google::protobuf;
|
|
||||||
|
|
||||||
ngx_uint_t level = logLevel == LOGLEVEL_FATAL ? NGX_LOG_EMERG :
|
|
||||||
logLevel == LOGLEVEL_ERROR ? NGX_LOG_ERR :
|
|
||||||
logLevel == LOGLEVEL_WARNING ? NGX_LOG_WARN :
|
|
||||||
/*LOGLEVEL_INFO*/ NGX_LOG_INFO;
|
|
||||||
|
|
||||||
ngx_log_error(level, ngx_cycle->log, 0, "OTel/protobuf: %s", msg.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
ngx_int_t initModule(ngx_conf_t* cf)
|
ngx_int_t initModule(ngx_conf_t* cf)
|
||||||
{
|
{
|
||||||
auto cmcf = (ngx_http_core_main_conf_t*)ngx_http_conf_get_module_main_conf(
|
auto cmcf = (ngx_http_core_main_conf_t*)ngx_http_conf_get_module_main_conf(
|
||||||
|
|
@ -547,8 +520,7 @@ ngx_int_t initModule(ngx_conf_t* cf)
|
||||||
|
|
||||||
*h = onRequestEnd;
|
*h = onRequestEnd;
|
||||||
|
|
||||||
gpr_set_log_function(grpcLogHandler);
|
initGrpcLog();
|
||||||
google::protobuf::SetLogHandler(protobufLogHandler);
|
|
||||||
|
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
7
src/ngx.hpp
Normal file
7
src/ngx.hpp
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include <ngx_config.h>
|
||||||
|
#include <ngx_core.h>
|
||||||
|
#include <ngx_http.h>
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue