Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add message container to payload #119

Merged
merged 1 commit into from
Apr 23, 2024
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
8 changes: 4 additions & 4 deletions src/main/java/com/spotify/confidence/Confidence.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ public Confidence withContext(Map<String, ConfidenceValue> context) {
}

@Override
public void send(String name) {
public void send(String eventName) {
try {
client().send(name, getContext(), Optional.empty());
client().send(eventName, getContext(), Optional.empty());
} catch (IllegalStateException e) {
// swallow this exception
}
}

@Override
public void send(String name, ConfidenceValue.Struct message) {
public void send(String eventName, ConfidenceValue.Struct message) {
try {
client().send(name, getContext(), Optional.of(message));
client().send(eventName, getContext(), Optional.of(message));
} catch (IllegalStateException e) {
// swallow this exception
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/spotify/confidence/EventSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

@Beta
public interface EventSender extends Contextual {
public void send(String name, ConfidenceValue.Struct message);
public void send(String eventName, ConfidenceValue.Struct message);

public void send(String name);
public void send(String eventName);

@Override
EventSender withContext(ConfidenceValue.Struct context);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/spotify/confidence/EventUploader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.spotify.confidence;

import static com.spotify.confidence.GrpcEventUploader.CONTEXT;

import com.google.protobuf.Struct;
import com.spotify.confidence.events.v1.Event;
import java.util.List;
Expand All @@ -15,8 +13,8 @@ static Event.Builder event(
.setEventDefinition(EventSenderEngineImpl.EVENT_NAME_PREFIX + name)
.setPayload(
Struct.newBuilder()
.putAllFields(message.orElse(ConfidenceValue.Struct.EMPTY).asProtoMap())
.putFields(CONTEXT, context.toProto()));
.putAllFields(context.asProtoMap())
.putFields("message", message.orElse(ConfidenceValue.Struct.EMPTY).toProto()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll assume this sends an empty message struct on absent messages. That's fine, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking it is better this way, as we ensure the message key is always reserved and not just overriding contexts if message has fields

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree 👍

}

CompletableFuture<Boolean> upload(List<Event> events);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class GrpcEventUploader implements EventUploader {
Status.Code.ABORTED,
Status.Code.INTERNAL,
Status.Code.DATA_LOSS);
static final String CONTEXT = "context";
private final String clientSecret;
private final Sdk sdk;
private final ManagedChannel managedChannel;
Expand Down
53 changes: 52 additions & 1 deletion src/test/java/com/spotify/confidence/EventSenderEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import java.io.IOException;
import java.time.Duration;
Expand Down Expand Up @@ -81,6 +82,48 @@ public void testEngineUploads() throws IOException {
assertThat(uploadCallsCount - fullBatchCount).isEqualTo(additionalBatch);
}

@Test
public void testOverlappingKeysInPayload() throws InterruptedException {
final FakeUploader alwaysSucceedUploader = new FakeUploader(List.of());
final EventSenderEngine engine =
new EventSenderEngineImpl(
1,
alwaysSucceedUploader,
clock,
DEFAULT_MAX_FLUSH_INTERVAL,
DEFAULT_MAX_MEMORY_CONSUMPTION);
// wait for the flush timeout to trigger the upload
engine.send(
"my_event",
ConfidenceValue.of(
ImmutableMap.of(
"a", ConfidenceValue.of(2),
"message", ConfidenceValue.of(3))),
Optional.of(
ConfidenceValue.Struct.of(
Map.of(
"a", ConfidenceValue.of(0),
"message", ConfidenceValue.of(1)))));
Thread.sleep(300);
assertThat(alwaysSucceedUploader.uploadCalls.peek().get(0).getPayload())
.isEqualTo(
Struct.newBuilder()
.putAllFields(
Map.of(
"message",
Value.newBuilder()
.setStructValue(
Struct.newBuilder()
.putFields(
"a", Value.newBuilder().setNumberValue(0).build())
.putFields(
"message",
Value.newBuilder().setNumberValue(1).build()))
.build(),
"a", Value.newBuilder().setNumberValue(2).build()))
.build());
}

@Test
public void testEngineCloseSuccessfullyWithoutEventsQueued() throws IOException {
final FakeUploader alwaysSucceedUploader = new FakeUploader(List.of());
Expand Down Expand Up @@ -156,7 +199,15 @@ public void testEngineUploadsWhenIntermittentErrorWillRetry() throws IOException
final Set<Value> uniqueEventIds =
fakeUploader.uploadCalls.stream()
.flatMap(Collection::stream)
.map(event -> event.getPayload().getFieldsMap().get("id"))
.map(
event ->
event
.getPayload()
.getFieldsMap()
.get("message")
.getStructValue()
.getFieldsMap()
.get("id"))
.collect(Collectors.toSet());
// Verify all events reached the uploader
assertThat(uniqueEventIds.size()).isEqualTo(numEvents);
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/com/spotify/confidence/GrpcEventUploaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ public void testMapsSingleEventBatchToProtobuf() throws ExecutionException, Inte
assertThat(protoEvent.getEventDefinition()).isEqualTo("eventDefinitions/event1");

final Map<String, com.google.protobuf.Value> fieldsMap = protoEvent.getPayload().getFieldsMap();
assertThat(fieldsMap.get("messageKey").getStringValue()).isEqualTo("value_1");
assertThat(
fieldsMap
.get(CONTEXT)
.get("message")
.getStructValue()
.getFieldsMap()
.get("contextKey")
.get("messageKey")
.getStringValue())
.isEqualTo("value_1");
assertThat(fieldsMap.get("contextKey").getStringValue()).isEqualTo("value_1");
}

@Test
Expand Down Expand Up @@ -136,15 +136,15 @@ public void testMapsMultiEventBatchToProtobuf() {

final Map<String, com.google.protobuf.Value> fieldsMap =
protoEvent.getPayload().getFieldsMap();
assertThat(fieldsMap.get("messageKey").getStringValue()).isEqualTo("value_m" + (i + 1));
assertThat(
fieldsMap
.get(CONTEXT)
.get("message")
.getStructValue()
.getFieldsMap()
.get("contextKey")
.get("messageKey")
.getStringValue())
.isEqualTo("value_c" + (i + 1));
.isEqualTo("value_m" + (i + 1));
assertThat(fieldsMap.get("contextKey").getStringValue()).isEqualTo("value_c" + (i + 1));
}
}

Expand Down
Loading