Skip to content
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
2 changes: 2 additions & 0 deletions .github/package-check/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ int main() {
#ifdef EXPECT_COUNTLY_SQLITE
countly.setDatabasePath("install_check.db");
#endif
// works in every build: without zlib it logs a warning and requests stay plain
countly.enableRequestCompression();
countly.setDeviceID("install-check-device");
countly.start("install-check-app-key", "https://example.invalid", 443, false);

Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/packaging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ jobs:
"$WORK/app-sqlite-no-path/app"
fi

- name: Install the port with the compression feature
run: '"$VCPKG_ROOT/vcpkg" install "countly-sdk-cpp[compression]" --recurse --overlay-ports="$WORK/ports" --triplet ${{ matrix.triplet }}'

- name: Build and run an app with the compression feature
if: matrix.run-app
run: |
cmake -S .github/package-check -B "$WORK/app-compression" -DCMAKE_TOOLCHAIN_FILE="$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake" -DVCPKG_TARGET_TRIPLET=${{ matrix.triplet }} -DCMAKE_BUILD_TYPE=Release
cmake --build "$WORK/app-compression" --config Release
if [ "$RUNNER_OS" = "Windows" ]; then
"$WORK/app-compression/Release/app.exe"
else
"$WORK/app-compression/app"
fi

# ──────────────────────────────────────────────
# Conan: the recipe builds from an archive of this
# commit and its test_package runs, without and with
Expand Down Expand Up @@ -240,6 +254,9 @@ jobs:
- name: Create the package with SQLite
run: conan create "$WORK/recipe" --version "$COUNTLY_VERSION" --build=missing -o "countly-sdk-cpp/*:with_sqlite=True"

- name: Create the package with request compression
run: conan create "$WORK/recipe" --version "$COUNTLY_VERSION" --build=missing -o "countly-sdk-cpp/*:with_zlib=True"

- name: Create the package as shared libraries
run: conan create "$WORK/recipe" --version "$COUNTLY_VERSION" --build=missing -o "*:shared=True"

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get -qq update
sudo apt-get -qq install -y --no-install-recommends clang-18 clang-tidy-18 libcurl4-openssl-dev libssl-dev
sudo apt-get -qq install -y --no-install-recommends clang-18 clang-tidy-18 libcurl4-openssl-dev libssl-dev zlib1g-dev

# On pull requests the base branch is analyzed too, so that only findings it lacks can fail the job.
- name: Analyze the SDK
run: |
# Runs clang-tidy over the sources of the checkout in $1 and writes the findings as SARIF to $2.sarif.
analyze() {
cd "$1"
cmake -DCOUNTLY_USE_SQLITE=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_C_COMPILER=clang-18 -DCMAKE_CXX_COMPILER=clang++-18 -B build .
cmake -DCOUNTLY_USE_SQLITE=ON -DCOUNTLY_USE_ZLIB=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_C_COMPILER=clang-18 -DCMAKE_CXX_COMPILER=clang++-18 -B build .
# run-clang-tidy analyzes nothing, and reports nothing, when its file pattern matches no source.
if ! grep -qF "\"file\": \"$PWD/src/" build/compile_commands.json; then
echo "::error title=clang-tidy::The compilation database has no source under $PWD/src"
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ jobs:
- name: system-sqlite
flags: -DCOUNTLY_USE_SQLITE=ON -DCOUNTLY_USE_SYSTEM_SQLITE=ON
apt: libsqlite3-dev
- name: zlib
flags: -DCOUNTLY_USE_SQLITE=ON -DCOUNTLY_USE_ZLIB=ON
apt: zlib1g-dev
- name: zlib-sanitizer-asan
flags: -DCOUNTLY_USE_SQLITE=ON -DCOUNTLY_USE_ZLIB=ON -DCMAKE_CXX_FLAGS="-fsanitize=address -fno-omit-frame-pointer" -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address" -DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=address"
apt: zlib1g-dev
- name: cpp17
flags: -DCOUNTLY_USE_SQLITE=ON -DCMAKE_CXX_STANDARD=17
- name: cpp20
Expand Down Expand Up @@ -354,12 +360,12 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get $APT_OPTS update
sudo apt-get $APT_OPTS install -y --no-install-recommends libcurl4-openssl-dev libssl-dev gcovr
sudo apt-get $APT_OPTS install -y --no-install-recommends libcurl4-openssl-dev libssl-dev zlib1g-dev gcovr

# Atomic counters: the SDK's request thread runs beside the tests, and racing plain counters yield negative branch counts that gcovr rejects.
- name: Build the tests with coverage
run: |
cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=ON -DCMAKE_BUILD_TYPE=Debug \
cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=ON -DCOUNTLY_USE_ZLIB=ON -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_C_FLAGS="--coverage -fprofile-update=atomic" -DCMAKE_CXX_FLAGS="--coverage -fprofile-update=atomic" \
-DCMAKE_EXE_LINKER_FLAGS=--coverage -DCMAKE_SHARED_LINKER_FLAGS=--coverage -B build .
cmake --build build -j 4
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
- Requests are built with fewer copies of their data, so a large request, such as one with a native crash dump, needs less memory.
- The minidumps of the SDK's crash handler hold the breadcrumbs and the app version of the crash moment. The SDK behavior setting "Allow Automatic Crash Reporting" (`acr`) turns the handler on and off.
- The vendored Breakpad client (`vendor/breakpad`, updated with `scripts/update_breakpad.py`) needs a C++17 compiler; the SDK itself still builds with C++14.
- Added `enableRequestCompression()` for sending request bodies of 1 KB to 1 MB compressed with gzip. The SDK compresses only after the server's SDK Behavior Settings answer says that it reads compressed bodies, so other servers keep getting plain requests.
- Added the CMake option `COUNTLY_USE_ZLIB` (vcpkg feature `compression`, Conan option `with_zlib`), which links zlib for `enableRequestCompression()`. It is off by default; in a build without it the call logs a warning and requests stay plain.
- With request compression, a client set with `setHTTPClient` gets `?content_encoding=gzip` at the end of the path and the gzip data, which contains zero bytes, as `data`: it must send all `data.size()` bytes.

- Mitigated an issue where reaching the event queue threshold logged a warning that events were dropped, although they were sent.
- Mitigated an issue where a view that was open during a device ID change without merge was ended under the new device ID.
Expand Down
21 changes: 20 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ option(COUNTLY_USE_SQLITE "Use SQLite" OFF)
option(COUNTLY_USE_SYSTEM_SQLITE "Link the system SQLite instead of compiling the vendored amalgamation" OFF)
option(COUNTLY_USE_SYSTEM_JSON "Compile against the system nlohmann_json instead of the vendored copy" OFF)
option(COUNTLY_USE_BREAKPAD "Capture native crashes with the vendored Breakpad client (Windows, Linux)" OFF)
option(COUNTLY_USE_ZLIB "Link zlib, which enableRequestCompression() needs to compress request bodies" OFF)
option(COUNTLY_BUILD_TESTS "Build test programs" OFF)
option(COUNTLY_BUILD_SAMPLE "Build Sample programs" OFF)

Expand All @@ -32,6 +33,7 @@ message("Use SQLite:" ${COUNTLY_USE_SQLITE})
message("Use system SQLite:" ${COUNTLY_USE_SYSTEM_SQLITE})
message("Use system nlohmann_json:" ${COUNTLY_USE_SYSTEM_JSON})
message("Use Breakpad:" ${COUNTLY_USE_BREAKPAD})
message("Use zlib:" ${COUNTLY_USE_ZLIB})
message("Build test programs:" ${COUNTLY_BUILD_TESTS})
message("Build Sample programs:" ${COUNTLY_BUILD_SAMPLE})

Expand Down Expand Up @@ -126,6 +128,14 @@ elseif(NOT WIN32)
target_link_libraries(countly CURL::libcurl)
endif()

if(COUNTLY_USE_ZLIB)
message("Linking zlib for request compression")
find_package(ZLIB REQUIRED)
# No public header changes with this definition, so only the library sees it
target_compile_definitions(countly PRIVATE COUNTLY_USE_ZLIB)
target_link_libraries(countly ZLIB::ZLIB)
endif()

if(COUNTLY_USE_SQLITE)
# The public headers change with this definition, so code that includes them must see it too
target_compile_definitions(countly PUBLIC COUNTLY_USE_SQLITE)
Expand Down Expand Up @@ -220,7 +230,8 @@ if(COUNTLY_BUILD_TESTS)
${CMAKE_CURRENT_SOURCE_DIR}/tests/location.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/server_url_app_key.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/request_scenarios.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/native_crash.cpp)
${CMAKE_CURRENT_SOURCE_DIR}/tests/native_crash.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/request_compression.cpp)

