Skip to content

Commit

Permalink
Add CMake gRPC support when OTLP is enabled (open-telemetry#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan authored Dec 13, 2020
1 parent 68e510c commit ec6b6c7
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 37 deletions.
14 changes: 10 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ endif()

if(WITH_PROTOBUF)
set(protobuf_MODULE_COMPATIBLE ON)
find_package(Protobuf REQUIRED)
if(NOT protobuf_FOUND)
if(WIN32)
find_package(Protobuf)
find_package(gRPC)
if((NOT protobuf_FOUND) OR (NOT gRPC_FOUND))
if(WIN32 AND (NOT DEFINED CMAKE_TOOLCHAIN_FILE))
install_windows_deps()
endif()
find_package(Protobuf REQUIRED)
if(NOT protobuf_FOUND)
find_package(Protobuf REQUIRED)
endif()
if(NOT gRPC_FOUND)
find_package(gRPC REQUIRED)
endif()
if(WIN32)
# Always use x64 protoc.exe
if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
Expand Down
147 changes: 118 additions & 29 deletions cmake/opentelemetry-proto.cmake
Original file line number Diff line number Diff line change
@@ -1,48 +1,137 @@
set(PROTO_PATH "${CMAKE_SOURCE_DIR}/third_party/opentelemetry-proto")

set(COMMON_PROTO "${PROTO_PATH}/opentelemetry/proto/common/v1/common.proto")
set(RESOURCE_PROTO "${PROTO_PATH}/opentelemetry/proto/resource/v1/resource.proto")
set(RESOURCE_PROTO
"${PROTO_PATH}/opentelemetry/proto/resource/v1/resource.proto")
set(TRACE_PROTO "${PROTO_PATH}/opentelemetry/proto/trace/v1/trace.proto")
set(LOGS_PROTO "${PROTO_PATH}/opentelemetry/proto/logs/v1/logs.proto")
set(METRICS_PROTO "${PROTO_PATH}/opentelemetry/proto/metrics/v1/metrics.proto")

set(GENERATED_PROTOBUF_PATH "${CMAKE_BINARY_DIR}/generated/third_party/opentelemetry-proto")
set(TRACE_SERVICE_PROTO
"${PROTO_PATH}/opentelemetry/proto/collector/trace/v1/trace_service.proto")
set(LOGS_SERVICE_PROTO
"${PROTO_PATH}/opentelemetry/proto/collector/logs/v1/logs_service.proto")
set(METRICS_SERVICE_PROTO
"${PROTO_PATH}/opentelemetry/proto/collector/metrics/v1/metrics_service.proto"
)

set(GENERATED_PROTOBUF_PATH
"${CMAKE_BINARY_DIR}/generated/third_party/opentelemetry-proto")

file(MAKE_DIRECTORY "${GENERATED_PROTOBUF_PATH}")

set(COMMON_PB_CPP_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/common/v1/common.pb.cc")
set(COMMON_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/common/v1/common.pb.h")
set(RESOURCE_PB_CPP_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/resource/v1/resource.pb.cc")
set(RESOURCE_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/resource/v1/resource.pb.h")
set(TRACE_PB_CPP_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/trace/v1/trace.pb.cc")
set(TRACE_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/trace/v1/trace.pb.h")
set(COMMON_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/common/v1/common.pb.cc")
set(COMMON_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/common/v1/common.pb.h")
set(RESOURCE_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/resource/v1/resource.pb.cc")
set(RESOURCE_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/resource/v1/resource.pb.h")
set(TRACE_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/trace/v1/trace.pb.cc")
set(TRACE_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/trace/v1/trace.pb.h")
set(LOGS_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/logs/v1/logs.pb.cc")
set(LOGS_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/logs/v1/logs.pb.h")
set(METRICS_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/metrics/v1/metrics.pb.cc")
set(METRICS_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/metrics/v1/metrics.pb.h")

set(TRACE_SERVICE_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/trace/v1/trace_service.pb.cc"
)
set(TRACE_SERVICE_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/trace/v1/trace_service.pb.h"
)
set(TRACE_SERVICE_GRPC_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.cc"
)
set(TRACE_SERVICE_GRPC_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h"
)
set(LOGS_SERVICE_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/logs/v1/logs_service.pb.cc"
)
set(LOGS_SERVICE_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/logs/v1/logs_service.pb.h"
)
set(LOGS_SERVICE_GRPC_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/logs/v1/logs_service.grpc.pb.cc"
)
set(LOGS_SERVICE_GRPC_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/logs/v1/logs_service.grpc.pb.h"
)
set(METRICS_SERVICE_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/metrics/v1/metrics_service.pb.cc"
)
set(METRICS_SERVICE_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/metrics/v1/metrics_service.pb.h"
)
set(METRICS_SERVICE_GRPC_PB_CPP_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/metrics/v1/metrics_service.grpc.pb.cc"
)
set(METRICS_SERVICE_GRPC_PB_H_FILE
"${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/metrics/v1/metrics_service.grpc.pb.h"
)

foreach(IMPORT_DIR ${PROTOBUF_IMPORT_DIRS})
list(APPEND PROTOBUF_INCLUDE_FLAGS "-I${IMPORT_DIR}")
endforeach()

get_target_property(gRPC_CPP_PLUGIN_EXECUTABLE gRPC::grpc_cpp_plugin
IMPORTED_LOCATION_RELEASE)

add_custom_command(
OUTPUT
${COMMON_PB_H_FILE}
${COMMON_PB_CPP_FILE}
${RESOURCE_PB_H_FILE}
${RESOURCE_PB_CPP_FILE}
${TRACE_PB_H_FILE}
${TRACE_PB_CPP_FILE}
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS
"--proto_path=${PROTO_PATH}"
${PROTOBUF_INCLUDE_FLAGS}
"--cpp_out=${GENERATED_PROTOBUF_PATH}"
${COMMON_PROTO}
${RESOURCE_PROTO}
${TRACE_PROTO}
)
OUTPUT ${COMMON_PB_H_FILE}
${COMMON_PB_CPP_FILE}
${RESOURCE_PB_H_FILE}
${RESOURCE_PB_CPP_FILE}
${TRACE_PB_H_FILE}
${TRACE_PB_CPP_FILE}
${LOGS_PB_H_FILE}
${LOGS_PB_CPP_FILE}
${METRICS_PB_H_FILE}
${METRICS_PB_CPP_FILE}
${TRACE_SERVICE_PB_H_FILE}
${TRACE_SERVICE_PB_CPP_FILE}
${TRACE_SERVICE_GRPC_PB_H_FILE}
${TRACE_SERVICE_GRPC_PB_CPP_FILE}
${LOGS_SERVICE_PB_H_FILE}
${LOGS_SERVICE_PB_CPP_FILE}
${LOGS_SERVICE_GRPC_PB_H_FILE}
${LOGS_SERVICE_GRPC_PB_CPP_FILE}
${METRICS_SERVICE_PB_H_FILE}
${METRICS_SERVICE_PB_CPP_FILE}
${METRICS_SERVICE_GRPC_PB_H_FILE}
${METRICS_SERVICE_GRPC_PB_CPP_FILE}
COMMAND
${PROTOBUF_PROTOC_EXECUTABLE} ARGS "--proto_path=${PROTO_PATH}"
${PROTOBUF_INCLUDE_FLAGS} "--cpp_out=${GENERATED_PROTOBUF_PATH}"
"--grpc_out=${GENERATED_PROTOBUF_PATH}"
--plugin=protoc-gen-grpc="${gRPC_CPP_PLUGIN_EXECUTABLE}" ${COMMON_PROTO}
${RESOURCE_PROTO} ${TRACE_PROTO} ${LOGS_PROTO} ${METRICS_PROTO}
${TRACE_SERVICE_PROTO} ${LOGS_SERVICE_PROTO} ${METRICS_SERVICE_PROTO})

include_directories("${GENERATED_PROTOBUF_PATH}")

add_library(opentelemetry_proto OBJECT
${COMMON_PB_CPP_FILE}
${RESOURCE_PB_CPP_FILE}
${TRACE_PB_CPP_FILE})
if (BUILD_SHARED_LIBS)
add_library(
opentelemetry_proto OBJECT
${COMMON_PB_CPP_FILE}
${RESOURCE_PB_CPP_FILE}
${TRACE_PB_CPP_FILE}
${LOGS_PB_CPP_FILE}
${METRICS_PB_CPP_FILE}
${TRACE_SERVICE_PB_CPP_FILE}
${TRACE_SERVICE_GRPC_PB_CPP_FILE}
${LOGS_SERVICE_PB_CPP_FILE}
${LOGS_SERVICE_GRPCPB_CPP_FILE}
${METRICS_SERVICE_PB_CPP_FILE}
${METRICS_SERVICE_GRPC_PB_CPP_FILE})

if(BUILD_SHARED_LIBS)
set_property(TARGET opentelemetry_proto PROPERTY POSITION_INDEPENDENT_CODE ON)
endif()
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
if(WITH_OTLP)
add_subdirectory(otlp)
endif()
add_subdirectory(plugin)
add_subdirectory(simple)
add_subdirectory(batch)
Expand Down
5 changes: 3 additions & 2 deletions examples/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ target_link_libraries(otlp_foo_library ${CMAKE_THREAD_LIBS_INIT}
opentelemetry_api)

add_executable(example_otlp main.cc)
target_link_libraries(example_otlp ${CMAKE_THREAD_LIBS_INIT} otlp_foo_library
opentelemetry_trace opentelemetry_exporter_otprotocol)
target_link_libraries(
example_otlp ${CMAKE_THREAD_LIBS_INIT} otlp_foo_library opentelemetry_trace
opentelemetry_exporter_otprotocol gRPC::grpc++)
3 changes: 2 additions & 1 deletion exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include_directories(include)

add_library(opentelemetry_exporter_otprotocol src/recordable.cc)
add_library(opentelemetry_exporter_otprotocol src/recordable.cc
src/otlp_exporter.cc)
target_link_libraries(opentelemetry_exporter_otprotocol opentelemetry_proto)

if(BUILD_TESTING)
Expand Down
3 changes: 2 additions & 1 deletion tools/setup-buildtools.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if ERRORLEVEL 0 (
)

REM Try to autodetect Visual Studio
call "%~dp0\vcvars.cmd"
call "%~dp0vcvars.cmd" x64
if "%TOOLS_VS_NOTFOUND%" == "1" (
REM Cannot detect MSBuild path
REM TODO: no command line tools..
Expand All @@ -51,5 +51,6 @@ vcpkg install ms-gsl:x64-windows
vcpkg install nlohmann-json:x64-windows
vcpkg install abseil:x64-windows
vcpkg install protobuf:x64-windows
vcpkg install gRPC:x64-windows
popd
exit /b 0

0 comments on commit ec6b6c7

Please sign in to comment.