From 165c4662bb3308ac1dbc9a7bad2753be730728c0 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Tue, 8 Mar 2022 16:48:33 +0100 Subject: [PATCH 1/2] feat: SDK name override --- CHANGELOG.md | 2 ++ CMakeLists.txt | 10 ++++++++++ README.md | 5 ++++- include/sentry.h | 6 ------ src/sentry_boot.h | 2 ++ tests/assertions.py | 4 ++++ tests/test_integration_stdout.py | 22 ++++++++++++++++++++++ 7 files changed, 44 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d8a88185..ebf42c305 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ **Features**: - Removed the `SENTRY_PERFORMANCE_MONITORING` compile flag requirement to access performance monitoring in the Sentry SDK. Performance monitoring is now available to everybody who has opted into the experimental API. +- Allow overriding the SDK name at build time - set the `SENTRY_SDK_NAME` CMake cache variable. +- Remove `SENTRY_SDK_NAME` and `SENTRY_SDK_USER_AGENT` macros from `sentry.h` ## 0.4.15 diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bd67acf8..74872b9f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,9 +151,17 @@ if(SENTRY_BACKEND_CRASHPAD AND ANDROID) message(FATAL_ERROR "The Crashpad backend is not currently supported on Android") endif() +if(ANDROID) + set(SENTRY_DEFAULT_SDK_NAME "sentry.native.android") +else() + set(SENTRY_DEFAULT_SDK_NAME "sentry.native") +endif() +set(SENTRY_SDK_NAME "${SENTRY_DEFAULT_SDK_NAME}" CACHE STRING "The SDK name to report when sending events.") + message(STATUS "SENTRY_TRANSPORT=${SENTRY_TRANSPORT}") message(STATUS "SENTRY_BACKEND=${SENTRY_BACKEND}") message(STATUS "SENTRY_LIBRARY_TYPE=${SENTRY_LIBRARY_TYPE}") +message(STATUS "SENTRY_SDK_NAME=${SENTRY_SDK_NAME}") if(ANDROID) set(SENTRY_WITH_LIBUNWINDSTACK TRUE) @@ -221,6 +229,8 @@ target_sources(sentry PRIVATE "${PROJECT_SOURCE_DIR}/include/sentry.h") add_library(sentry::sentry ALIAS sentry) add_subdirectory(src) +target_compile_definitions(sentry PRIVATE SENTRY_SDK_NAME="${SENTRY_SDK_NAME}") + # we do not need this on android, only linux if(LINUX) target_sources(sentry PRIVATE diff --git a/README.md b/README.md index 0261cb3aa..4f1da4b07 100644 --- a/README.md +++ b/README.md @@ -264,13 +264,16 @@ Legend: - `SENTRY_FOLDER` (Default: not defined): Sets the sentry-native projects folder name for generators which support project hierarchy (like Microsoft Visual Studio). - To use this feature you need to enable hierarchy via [`USE_FOLDERS` property](https://cmake.org/cmake/help/latest/prop_gbl/USE_FOLDERS.html) + To use this feature you need to enable hierarchy via [`USE_FOLDERS` property](https://cmake.org/cmake/help/latest/prop_gbl/USE_FOLDERS.html) - `CRASHPAD_ENABLE_STACKTRACE` (Default: OFF): This enables client-side stackwalking when using the crashpad backend. Stack unwinding will happen on the client's machine and the result will be submitted to Sentry attached to the generated minidump. Note that this feature is still experimental. +- `SENTRY_SDK_NAME` (Default: sentry.native or sentry.native.android): + Sets the SDK name that should be included in the reported events. + ### Build Targets - `sentry`: This is the main library and the only default build target. diff --git a/include/sentry.h b/include/sentry.h index bfd365053..2f44f1832 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -23,13 +23,7 @@ extern "C" { #endif /* SDK Version */ -#ifdef __ANDROID__ -# define SENTRY_SDK_NAME "sentry.native.android" -#else -# define SENTRY_SDK_NAME "sentry.native" -#endif #define SENTRY_SDK_VERSION "0.4.15" -#define SENTRY_SDK_USER_AGENT SENTRY_SDK_NAME "/" SENTRY_SDK_VERSION /* common platform detection */ #ifdef _WIN32 diff --git a/src/sentry_boot.h b/src/sentry_boot.h index 2231366c0..7e8be85f3 100644 --- a/src/sentry_boot.h +++ b/src/sentry_boot.h @@ -40,4 +40,6 @@ #include #include +#define SENTRY_SDK_USER_AGENT SENTRY_SDK_NAME "/" SENTRY_SDK_VERSION + #endif diff --git a/tests/assertions.py b/tests/assertions.py index bc956e199..9e33e1d42 100644 --- a/tests/assertions.py +++ b/tests/assertions.py @@ -40,6 +40,7 @@ def assert_meta( release="test-example-release", integration=None, transaction="test-transaction", + sdk_override=None, ): event = envelope.get_event() @@ -94,6 +95,9 @@ def assert_meta( ) assert event["contexts"]["os"]["build"] is not None + if sdk_override != None: + expected_sdk["name"] = sdk_override + assert_matches(event, expected) assert_matches(event["sdk"], expected_sdk) assert_matches( diff --git a/tests/test_integration_stdout.py b/tests/test_integration_stdout.py index 8998fefba..710dea63a 100644 --- a/tests/test_integration_stdout.py +++ b/tests/test_integration_stdout.py @@ -42,6 +42,28 @@ def test_capture_stdout(cmake): assert_event(envelope) +def test_sdk_name_override(cmake): + sdk_name = "cUsToM.SDK" + tmp_path = cmake( + ["sentry_example"], + { + "SENTRY_BACKEND": "none", + "SENTRY_TRANSPORT": "none", + "SENTRY_SDK_NAME": sdk_name, + }, + ) + + output = check_output( + tmp_path, + "sentry_example", + ["stdout", "capture-event"], + ) + envelope = Envelope.deserialize(output) + + assert_meta(envelope, sdk_override=sdk_name) + assert_event(envelope) + + @pytest.mark.skipif(not has_files, reason="test needs a local filesystem") def test_multi_process(cmake): # NOTE: It would have been nice to do *everything* in a unicode-named From 929755ba095ee50b4e7750523f7a9eeb53505267 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Wed, 9 Mar 2022 10:56:37 +0100 Subject: [PATCH 2/2] refactor: bring back SDK_NAME and SDK_USER_AGENT to sentry.h --- CHANGELOG.md | 1 - CMakeLists.txt | 11 ++++------- README.md | 3 ++- include/sentry.h | 8 ++++++++ src/sentry_boot.h | 2 -- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebf42c305..d3f17eaf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,6 @@ - Removed the `SENTRY_PERFORMANCE_MONITORING` compile flag requirement to access performance monitoring in the Sentry SDK. Performance monitoring is now available to everybody who has opted into the experimental API. - Allow overriding the SDK name at build time - set the `SENTRY_SDK_NAME` CMake cache variable. -- Remove `SENTRY_SDK_NAME` and `SENTRY_SDK_USER_AGENT` macros from `sentry.h` ## 0.4.15 diff --git a/CMakeLists.txt b/CMakeLists.txt index 74872b9f7..06f610344 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,12 +151,7 @@ if(SENTRY_BACKEND_CRASHPAD AND ANDROID) message(FATAL_ERROR "The Crashpad backend is not currently supported on Android") endif() -if(ANDROID) - set(SENTRY_DEFAULT_SDK_NAME "sentry.native.android") -else() - set(SENTRY_DEFAULT_SDK_NAME "sentry.native") -endif() -set(SENTRY_SDK_NAME "${SENTRY_DEFAULT_SDK_NAME}" CACHE STRING "The SDK name to report when sending events.") +set(SENTRY_SDK_NAME "" CACHE STRING "The SDK name to report when sending events.") message(STATUS "SENTRY_TRANSPORT=${SENTRY_TRANSPORT}") message(STATUS "SENTRY_BACKEND=${SENTRY_BACKEND}") @@ -229,7 +224,9 @@ target_sources(sentry PRIVATE "${PROJECT_SOURCE_DIR}/include/sentry.h") add_library(sentry::sentry ALIAS sentry) add_subdirectory(src) -target_compile_definitions(sentry PRIVATE SENTRY_SDK_NAME="${SENTRY_SDK_NAME}") +if (NOT SENTRY_SDK_NAME STREQUAL "") + target_compile_definitions(sentry PRIVATE SENTRY_SDK_NAME="${SENTRY_SDK_NAME}") +endif() # we do not need this on android, only linux if(LINUX) diff --git a/README.md b/README.md index 4f1da4b07..c072958a2 100644 --- a/README.md +++ b/README.md @@ -272,7 +272,8 @@ Legend: Note that this feature is still experimental. - `SENTRY_SDK_NAME` (Default: sentry.native or sentry.native.android): - Sets the SDK name that should be included in the reported events. + Sets the SDK name that should be included in the reported events. If you're overriding this, make sure to also define + the same value using `target_compile_definitions()` on your own targets that include `sentry.h`. ### Build Targets diff --git a/include/sentry.h b/include/sentry.h index 2f44f1832..428633ee9 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -23,7 +23,15 @@ extern "C" { #endif /* SDK Version */ +#ifndef SENTRY_SDK_NAME +# ifdef __ANDROID__ +# define SENTRY_SDK_NAME "sentry.native.android" +# else +# define SENTRY_SDK_NAME "sentry.native" +# endif +#endif #define SENTRY_SDK_VERSION "0.4.15" +#define SENTRY_SDK_USER_AGENT SENTRY_SDK_NAME "/" SENTRY_SDK_VERSION /* common platform detection */ #ifdef _WIN32 diff --git a/src/sentry_boot.h b/src/sentry_boot.h index 7e8be85f3..2231366c0 100644 --- a/src/sentry_boot.h +++ b/src/sentry_boot.h @@ -40,6 +40,4 @@ #include #include -#define SENTRY_SDK_USER_AGENT SENTRY_SDK_NAME "/" SENTRY_SDK_VERSION - #endif