target_compile_options(countly-tests PRIVATE $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-g>)
# the request scenarios read and write their records under api/requests/
Expand Down Expand Up @@ -251,6 +262,11 @@ if(COUNTLY_BUILD_TESTS)
message("Compiling definitions for custom http")
target_compile_definitions(countly-tests PRIVATE COUNTLY_USE_CUSTOM_HTTP)
endif()
if(COUNTLY_USE_ZLIB)
# the compression tests decode what the SDK sent
target_compile_definitions(countly-tests PRIVATE COUNTLY_USE_ZLIB)
target_link_libraries(countly-tests ZLIB::ZLIB)
endif()
target_include_directories(countly-tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor/doctest/doctest)
target_link_libraries(countly-tests countly)
set_target_properties(countly-tests PROPERTIES
Expand Down Expand Up @@ -306,6 +322,9 @@ endif()
if(COUNTLY_USE_SQLITE AND COUNTLY_USE_SYSTEM_SQLITE)
string(APPEND COUNTLY_CONFIG_DEPENDENCIES "\nfind_dependency(SQLite3)")
endif()
if(COUNTLY_USE_ZLIB)
string(APPEND COUNTLY_CONFIG_DEPENDENCIES "\nfind_dependency(ZLIB)")
endif()
include(CMakePackageConfigHelpers)
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/countlyConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/countlyConfig.cmake
INSTALL_DESTINATION lib/cmake/countly)
Expand Down
4 changes: 4 additions & 0 deletions api/countly-sdk-cpp.api
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ cly::Countly::enableCrashReporting method void () public
cly::Countly::enableImmediateRequestOnStop method void () public
cly::Countly::enableManualSessionControl method void () public
cly::Countly::enableRemoteConfig method void () public
cly::Countly::enableRequestCompression method void () public
cly::Countly::endSession method bool () public
cly::Countly::enrollABOnRCDownload method void () public
cly::Countly::fillEventsIntoJson method void (nlohmann::json &, std::string &) public
Expand Down Expand Up @@ -220,6 +221,7 @@ cly::CountlyConfiguration::maxStackTraceLinesPerThread field unsigned int public
cly::CountlyConfiguration::maxValueSize field unsigned int public
cly::CountlyConfiguration::metrics field nlohmann::json public
cly::CountlyConfiguration::port field unsigned int public
cly::CountlyConfiguration::requestCompressionEnabled field bool public
cly::CountlyConfiguration::requestDropAgeHours field unsigned int public
cly::CountlyConfiguration::requestQueueThreshold field unsigned int public
cly::CountlyConfiguration::salt field std::string public
Expand Down Expand Up @@ -351,13 +353,15 @@ cly::RequestModule::RequestModule constructor void (std::shared_ptr<CountlyConfi
cly::RequestModule::addRequestToQueue method void (const std::map<std::string, std::string> &) public
cly::RequestModule::addRequestToQueue method void (const std::map<std::string, std::string> &, std::chrono::system_clock::time_point) public
cly::RequestModule::clearRequestQueue method void () public
cly::RequestModule::currentServer method std::string () public
cly::RequestModule::initGlobalNetworking method void () static public
cly::RequestModule::limitSendTime method void () public
cly::RequestModule::processQueue method void (std::shared_ptr<std::mutex>, bool) public
cly::RequestModule::removeOtherAppKeysFromQueue method long long (const std::string &) public
cly::RequestModule::replaceAppKeysInQueue method void (const std::string &) public
cly::RequestModule::sendHTTP method HTTPResponse (std::string, std::string) public
cly::RequestModule::sendImmediate method HTTPResponse (const std::string &, const std::string &) public
cly::RequestModule::setCompressedBodiesAccepted method void (bool, const std::string &) public
cly::RequestModule::setConfigurationProvider method void (std::weak_ptr<ConfigurationProvider>) public
cly::RequestModule::setOnMergeDelivered method void (std::function<void ()>) public
cly::RequestModule::setServerUrl method void (const std::string &, unsigned int, const std::string &) public
Expand Down
17 changes: 17 additions & 0 deletions include/countly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,23 @@ class Countly : public cly::CountlyDelegates {
*/
void disableHealthCheck();

/**
* Sends request bodies of 1 KB to 1 MB compressed with gzip, which makes them several times smaller. Off by
* default. The SDK compresses only after the server's SDK behavior settings answer said that it reads compressed
* bodies, so a server that doesn't know the feature keeps getting plain requests, and with
* disableSDKBehaviorSettingsUpdates() the SDK never compresses. When the server, or something in front of it,
* answers a compressed request without accepting it, that request goes plain on its next try and compression stops
* until a later SDK behavior settings answer says again that the server reads compressed bodies.
*
* It needs an SDK built with the CMake option COUNTLY_USE_ZLIB; in other builds it logs a warning and requests stay
* plain. A client set with setHTTPClient gets a compressed request with "?content_encoding=gzip" at the end of its
* path and the gzip data, which contains zero bytes, as 'data': it must send all data.size() bytes. Such a client
* may report no status code, so every failed compressed request it sends, also one that got no answer, stops
* compression until the next SDK behavior settings answer.
* Call it before start().
*/
void enableRequestCompression();

/**
* Disable SDK behavior settings updates that SDK performs periodically from the server.
*/
Expand Down
6 changes: 6 additions & 0 deletions include/countly/countly_configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ struct CountlyConfiguration {
*/
bool healthCheckEnabled = true;

/**
* Whether request bodies are sent gzip-compressed, once the server's SDK behavior settings answer said that it
* reads compressed bodies. Only builds with COUNTLY_USE_ZLIB compress.
*/
bool requestCompressionEnabled = false;

std::string sdkBehaviorSettings;

CountlyConfiguration(const std::string appKey, std::string serverUrl) {
Expand Down
16 changes: 16 additions & 0 deletions include/countly/request_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ class RequestModule {
*/
void setServerUrl(const std::string &url, unsigned int port, const std::string &path = std::string());

/**
* The server the requests go to now, as "<URL>:<port><URL path>". A behavior settings fetch notes it before the
* request and passes it to setCompressedBodiesAccepted() with the answer.
*/
std::string currentServer();

/**
* Tells what an SDK behavior settings answer said about gzip-compressed request bodies. Every answer replaces the
* one before, and an accepting answer also ends a stop caused by a refused compressed request. The answer of a
* server other than the current one is ignored, as is every answer without enableRequestCompression().
*
* @param accepted: whether the answer carried "gzip": true
* @param server: the server that answered, as currentServer() gave it before the request
*/
void setCompressedBodiesAccepted(bool accepted, const std::string &server);

/**
* Gives every queued request the app key. The caller holds the mutex it passes to processQueue().
*/
Expand Down
11 changes: 6 additions & 5 deletions packaging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ submitted by copying them over:
- `vcpkg/ports/countly-sdk-cpp` goes to `ports/countly-sdk-cpp` in [microsoft/vcpkg](https://github.com/microsoft/vcpkg).
- `conan/countly-sdk-cpp` goes to `recipes/countly-sdk-cpp` in [conan-io/conan-center-index](https://github.com/conan-io/conan-center-index).

Both build against the package manager's own nlohmann/json, OpenSSL, libcurl and (with the `sqlite` feature or the
`with_sqlite` option) SQLite, and install the CMake package used as:
Both build against the package manager's own nlohmann/json, OpenSSL, libcurl, (with the `sqlite` feature or the
`with_sqlite` option) SQLite and (with the `compression` feature or the `with_zlib` option) zlib, and install the CMake
package used as:

```cmake
find_package(countly CONFIG REQUIRED)
Expand Down Expand Up @@ -40,6 +41,6 @@ local archive of the tree instead.

`.github/workflows/packaging.yml` does that for every commit that touches the build or these files: it packs the commit
with `git archive` (which, like GitHub's release archives, leaves the submodules out), builds the port and the recipe
from that archive on Linux, Windows and macOS, and builds an app against each, without and with SQLite. The port is
also built for arm64 Android, and the recipe also as shared libraries. It also checks that the versions in both match
`COUNTLY_SDK_VERSION`.
from that archive on Linux, Windows and macOS, and builds an app against each, without and with SQLite and with
request compression. The port is also built for arm64 Android, and the recipe also as shared libraries. It also checks
that the versions in both match `COUNTLY_SDK_VERSION`.
5 changes: 5 additions & 0 deletions packaging/conan/countly-sdk-cpp/all/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ class CountlySdkCppConan(ConanFile):
"shared": [True, False],
"fPIC": [True, False],
"with_sqlite": [True, False],
"with_zlib": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"with_sqlite": False,
"with_zlib": False,
}
implements = ["auto_shared_fpic"]

Expand All @@ -43,6 +45,8 @@ def requirements(self):
self.requires("libcurl/[>=7.78 <9]")
if self.options.with_sqlite:
self.requires("sqlite3/3.54.0")
if self.options.with_zlib:
self.requires("zlib/[>=1.2.11 <2]")

def validate(self):
check_min_cppstd(self, 14)
Expand All @@ -58,6 +62,7 @@ def generate(self):
tc.cache_variables["COUNTLY_USE_SYSTEM_JSON"] = True
tc.cache_variables["COUNTLY_USE_SQLITE"] = bool(self.options.with_sqlite)
tc.cache_variables["COUNTLY_USE_SYSTEM_SQLITE"] = bool(self.options.with_sqlite)
tc.cache_variables["COUNTLY_USE_ZLIB"] = bool(self.options.with_zlib)
tc.cache_variables["COUNTLY_BUILD_TESTS"] = False
tc.cache_variables["COUNTLY_BUILD_SAMPLE"] = False
tc.generate()
Expand Down
1 change: 1 addition & 0 deletions packaging/vcpkg/ports/countly-sdk-cpp/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ vcpkg_from_github(

vcpkg_check_features(OUT_FEATURE_OPTIONS FEATURE_OPTIONS
FEATURES
compression COUNTLY_USE_ZLIB
sqlite COUNTLY_USE_SQLITE
sqlite COUNTLY_USE_SYSTEM_SQLITE
)
Expand Down
6 changes: 6 additions & 0 deletions packaging/vcpkg/ports/countly-sdk-cpp/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
}
],
"features": {
"compression": {
"description": "Compress request bodies with gzip",
"dependencies": [
"zlib"
]
},
"sqlite": {
"description": "Store events and requests in SQLite",
"dependencies": [
Expand Down
12 changes: 12 additions & 0 deletions scripts/generate_sbom.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ def build_sbom() -> dict:
prop("countly:condition", "linked on non-Windows platforms unless the consumer supplies a custom HTTP function; version is determined by the consumer's environment"),
],
},
{
"bom-ref": "zlib",
"type": "library",
"name": "zlib",
"purl": "pkg:generic/zlib",
"scope": "optional",
"properties": [
prop("countly:origin", "system library, resolved at consumer build time (find_package(ZLIB))"),
prop("countly:cmake-option", "COUNTLY_USE_ZLIB=ON"),
prop("countly:condition", "linked only when COUNTLY_USE_ZLIB=ON, for gzip-compressed request bodies; version is determined by the consumer's environment"),
],
},
{
"bom-ref": "winhttp",
"type": "library",
Expand Down
Loading
Loading