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

Build OpenTelemetry C++ SDK and exporter into DLL #1932

Merged
merged 53 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
dbd818d
Initial changeset
ThomsonTan Jan 13, 2023
468c189
Build logs into DLL
ThomsonTan Jan 19, 2023
011d96e
Merge branch 'main' into Build_DLL
ThomsonTan Jan 19, 2023
435e88b
Revert changing library to object
ThomsonTan Jan 19, 2023
27d8a5c
Add TracerProviderFactory to export
ThomsonTan Jan 19, 2023
52ab127
Fix clang-format
ThomsonTan Jan 19, 2023
db962d0
Rename OPENTELEMETRY_API to OPENTELEMETRY_EXPORT
ThomsonTan Jan 19, 2023
3178916
Add preprocess on the DLL DEF file
ThomsonTan Jan 20, 2023
01c8dd2
Disable clang format for export def file
ThomsonTan Jan 21, 2023
1572f17
Fix more format
ThomsonTan Jan 21, 2023
f8ee55a
Change line format for clang-format
ThomsonTan Jan 21, 2023
16b2011
Merge branch 'main' into Build_DLL
ThomsonTan Jan 24, 2023
d61520d
Export more classes from SDK
ThomsonTan Jan 24, 2023
1ad93bd
Add default_delete for std::unique_ptr exposed to users
ThomsonTan Jan 25, 2023
0af8403
Fix cmake format
ThomsonTan Jan 25, 2023
aa09118
More cmake-format
ThomsonTan Jan 25, 2023
767a4f1
Change newline to unix style in markdown
ThomsonTan Jan 25, 2023
d14227f
Add DLL build to CI
ThomsonTan Jan 25, 2023
9295d6b
Honor OTLP build option from CMake
ThomsonTan Jan 25, 2023
86bb52c
Add deleter for GetLogger which returns nostd::shared_ptr
ThomsonTan Jan 27, 2023
321359d
More format
ThomsonTan Jan 27, 2023
e79a554
Run cmake-format
ThomsonTan Jan 27, 2023
5c3c5e5
Export OTLP symbols based on build flags
ThomsonTan Jan 27, 2023
1c54eb6
Merge branch 'main' into Build_DLL
ThomsonTan Jan 27, 2023
1fc1467
Address feedback about creating temprory unique_ptr
ThomsonTan Jan 30, 2023
eebe85b
Merge branch 'main' into Build_DLL
ThomsonTan Jan 30, 2023
6cda6e5
Add comment with undecorated symbols for exported decorated symbols f…
ThomsonTan Jan 30, 2023
44ca55a
Remove SystemTimestamp from export list
ThomsonTan Jan 31, 2023
91bfd07
Fix cmake format
ThomsonTan Jan 31, 2023
20cceb7
Update doc to trigger new build
ThomsonTan Jan 31, 2023
fcb7f22
Emit error when WITH_STL is enabled for DLL build
ThomsonTan Jan 31, 2023
d1cab2c
Merge branch 'main' into Build_DLL
ThomsonTan Jan 31, 2023
7f1cfd7
Merge branch 'main' into Build_DLL
ThomsonTan Jan 31, 2023
b3c4029
Update message
ThomsonTan Feb 2, 2023
1a8d6eb
Run example with opentelemetry DLL under sanitizer
ThomsonTan Feb 3, 2023
bcc3909
Fix CI task name
ThomsonTan Feb 3, 2023
86ea207
Fix the sanitize task name
ThomsonTan Feb 3, 2023
4b1c991
Remove default_delete and address sanitizer
ThomsonTan Feb 4, 2023
9312aef
Revert experiment change to macros.h
ThomsonTan Feb 4, 2023
2260a1b
Merge branch 'main' into Build_DLL
ThomsonTan Feb 4, 2023
8bd4fab
Remove extra end curly brace
ThomsonTan Feb 4, 2023
36e1ece
Update export symbol name for LoggerProvider::Create
ThomsonTan Feb 4, 2023
e98815f
Format cmake
ThomsonTan Feb 4, 2023
594f289
Run example to catch runtime error like memory allocation inconsistency
ThomsonTan Feb 4, 2023
ecddf7c
Add DLL path to environment for loading
ThomsonTan Feb 4, 2023
f6ab0fc
Merge branch 'main' into Build_DLL
ThomsonTan Feb 6, 2023
059e2e6
Merge branch 'main' into Build_DLL
ThomsonTan Feb 7, 2023
c23051f
Move changelog to unrelease section
ThomsonTan Feb 7, 2023
bacc3b6
Address feedback on missing copyright
ThomsonTan Feb 8, 2023
e4965a9
Merge branch 'main' into Build_DLL
ThomsonTan Feb 8, 2023
0b858a6
Add missing export
ThomsonTan Feb 10, 2023
5ecbf16
Merge branch 'main' into Build_DLL
ThomsonTan Feb 10, 2023
bca6227
Merge branch 'main' into Build_DLL
ThomsonTan Feb 11, 2023
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
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,23 @@ jobs:
- name: run otprotocol test
run: ./ci/do_ci.ps1 cmake.exporter.otprotocol.test

