Skip to content

Commit ecc7e2f

Browse files
committed
Fix serialization of identify event
1 parent 35e324b commit ecc7e2f

File tree

7 files changed

+45
-21
lines changed

7 files changed

+45
-21
lines changed

libs/common/include/events/detail/dispatcher.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "events/detail/summary_state.hpp"
1515
#include "events/events.hpp"
1616
#include "logger.hpp"
17+
#include "context_filter.hpp"
18+
1719

1820
namespace launchdarkly::events::detail {
1921

@@ -28,7 +30,7 @@ class Dispatcher {
2830
void request_flush();
2931

3032
void send(InputEvent);
31-
33+
3234
void shutdown();
3335

3436
private:
@@ -57,6 +59,8 @@ class Dispatcher {
5759

5860
bool full_outbox_encountered_;
5961

62+
launchdarkly::ContextFilter filter_;
63+
6064
Logger& logger_;
6165

6266
void handle_send(InputEvent e);

libs/common/include/events/events.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using Value = launchdarkly::Value;
1919
using VariationIndex = size_t;
2020
using Reason = EvaluationReason;
2121
using Context = launchdarkly::Context;
22-
using Json = boost::json::value;
22+
using EventContext = boost::json::value;
2323
using Version = std::uint64_t;
2424
using ContextKeys = std::map<std::string, std::string>;
2525

@@ -53,7 +53,12 @@ struct IdentifyEvent {
5353
Context context;
5454
};
5555

56-
struct IndexEvent : public IdentifyEvent {};
56+
struct OutIdentifyEvent {
57+
Date creation_date;
58+
EventContext context;
59+
};
60+
61+
struct IndexEvent : public OutIdentifyEvent {};
5762

5863
struct CustomEvent {
5964
Date creation_date;
@@ -86,7 +91,10 @@ struct VariationKey {
8691

8792
using InputEvent = std::variant<FeatureEvent, IdentifyEvent, CustomEvent>;
8893

89-
using OutputEvent = std::
90-
variant<IndexEvent, DebugEvent, FeatureEvent, IdentifyEvent, CustomEvent>;
94+
using OutputEvent = std::variant<IndexEvent,
95+
DebugEvent,
96+
FeatureEvent,
97+
OutIdentifyEvent,
98+
CustomEvent>;
9199

92100
} // namespace launchdarkly::events

libs/common/include/serialization/events/json_events.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void tag_invoke(boost::json::value_from_tag const&,
2424

2525
void tag_invoke(boost::json::value_from_tag const&,
2626
boost::json::value& json_value,
27-
IdentifyEvent const& event);
27+
OutIdentifyEvent const& event);
2828

2929
void tag_invoke(boost::json::value_from_tag const&,
3030
boost::json::value& json_value,

libs/common/src/events/dispatcher.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Dispatcher::Dispatcher(boost::asio::any_io_executor io,
3030
authorization_(std::move(authorization)),
3131
uuids_(),
3232
full_outbox_encountered_(false),
33+
filter_(config.all_attributes_private(), config.private_attributes()),
3334
logger_(logger) {
3435
schedule_flush();
3536
}
@@ -126,7 +127,13 @@ std::vector<OutputEvent> Dispatcher::process(InputEvent event) {
126127
std::vector<OutputEvent> out;
127128
std::visit(
128129
overloaded{[&](FeatureEvent&& e) { out.emplace_back(std::move(e)); },
129-
[&](IdentifyEvent&& e) { out.emplace_back(std::move(e)); },
130+
[&](IdentifyEvent&& e) {
131+
// Contexts should already have been checked for validity
132+
// by this point.
133+
assert(e.context.valid());
134+
out.emplace_back(OutIdentifyEvent{
135+
e.creation_date, filter_.filter(e.context)});
136+
},
130137
[&](CustomEvent&& e) { out.emplace_back(std::move(e)); }},
131138
std::move(event));
132139

libs/common/src/serialization/events/json_events.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ void tag_invoke(boost::json::value_from_tag const&,
5454

5555
void tag_invoke(boost::json::value_from_tag const&,
5656
boost::json::value& json_value,
57-
IdentifyEvent const& event) {
57+
OutIdentifyEvent const& event) {
5858
auto& obj = json_value.emplace_object();
5959
obj.emplace("kind", "identify");
6060
obj.emplace("creationDate", boost::json::value_from(event.creation_date));
61-
obj.emplace("context", boost::json::value_from(event.context));
61+
obj.emplace("context", event.context);
6262
}
6363

6464
void tag_invoke(boost::json::value_from_tag const&,

libs/common/tests/event_processor_test.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,18 @@ TEST_F(EventProcessorTests, thing) {
3131
std::this_thread::sleep_for(std::chrono::seconds(2));
3232

3333
auto c = launchdarkly::ContextBuilder().kind("org", "ld").build();
34-
auto ev = events::CustomEvent{
34+
ASSERT_TRUE(c.valid());
35+
auto ev = events::IdentifyEvent{
3536
std::chrono::system_clock::now(),
37+
c,
3638
};
37-
ev.data = Value({"foo", "bar", "baz"});
38-
ev.metric_value = 30;
3939

40-
for (std::size_t i = 0; i < 10; i++) {
40+
for (std::size_t i = 0; i < 3; i++) {
4141
ep.async_send(ev);
4242
}
4343

4444
std::this_thread::sleep_for(std::chrono::seconds(2));
4545

46-
ev.data = Value("qux");
47-
ev.metric_value = 42;
48-
49-
ep.async_send(ev);
50-
5146
ep.async_close();
5247
t.join();
5348
}

libs/common/tests/event_serialization_test.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#include "context_builder.hpp"
66
#include "events/events.hpp"
77

8+
#include "context_filter.hpp"
89
#include "serialization/events/json_events.hpp"
10+
#include "value.hpp"
911

1012
namespace launchdarkly::events {
1113

@@ -50,14 +52,22 @@ TEST(EventSerialization, DebugEvent) {
5052

5153
TEST(EventSerialization, IdentifyEvent) {
5254
auto creation_date = std::chrono::system_clock::from_time_t({});
53-
auto e = events::IdentifyEvent{creation_date,
54-
ContextBuilder().kind("foo", "bar").build()};
55+
auto attrs = AttributeReference::SetType({"/redact_me"});
56+
ContextFilter filter(false, attrs);
57+
58+
auto e = events::OutIdentifyEvent{creation_date,
59+
filter.filter(ContextBuilder()
60+
.kind("foo", "org")
61+
.set("bar", "baz")
62+
.set("redact_me", 3)
63+
.build())};
5564

5665
auto event = boost::json::value_from(e);
5766

5867
auto result = boost::json::parse(
5968
"{\"kind\":\"identify\",\"creationDate\":0,\"context\":{\"key\":"
60-
"\"bar\",\"kind\":\"foo\"}}");
69+
"\"org\",\"kind\":\"foo\",\"bar\":\"baz\",\"_meta\":{"
70+
"\"redactedAttributes\":[\"/redact_me\"]}}}");
6171

6272
ASSERT_EQ(result, event);
6373
}

0 commit comments

Comments
 (0)