Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with gRPC v1.65.0 and some build system enhancements #58

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 39 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ set(NGX_OTEL_NGINX_BUILD_DIR ""
set(NGX_OTEL_NGINX_DIR "${NGX_OTEL_NGINX_BUILD_DIR}/.."
CACHE PATH "Nginx source dir")

set(NGX_OTEL_FETCH_DEPS ON CACHE BOOL "Download dependencies")
set(NGX_OTEL_GRPC e241f37befe7ba4688effd84bfbf99b0f681a2f7 # v1.49.4
CACHE STRING "gRPC tag to download or 'package' to use preinstalled")
set(NGX_OTEL_SDK 11d5d9e0d8fd8ba876c8994714cc2647479b6574 # v1.11.0
CACHE STRING "OTel SDK tag to download or 'package' to use preinstalled")
set(NGX_OTEL_PROTO_DIR "" CACHE PATH "OTel proto files root")
set(NGX_OTEL_DEV OFF CACHE BOOL "Enforce compiler warnings")

Expand All @@ -16,26 +19,52 @@ endif()

set(CMAKE_CXX_VISIBILITY_PRESET hidden)

if(NGX_OTEL_FETCH_DEPS)
if(NGX_OTEL_GRPC STREQUAL "package")
find_package(protobuf REQUIRED)
find_package(gRPC REQUIRED)
else()
include(FetchContent)

FetchContent_Declare(
grpc
GIT_REPOSITORY https://github.com/grpc/grpc
GIT_TAG e241f37befe7ba4688effd84bfbf99b0f681a2f7 # v1.49.4
GIT_TAG ${NGX_OTEL_GRPC}
GIT_SUBMODULES third_party/protobuf third_party/abseil-cpp third_party/re2
GIT_SHALLOW ON)

set(gRPC_USE_PROTO_LITE ON CACHE INTERNAL "")
set(gRPC_INSTALL OFF CACHE INTERNAL "")
set(gRPC_USE_SYSTEMD OFF CACHE INTERNAL "")
set(gRPC_DOWNLOAD_ARCHIVES OFF CACHE INTERNAL "")
set(gRPC_CARES_PROVIDER package CACHE INTERNAL "")
set(gRPC_SSL_PROVIDER package CACHE INTERNAL "")
set(gRPC_ZLIB_PROVIDER package CACHE INTERNAL "")

set(protobuf_INSTALL OFF CACHE INTERNAL "")

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

FetchContent_MakeAvailable(grpc)

# reconsider once https://github.com/grpc/grpc/issues/36023 is done
target_compile_definitions(grpc PRIVATE GRPC_NO_XDS GRPC_NO_RLS)

set_property(DIRECTORY ${grpc_SOURCE_DIR}
PROPERTY EXCLUDE_FROM_ALL YES)

add_library(gRPC::grpc++ ALIAS grpc++)
add_executable(gRPC::grpc_cpp_plugin ALIAS grpc_cpp_plugin)
endif()

if(NGX_OTEL_SDK STREQUAL "package")
find_package(opentelemetry-cpp REQUIRED)
else()
include(FetchContent)

FetchContent_Declare(
otelcpp
GIT_REPOSITORY https://github.com/open-telemetry/opentelemetry-cpp
GIT_TAG 11d5d9e0d8fd8ba876c8994714cc2647479b6574 # v1.11.0
GIT_TAG ${NGX_OTEL_SDK}
GIT_SUBMODULES third_party/opentelemetry-proto
GIT_SHALLOW ON)

Expand All @@ -45,13 +74,8 @@ if(NGX_OTEL_FETCH_DEPS)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)

FetchContent_MakeAvailable(grpc otelcpp)
FetchContent_MakeAvailable(otelcpp)

# reconsider once https://github.com/grpc/grpc/issues/36023 is done
target_compile_definitions(grpc PRIVATE GRPC_NO_XDS GRPC_NO_RLS)

set_property(DIRECTORY ${grpc_SOURCE_DIR}
PROPERTY EXCLUDE_FROM_ALL YES)
set_property(DIRECTORY ${otelcpp_SOURCE_DIR}
PROPERTY EXCLUDE_FROM_ALL YES)

Expand All @@ -61,12 +85,6 @@ if(NGX_OTEL_FETCH_DEPS)
endif()

add_library(opentelemetry-cpp::trace ALIAS opentelemetry_trace)
add_library(gRPC::grpc++ ALIAS grpc++)
add_executable(gRPC::grpc_cpp_plugin ALIAS grpc_cpp_plugin)
else()
find_package(opentelemetry-cpp REQUIRED)
find_package(protobuf REQUIRED)
find_package(gRPC REQUIRED)
endif()

set(PROTO_DIR ${NGX_OTEL_PROTO_DIR})
Expand Down Expand Up @@ -94,16 +112,16 @@ add_custom_command(
--plugin protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>
${PROTOS}
# remove inconsequential UTF8 check during serialization to aid performance
COMMAND sed -i.bak
-e [[/ ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String(/,/);/d]]
COMMAND sed -i.bak -E
-e [[/ ::(PROTOBUF_NAMESPACE_ID|google::protobuf)::internal::WireFormatLite::VerifyUtf8String\(/,/\);/d]]
${PROTO_SOURCES}
DEPENDS ${PROTOS} protobuf::protoc gRPC::grpc_cpp_plugin
VERBATIM)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)

if (NGX_OTEL_DEV)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)

add_compile_options(-Wall -Wtype-limits -Werror)
endif()

Expand Down
3 changes: 1 addition & 2 deletions config
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
ngx_addon_name=ngx_otel_module

cmake -D NGX_OTEL_NGINX_BUILD_DIR=$NGX_OBJS \
-D NGX_OTEL_FETCH_DEPS=OFF \
-D NGX_OTEL_PROTO_DIR=$NGX_OTEL_PROTO_DIR \
-D CMAKE_LIBRARY_OUTPUT_DIRECTORY=$PWD/$NGX_OBJS \
-D "CMAKE_C_FLAGS=$NGX_CC_OPT" \
-D "CMAKE_CXX_FLAGS=$NGX_CC_OPT" \
-D "CMAKE_MODULE_LINKER_FLAGS=$NGX_LD_OPT" \
$NGX_OTEL_CMAKE_OPTS \
-S $ngx_addon_dir -B $NGX_OBJS/otel || exit 1
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