Skip to content

Commit

Permalink
add context field in the payload
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidlazio committed May 29, 2024
1 parent 87407c7 commit a30c315
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/spotify/confidence/Confidence.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public void track(String eventName) {
}

@Override
public void track(String eventName, ConfidenceValue.Struct message) {
public void track(String eventName, ConfidenceValue.Struct data) {
try {
client().emit(eventName, getContext(), Optional.of(message));
client().emit(eventName, getContext(), Optional.of(data));
} catch (IllegalStateException e) {
// swallow this exception
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/spotify/confidence/EventSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

public void track(String eventName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Optional;

interface EventSenderEngine extends Closeable {
void emit(String name, ConfidenceValue.Struct context, Optional<ConfidenceValue.Struct> message);
void emit(String name, ConfidenceValue.Struct context, Optional<ConfidenceValue.Struct> data);

void flush();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ class EventSenderEngineImpl implements EventSenderEngine {

@Override
public void emit(
String name, ConfidenceValue.Struct context, Optional<ConfidenceValue.Struct> message) {
String name, ConfidenceValue.Struct context, Optional<ConfidenceValue.Struct> data) {
if (intakeClosed) {
log.warn("EventSenderEngine is closed, dropping event {}", name);
return;
}
final Event event =
EventUploader.event(name, context, message).setEventTime(clock.getTimestamp()).build();
EventUploader.event(name, context, data).setEventTime(clock.getTimestamp()).build();
if (estimatedMemoryConsumption.get() + event.getSerializedSize() > maxMemoryConsumption) {
log.warn("EventSenderEngine is overloaded, dropping event {}", name);
return;
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/spotify/confidence/EventUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

import com.google.protobuf.Struct;
import com.spotify.confidence.events.v1.Event;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

interface EventUploader {
static Event.Builder event(
String name, ConfidenceValue.Struct context, Optional<ConfidenceValue.Struct> message) {
String name, ConfidenceValue.Struct context, Optional<ConfidenceValue.Struct> data) {
Map<String, ConfidenceValue> map = new HashMap<>(Map.of());
ConfidenceValue.Struct dataStruct = data.orElse(ConfidenceValue.Struct.EMPTY);
map.putAll(dataStruct.asMap());
map.put("context", context);

return Event.newBuilder()
.setEventDefinition(EventSenderEngineImpl.EVENT_NAME_PREFIX + name)
.setPayload(
Struct.newBuilder()
.putAllFields(context.asProtoMap())
.putAllFields(message.orElse(ConfidenceValue.Struct.EMPTY).asProtoMap()));
.setPayload(Struct.newBuilder().putAllFields(ConfidenceValue.of(map).asProtoMap()));
}

CompletableFuture<Boolean> upload(List<Event> events);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public void close() throws IOException {

@Override
public void emit(
String name, ConfidenceValue.Struct context, Optional<ConfidenceValue.Struct> message) {
events.add(event(name, context, message).setEventTime(clock.getTimestamp()).build());
String name, ConfidenceValue.Struct context, Optional<ConfidenceValue.Struct> data) {
events.add(event(name, context, data).setEventTime(clock.getTimestamp()).build());
}

@Override
Expand Down

0 comments on commit a30c315

Please sign in to comment.