Skip to content
Open
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
7 changes: 7 additions & 0 deletions libs/client-sdk/src/client_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "client_impl.hpp"
#include "data_sources/null_data_source.hpp"
#include "data_sources/polling_data_source.hpp"
Expand All @@ -12,6 +12,7 @@
#include <launchdarkly/logging/null_logger.hpp>

#include <chrono>
#include <cmath>
#include <optional>
#include <utility>

Expand Down Expand Up @@ -220,6 +221,12 @@
void ClientImpl::TrackInternal(std::string event_name,
std::optional<Value> data,
std::optional<double> metric_value) {
if (metric_value && !std::isfinite(*metric_value)) {
LD_LOG(logger_, LogLevel::kWarn)
<< "Track method called with a non-finite metric value for event \""
<< event_name << "\"; event was not recorded";
return;
}
event_processor_->SendAsync(events::TrackEventParams{
std::chrono::system_clock::now(), std::move(event_name), context_,
std::move(data), metric_value});
Expand All @@ -244,7 +251,7 @@
}

template <typename T>
EvaluationDetail<T> ClientImpl::VariationInternal(FlagKey const& key,

Check warning on line 254 in libs/client-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/client-sdk/src/client_impl.cpp:254:51 [bugprone-easily-swappable-parameters]

2 adjacent parameters of 'VariationInternal' of convertible types are easily swapped by mistake

Check warning on line 254 in libs/client-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/client-sdk/src/client_impl.cpp:254:33 [readability-function-cognitive-complexity]

function 'VariationInternal' has cognitive complexity of 28 (threshold 25)
Value default_value,
bool check_type,
bool detailed,
Expand Down
47 changes: 47 additions & 0 deletions libs/client-sdk/tests/client_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include <gtest/gtest.h>
#include <launchdarkly/client_side/client.hpp>
#include <launchdarkly/context_builder.hpp>
#include <launchdarkly/logging/log_backend.hpp>
#include <limits>
#include <map>
#include <memory>
#include <mutex>

using namespace launchdarkly;
using namespace launchdarkly::client_side;
Expand All @@ -14,7 +18,7 @@

Client client(std::move(*config), context);

char const* version = client.Version();

Check warning on line 21 in libs/client-sdk/tests/client_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/client-sdk/tests/client_test.cpp:21:27 [readability-static-accessed-through-instance]

static member accessed through instance
ASSERT_TRUE(version);
ASSERT_STREQ(version, "3.13.3"); // {x-release-please-version}
}
Expand Down Expand Up @@ -110,3 +114,46 @@

ASSERT_NE(date, client_side::data_sources::DataSourceStatus::DateTime{});
}

class WarningRecorder : public ILogBackend {
public:
bool Enabled(LogLevel level) noexcept override { return true; }

void Write(LogLevel level, std::string message) noexcept override {
std::lock_guard<std::mutex> lock(mutex_);
if (level == LogLevel::kWarn &&
message.find("non-finite metric value") != std::string::npos) {
count_++;
}
}

int Count() {
std::lock_guard<std::mutex> lock(mutex_);
return count_;
}

private:
std::mutex mutex_;
int count_ = 0;
};

TEST(ClientTest, TrackWithNonFiniteMetricValueLogsWarning) {
auto backend = std::make_shared<WarningRecorder>();
auto builder = ConfigBuilder("sdk-123");
builder.Logging().Logging(
config::shared::builders::LoggingBuilder::CustomLogging().Backend(
backend));
Client client(builder.Build().value(),
ContextBuilder().Kind("cat", "shadow").Build());

client.Track("finite", Value::Null(), 1.0);
ASSERT_EQ(backend->Count(), 0);

client.Track("nan", Value::Null(),
std::numeric_limits<double>::quiet_NaN());
client.Track("inf", Value::Null(),
std::numeric_limits<double>::infinity());
client.Track("negative-inf", Value::Null(),
-std::numeric_limits<double>::infinity());
ASSERT_EQ(backend->Count(), 3);
}
7 changes: 7 additions & 0 deletions libs/server-sdk/src/client_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "client_impl.hpp"

#include "all_flags_state/all_flags_state_builder.hpp"
Expand All @@ -24,6 +24,7 @@
#include <launchdarkly/server_side/config/built/all_built.hpp>

#include <chrono>
#include <cmath>
#include <optional>
#include <utility>

Expand Down Expand Up @@ -93,7 +94,7 @@
std::vector<std::unique_ptr<data_interfaces::IFDv2InitializerFactory>>
initializer_factories;
for (auto const& initializer : cfg.initializers) {
initializer_factories.push_back(

Check warning on line 97 in libs/server-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/client_impl.cpp:97:9 [performance-inefficient-vector-operation]

'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop
std::make_unique<data_systems::FDv2PollingInitializerFactory>(
executor, logger, endpoints, http_properties, initializer));
}
Expand Down Expand Up @@ -219,7 +220,7 @@
bool IsFlagPresent(
std::shared_ptr<data_model::FlagDescriptor> const& flag_desc);

