Skip to content

Commit 199b0dc

Browse files
author
Mike Davis
authored
Move serialization to LogEvent.getBody(). (#201)
This PR moves the serialization from POJO to JSON into LogEvent#getBody(). This allows the serialization of the event to be offloaded to with the event handler when the HTTP request is built. This should be 100% backwards compatible with any existing EventHandler implementations. Initial benchmarks are much improved over the current master in tight loop simulations.
1 parent 714ef5a commit 199b0dc

File tree

5 files changed

+46
-101
lines changed

5 files changed

+46
-101
lines changed

core-api/src/main/java/com/optimizely/ab/Optimizely.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,13 @@ private void sendImpression(@Nonnull ProjectConfig projectConfig,
199199
userId,
200200
filteredAttributes);
201201
logger.info("Activating user \"{}\" in experiment \"{}\".", userId, experiment.getKey());
202-
logger.debug(
203-
"Dispatching impression event to URL {} with params {} and payload \"{}\".",
204-
impressionEvent.getEndpointUrl(), impressionEvent.getRequestParams(), impressionEvent.getBody());
202+
203+
if (logger.isDebugEnabled()) {
204+
logger.debug(
205+
"Dispatching impression event to URL {} with params {} and payload \"{}\".",
206+
impressionEvent.getEndpointUrl(), impressionEvent.getRequestParams(), impressionEvent.getBody());
207+
}
208+
205209
try {
206210
eventHandler.dispatchEvent(impressionEvent);
207211
} catch (Exception e) {
@@ -294,8 +298,12 @@ public void track(@Nonnull String eventName,
294298
}
295299

296300
logger.info("Tracking event \"{}\" for user \"{}\".", eventName, userId);
297-
logger.debug("Dispatching conversion event to URL {} with params {} and payload \"{}\".",
298-
conversionEvent.getEndpointUrl(), conversionEvent.getRequestParams(), conversionEvent.getBody());
301+
302+
if (logger.isDebugEnabled()) {
303+
logger.debug("Dispatching conversion event to URL {} with params {} and payload \"{}\".",
304+
conversionEvent.getEndpointUrl(), conversionEvent.getRequestParams(), conversionEvent.getBody());
305+
}
306+
299307
try {
300308
eventHandler.dispatchEvent(conversionEvent);
301309
} catch (Exception e) {

core-api/src/main/java/com/optimizely/ab/event/LogEvent.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
*/
1717
package com.optimizely.ab.event;
1818

19+
import com.optimizely.ab.event.internal.payload.EventBatch;
20+
import com.optimizely.ab.event.internal.serializer.DefaultJsonSerializer;
21+
import com.optimizely.ab.event.internal.serializer.Serializer;
22+
1923
import java.util.Map;
2024

2125
import javax.annotation.Nonnull;
@@ -30,16 +34,16 @@ public class LogEvent {
3034
private final RequestMethod requestMethod;
3135
private final String endpointUrl;
3236
private final Map<String, String> requestParams;
33-
private final String body;
37+
private final EventBatch eventBatch;
3438

3539
public LogEvent(@Nonnull RequestMethod requestMethod,
3640
@Nonnull String endpointUrl,
3741
@Nonnull Map<String, String> requestParams,
38-
@Nonnull String body) {
42+
EventBatch eventBatch) {
3943
this.requestMethod = requestMethod;
4044
this.endpointUrl = endpointUrl;
4145
this.requestParams = requestParams;
42-
this.body = body;
46+
this.eventBatch = eventBatch;
4347
}
4448

4549
//======== Getters ========//
@@ -57,7 +61,12 @@ public Map<String, String> getRequestParams() {
5761
}
5862

5963
public String getBody() {
60-
return body;
64+
if (eventBatch == null) {
65+
return "";
66+
}
67+
68+
Serializer serializer = DefaultJsonSerializer.getInstance();
69+
return serializer.serialize(eventBatch);
6170
}
6271

6372
//======== Overriding method ========//
@@ -68,7 +77,7 @@ public String toString() {
6877
"requestMethod=" + requestMethod +
6978
", endpointUrl='" + endpointUrl + '\'' +
7079
", requestParams=" + requestParams +
71-
", body='" + body + '\'' +
80+
", body='" + getBody() + '\'' +
7281
'}';
7382
}
7483

core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import com.optimizely.ab.event.internal.payload.Event;
2929
import com.optimizely.ab.event.internal.payload.Snapshot;
3030
import com.optimizely.ab.event.internal.payload.Visitor;
31-
import com.optimizely.ab.event.internal.serializer.DefaultJsonSerializer;
32-
import com.optimizely.ab.event.internal.serializer.Serializer;
3331
import com.optimizely.ab.internal.EventTagUtils;
3432
import com.optimizely.ab.internal.ControlAttribute;
3533
import org.slf4j.Logger;
@@ -46,7 +44,6 @@ public class EventFactory {
4644
static final String EVENT_ENDPOINT = "https://logx.optimizely.com/v1/events"; // Should be part of the datafile
4745
static final String ACTIVATE_EVENT_KEY = "campaign_activated";
4846

49-
private Serializer serializer;
5047
@VisibleForTesting
5148
public final String clientVersion;
5249
@VisibleForTesting
@@ -59,7 +56,6 @@ public EventFactory() {
5956
public EventFactory(EventBatch.ClientEngine clientEngine, String clientVersion) {
6057
this.clientEngine = clientEngine;
6158
this.clientVersion = clientVersion;
62-
this.serializer = DefaultJsonSerializer.getInstance();
6359
}
6460

6561
public LogEvent createImpressionEvent(@Nonnull ProjectConfig projectConfig,
@@ -104,8 +100,7 @@ public LogEvent createImpressionEvent(@Nonnull ProjectConfig projectConfig,
104100
.setRevision(projectConfig.getRevision())
105101
.build();
106102

107-
String payload = this.serializer.serialize(eventBatch);
108-
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), payload);
103+
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), eventBatch);
109104
}
110105

111106
public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig,
@@ -166,8 +161,7 @@ public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig,
166161
.setRevision(projectConfig.getRevision())
167162
.build();
168163

169-
String payload = this.serializer.serialize(eventBatch);
170-
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), payload);
164+
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), eventBatch);
171165
}
172166

173167
private List<Attribute> buildAttributeList(ProjectConfig projectConfig, Map<String, String> attributes) {

0 commit comments

Comments
 (0)