windows-build-dll:
name: CMake -> exporter proto (Build as DLL)
runs-on: windows-2019
steps:
- uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: setup
run: |
./ci/setup_windows_cmake.ps1
./ci/setup_windows_ci_environment.ps1
./ci/install_windows_protobuf.ps1
- name: run cmake test (DLL build)
run: ./ci/do_ci.ps1 cmake.dll.test
- name: run otprotocol test (DLL build)
run: ./ci/do_ci.ps1 cmake.exporter.otprotocol.dll.test
marcalff marked this conversation as resolved.
Show resolved Hide resolved

windows_with_async_export:
name: CMake (With async export) -> exporter proto
runs-on: windows-2019
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*.so
*.dylib
*.dll
*.pdb

# Fortran module files
*.mod
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Increment the:

* Convert Prometheus Exporter to Pull MetricReader [#1953](https://github.com/open-telemetry/opentelemetry-cpp/pull/1953)
* Upgrade prometheus-cpp to v1.1.0 [#1954](https://github.com/open-telemetry/opentelemetry-cpp/pull/1954)
* [BUILD] Build OpenTelemetry SDK and exporters into DLL
[#1932](https://github.com/open-telemetry/opentelemetry-cpp/pull/1932)

## [1.8.2] 2023-01-31

Expand Down
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,17 @@ endif()

include(CMakePackageConfigHelpers)

if(DEFINED OPENTELEMETRY_BUILD_DLL)
owent marked this conversation as resolved.
Show resolved Hide resolved
if(NOT MSVC)
message(WARNING "Build DLL is supposed to work with MSVC!")
endif()
if(WITH_STL)
message(
WARNING "Build DLL with C++ STL could cause binary incompatibility!")
endif()
add_definitions(-DOPENTELEMETRY_BUILD_EXPORT_DLL)
marcalff marked this conversation as resolved.
Show resolved Hide resolved
endif()

include_directories(api/include)

add_subdirectory(api)
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/baggage/baggage.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ OPENTELEMETRY_BEGIN_NAMESPACE
namespace baggage
{

class Baggage
class OPENTELEMETRY_EXPORT Baggage
{
public:
static constexpr size_t kMaxKeyValuePairs = 180;
Expand Down
25 changes: 25 additions & 0 deletions api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,28 @@ point.
# define OPENTELEMETRY_API_SINGLETON

#endif

/* clang-format on */
//
// The if/elif order matters here. If both OPENTELEMETRY_BUILD_IMPORT_DLL and
// OPENTELEMETRY_BUILD_EXPORT_DLL are defined, the former takes precedence.
marcalff marked this conversation as resolved.
Show resolved Hide resolved
//
// TODO: consider define OPENTELEMETRY_EXPORT for cygwin/gcc, see below link.
// https://gcc.gnu.org/wiki/Visibility#How_to_use_the_new_C.2B-.2B-_visibility_support
//
#if defined(_MSC_VER) && defined(OPENTELEMETRY_BUILD_IMPORT_DLL)
ThomsonTan marked this conversation as resolved.
Show resolved Hide resolved
owent marked this conversation as resolved.
Show resolved Hide resolved

# define OPENTELEMETRY_EXPORT __declspec(dllimport)

#elif defined(_MSC_VER) && defined(OPENTELEMETRY_BUILD_EXPORT_DLL)

# define OPENTELEMETRY_EXPORT __declspec(dllexport)

#else

//
// build OpenTelemetry as static library or not on Windows.
//
# define OPENTELEMETRY_EXPORT

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace propagation

/* Stores the singleton TextMapPropagator */

class GlobalTextMapPropagator
class OPENTELEMETRY_EXPORT GlobalTextMapPropagator
{
public:
static nostd::shared_ptr<TextMapPropagator> GetGlobalPropagator() noexcept
Expand Down
4 changes: 2 additions & 2 deletions api/include/opentelemetry/context/runtime_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Token
* this class and passing an initialized RuntimeContextStorage object to
* RuntimeContext::SetRuntimeContextStorage.
*/
class RuntimeContextStorage
class OPENTELEMETRY_EXPORT RuntimeContextStorage
{
public:
/**
Expand Down Expand Up @@ -78,7 +78,7 @@ static RuntimeContextStorage *GetDefaultStorage() noexcept;
// Provides a wrapper for propagating the context object globally.
//
// By default, a thread-local runtime context storage is used.
class RuntimeContext
class OPENTELEMETRY_EXPORT RuntimeContext
{
public:
// Return the current context.
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/logs/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,4 @@ class Logger
} // namespace logs
OPENTELEMETRY_END_NAMESPACE

#endif
#endif // end of ENABLE_LOGS_PREVIEW
4 changes: 3 additions & 1 deletion api/include/opentelemetry/logs/logger_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#ifdef ENABLE_LOGS_PREVIEW

# include "opentelemetry/common/key_value_iterable.h"
owent marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -68,4 +69,5 @@ class LoggerProvider
};
} // namespace logs
OPENTELEMETRY_END_NAMESPACE
#endif

#endif // ENABLE_LOGS_PREVIEW
7 changes: 4 additions & 3 deletions api/include/opentelemetry/trace/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace trace
/**
* No-op implementation of Span. This class should not be used directly.
*/
class NoopSpan final : public Span
class OPENTELEMETRY_EXPORT NoopSpan final : public Span
{
public:
explicit NoopSpan(const std::shared_ptr<Tracer> &tracer) noexcept
Expand Down Expand Up @@ -76,7 +76,8 @@ class NoopSpan final : public Span
/**
* No-op implementation of Tracer.
*/
class NoopTracer final : public Tracer, public std::enable_shared_from_this<NoopTracer>
class OPENTELEMETRY_EXPORT NoopTracer final : public Tracer,
public std::enable_shared_from_this<NoopTracer>
{
public:
// Tracer
Expand All @@ -101,7 +102,7 @@ class NoopTracer final : public Tracer, public std::enable_shared_from_this<Noop
/**
* No-op implementation of a TracerProvider.
*/
class NoopTracerProvider final : public opentelemetry::trace::TracerProvider
class OPENTELEMETRY_EXPORT NoopTracerProvider final : public opentelemetry::trace::TracerProvider
{
public:
NoopTracerProvider() noexcept
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/trace/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace trace
/**
* Stores the singleton global TracerProvider.
*/
class Provider
class OPENTELEMETRY_EXPORT Provider
{
public:
/**
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/trace/trace_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace trace
* For more information, see the W3C Trace Context specification:
* https://www.w3.org/TR/trace-context
*/
class TraceState
class OPENTELEMETRY_EXPORT TraceState
{
public:
static constexpr int kKeyMaxSize = 256;
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/trace/tracer_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace trace
/**
* Creates new Tracer instances.
*/
class TracerProvider
class OPENTELEMETRY_EXPORT TracerProvider
{
public:
virtual ~TracerProvider() = default;
Expand Down
2 changes: 1 addition & 1 deletion api/test/logs/logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class TestProvider : public LoggerProvider
bool /* include_trace_context */,
const common::KeyValueIterable & /* attributes */) override
{
return shared_ptr<Logger>(new TestLogger());
return nostd::shared_ptr<Logger>(new TestLogger());
}
};

Expand Down
49 changes: 49 additions & 0 deletions ci/do_ci.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@ switch ($action) {
exit $exit
}
}
"cmake.dll.test" {
cd "$BUILD_DIR"
cmake $SRC_DIR `
-DVCPKG_TARGET_TRIPLET=x64-windows `
-DOPENTELEMETRY_BUILD_DLL=1 `
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_DIR/scripts/buildsystems/vcpkg.cmake"
$exit = $LASTEXITCODE
if ($exit -ne 0) {
exit $exit
}
cmake --build .
$exit = $LASTEXITCODE
if ($exit -ne 0) {
exit $exit
}
ctest -C Debug
$exit = $LASTEXITCODE
if ($exit -ne 0) {
exit $exit
}
$env:PATH = "$BUILD_DIR\ext\src\dll\Debug;$env:PATH"
examples\simple\Debug\example_simple.exe
$exit = $LASTEXITCODE
if ($exit -ne 0) {
exit $exit
}
}
"cmake.maintainer.test" {
cd "$BUILD_DIR"
cmake $SRC_DIR `
Expand Down Expand Up @@ -116,6 +143,28 @@ switch ($action) {
exit $exit
}
}
"cmake.exporter.otprotocol.dll.test" {
lalitb marked this conversation as resolved.
Show resolved Hide resolved
cd "$BUILD_DIR"
cmake $SRC_DIR `
-DVCPKG_TARGET_TRIPLET=x64-windows `
-DOPENTELEMETRY_BUILD_DLL=1 `
-DWITH_OTPROTCOL=ON `
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_DIR/scripts/buildsystems/vcpkg.cmake"
$exit = $LASTEXITCODE
if ($exit -ne 0) {
exit $exit
}
cmake --build .
$exit = $LASTEXITCODE
if ($exit -ne 0) {
exit $exit
}
ctest -C Debug
$exit = $LASTEXITCODE
if ($exit -ne 0) {
exit $exit
}
}
"cmake.exporter.otprotocol.with_async_export.test" {
cd "$BUILD_DIR"
cmake $SRC_DIR `
Expand Down
11 changes: 11 additions & 0 deletions docs/build-as-dll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Design of building OpenTelemetry C++ SDK and exporters as DLL

**NOTE**: the current build produces a single DLL which includes API, SDK and
exporters. This **MAY** change in future.

OpenTelemetry C++ can be built into single DLL named opentelemetry_cpp.dll on
Windows, with CMake flag `OPENTELEMETRY_BUILD_DLL` set to `ON`.

To instrument an application based on the DLL, please define macro
`OPENTELEMETRY_BUILD_IMPORT_DLL` before including any OpenTelemetry API header
files in the application source files.
lalitb marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions examples/common/foo_library/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

if(DEFINED OPENTELEMETRY_BUILD_DLL)
add_definitions(-DOPENTELEMETRY_BUILD_IMPORT_DLL)
endif()
marcalff marked this conversation as resolved.
Show resolved Hide resolved

add_library(common_foo_library foo_library.h foo_library.cc)
target_link_libraries(common_foo_library PUBLIC ${CMAKE_THREAD_LIBS_INIT}
opentelemetry_api)
4 changes: 4 additions & 0 deletions examples/common/logs_foo_library/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

if(DEFINED OPENTELEMETRY_BUILD_DLL)
add_definitions(-DOPENTELEMETRY_BUILD_IMPORT_DLL)
endif()

add_library(common_logs_foo_library foo_library.h foo_library.cc)
target_link_libraries(common_logs_foo_library PUBLIC ${CMAKE_THREAD_LIBS_INIT}
opentelemetry_api)
70 changes: 48 additions & 22 deletions examples/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,43 +1,69 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

if(DEFINED OPENTELEMETRY_BUILD_DLL)
add_definitions(-DOPENTELEMETRY_BUILD_IMPORT_DLL)
endif()

include_directories(
${CMAKE_BINARY_DIR}/generated/third_party/opentelemetry-proto
${CMAKE_SOURCE_DIR}/exporters/otlp/include)

if(WITH_OTLP_GRPC)
add_executable(example_otlp_grpc grpc_main.cc)
target_link_libraries(
example_otlp_grpc ${CMAKE_THREAD_LIBS_INIT} common_foo_library
opentelemetry_trace opentelemetry_exporter_otlp_grpc)

target_link_libraries(example_otlp_grpc ${CMAKE_THREAD_LIBS_INIT}
common_foo_library)

if(DEFINED OPENTELEMETRY_BUILD_DLL)
target_link_libraries(example_otlp_grpc opentelemetry_cpp)
else()
target_link_libraries(example_otlp_grpc opentelemetry_trace
opentelemetry_exporter_otlp_grpc)
endif()
marcalff marked this conversation as resolved.
Show resolved Hide resolved
if(WITH_LOGS_PREVIEW)
add_executable(example_otlp_grpc_log grpc_log_main.cc)
target_link_libraries(
example_otlp_grpc_log
${CMAKE_THREAD_LIBS_INIT}
common_logs_foo_library
opentelemetry_trace
opentelemetry_logs
opentelemetry_exporter_otlp_grpc
opentelemetry_exporter_otlp_grpc_log)

target_link_libraries(example_otlp_grpc_log ${CMAKE_THREAD_LIBS_INIT}
common_logs_foo_library)
if(DEFINED OPENTELEMETRY_BUILD_DLL)
target_link_libraries(example_otlp_grpc_log opentelemetry_cpp)
else()
target_link_libraries(
example_otlp_grpc_log opentelemetry_trace opentelemetry_logs
opentelemetry_exporter_otlp_grpc opentelemetry_exporter_otlp_grpc_log)
endif()
endif()
endif()

if(WITH_OTLP_HTTP)
add_executable(example_otlp_http http_main.cc)
target_link_libraries(
example_otlp_http ${CMAKE_THREAD_LIBS_INIT} common_foo_library
opentelemetry_trace opentelemetry_exporter_otlp_http)

target_link_libraries(example_otlp_http ${CMAKE_THREAD_LIBS_INIT}
common_foo_library)
if(DEFINED OPENTELEMETRY_BUILD_DLL)
target_link_libraries(example_otlp_http opentelemetry_cpp
opentelemetry_common)
else()
target_link_libraries(example_otlp_http opentelemetry_trace
opentelemetry_exporter_otlp_http)
endif()

if(WITH_LOGS_PREVIEW)
add_executable(example_otlp_http_log http_log_main.cc)
target_link_libraries(
example_otlp_http_log
${CMAKE_THREAD_LIBS_INIT}
common_logs_foo_library
opentelemetry_trace
opentelemetry_logs
opentelemetry_exporter_otlp_http
opentelemetry_exporter_otlp_http_log)
target_link_libraries(example_otlp_http_log ${CMAKE_THREAD_LIBS_INIT}
common_logs_foo_library)

if(DEFINED OPENTELEMETRY_BUILD_DLL)
target_link_libraries(example_otlp_http_log opentelemetry_cpp
opentelemetry_common)
else()
target_link_libraries(
example_otlp_http_log common_logs_foo_library opentelemetry_trace
opentelemetry_logs opentelemetry_exporter_otlp_http
opentelemetry_exporter_otlp_http_log)
endif()

endif()
endif()

Expand Down
Loading