From 15a1229824d8192abd5b6e0da79c7572ab0f0ef2 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos <6349682+vaind@users.noreply.github.com> Date: Wed, 9 Mar 2022 12:15:02 +0100 Subject: [PATCH] feat: SDK name override (#686) --- CHANGELOG.md | 1 + CMakeLists.txt | 7 +++++++ README.md | 6 +++++- include/sentry.h | 10 ++++++---- tests/assertions.py | 4 ++++ tests/test_integration_stdout.py | 22 ++++++++++++++++++++++ 6 files changed, 45 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d8a88185..d3f17eaf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ **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. ## 0.4.15 diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bd67acf8..06f610344 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,9 +151,12 @@ if(SENTRY_BACKEND_CRASHPAD AND ANDROID) message(FATAL_ERROR "The Crashpad backend is not currently supported on Android") endif() +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}") 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 +224,10 @@ target_sources(sentry PRIVATE "${PROJECT_SOURCE_DIR}/include/sentry.h") add_library(sentry::sentry ALIAS sentry) add_subdirectory(src) +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) target_sources(sentry PRIVATE diff --git a/README.md b/README.md index 0261cb3aa..c072958a2 100644 --- a/README.md +++ b/README.md @@ -264,13 +264,17 @@ 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. 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 - `sentry`: This is the main library and the only default build target. diff --git a/include/sentry.h b/include/sentry.h index bfd365053..428633ee9 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -23,10 +23,12 @@ extern "C" { #endif /* SDK Version */ -#ifdef __ANDROID__ -# define SENTRY_SDK_NAME "sentry.native.android" -#else -# define SENTRY_SDK_NAME "sentry.native" +#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 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