ClientImpl::ClientImpl(Config config, std::string const& version)

Check warning on line 223 in libs/server-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/client_impl.cpp:223:31 [performance-unnecessary-value-param]

the parameter 'config' is copied for each invocation but only used as a const reference; consider making it a const reference
: config_(config),
http_properties_(
config::builders::HttpPropertiesBuilder(config.HttpProperties())
Expand All @@ -237,7 +238,7 @@
logger_(MakeLogger(config.Logging())),
ioc_(kAsioConcurrencyHint),
work_(boost::asio::make_work_guard(ioc_)),
status_manager_(),

Check warning on line 241 in libs/server-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/client_impl.cpp:241:7 [readability-redundant-member-init]

initializer for member 'status_manager_' is redundant
data_system_(MakeDataSystem(http_properties_,
config_,
ioc_.get_executor(),
Expand Down Expand Up @@ -370,6 +371,12 @@
<< "Track method called with an invalid context";
return;
}
if (metric_value && !std::isfinite(*metric_value)) {
LD_LOG(logger_, LogLevel::kWarn)
<< "Track method called with a non-finite metric value for event \""
<< event_name << "\"; event was not recorded";
return;
}
// Execute afterTrack hooks before moving the data
// Typically we would execute this after the data has been enqueued.
// In this SDK doing so would introduce a performance penalty because we
Expand Down Expand Up @@ -546,7 +553,7 @@
}

auto detail = PostEvaluation(key, context, default_value, result,
event_scope, flag_rule.get()->item);

Check warning on line 556 in libs/server-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/client_impl.cpp:556:47 [readability-redundant-smartptr-get]

redundant get() call on smart pointer

// Execute afterEvaluation hooks
if (executor) {
Expand All @@ -570,7 +577,7 @@
return std::nullopt;
}

EvaluationDetail<Value> ClientImpl::PostEvaluation(

Check warning on line 580 in libs/server-sdk/src/client_impl.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/client_impl.cpp:580:37 [readability-convert-member-functions-to-static]

method 'PostEvaluation' can be made static
std::string const& key,
Context const& context,
Value const& default_value,
Expand Down
20 changes: 20 additions & 0 deletions libs/server-sdk/tests/hooks_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Tests for hooks implementation based on:
// https://github.com/launchdarkly/sdk-specs/blob/main/specs/HOOK-hooks/README.md
// Spec commit: main branch as of implementation date
Expand All @@ -10,6 +10,7 @@
#include <launchdarkly/server_side/config/config_builder.hpp>
#include <launchdarkly/server_side/hooks/hook.hpp>

#include <limits>
#include <memory>
#include <string>
#include <vector>
Expand All @@ -36,7 +37,7 @@

explicit RecordingHook(std::string name) : metadata_(std::move(name)) {}

HookMetadata const& Metadata() const override { return metadata_; }

Check warning on line 40 in libs/server-sdk/tests/hooks_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/hooks_test.cpp:40:5 [modernize-use-nodiscard]

function 'Metadata' should be marked [[nodiscard]]

EvaluationSeriesData BeforeEvaluation(
EvaluationSeriesContext const& series_context,
Expand Down Expand Up @@ -82,7 +83,7 @@
track_calls_.push_back(call);
}

std::vector<EvaluationCall> const& GetEvaluationCalls() const {

Check warning on line 86 in libs/server-sdk/tests/hooks_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/hooks_test.cpp:86:5 [modernize-use-nodiscard]

function 'GetEvaluationCalls' should be marked [[nodiscard]]
return evaluation_calls_;
}

Expand Down Expand Up @@ -1019,3 +1020,22 @@
ASSERT_TRUE(retrieved2.has_value());
EXPECT_EQ(std::any_cast<int>(*(*retrieved2)), 2);
}

TEST_F(HooksTest, TrackWithNonFiniteMetricValueIsNotRecorded) {
auto hook = std::make_shared<RecordingHook>("test-hook");

auto config = ConfigBuilder("sdk-key").Offline(true).Hooks(hook).Build();
ASSERT_TRUE(config);

Client client(std::move(*config));

auto context = ContextBuilder().Kind("user", "user-key").Build();

for (double metric : {std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::infinity(),
-std::numeric_limits<double>::infinity()}) {
client.Track(context, "test-event", Value::Null(), metric);
}

EXPECT_TRUE(hook->GetTrackCalls().empty());
}
Loading