Skip to content

Move serialization to LogEvent.getBody(). #201

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

Merged
merged 1 commit into from
Aug 1, 2018
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
18 changes: 13 additions & 5 deletions core-api/src/main/java/com/optimizely/ab/Optimizely.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,13 @@ private void sendImpression(@Nonnull ProjectConfig projectConfig,
userId,
filteredAttributes);
logger.info("Activating user \"{}\" in experiment \"{}\".", userId, experiment.getKey());
logger.debug(
"Dispatching impression event to URL {} with params {} and payload \"{}\".",
impressionEvent.getEndpointUrl(), impressionEvent.getRequestParams(), impressionEvent.getBody());

if (logger.isDebugEnabled()) {
logger.debug(
"Dispatching impression event to URL {} with params {} and payload \"{}\".",
impressionEvent.getEndpointUrl(), impressionEvent.getRequestParams(), impressionEvent.getBody());
}

try {
eventHandler.dispatchEvent(impressionEvent);
} catch (Exception e) {
Expand Down Expand Up @@ -294,8 +298,12 @@ public void track(@Nonnull String eventName,
}

logger.info("Tracking event \"{}\" for user \"{}\".", eventName, userId);
logger.debug("Dispatching conversion event to URL {} with params {} and payload \"{}\".",
conversionEvent.getEndpointUrl(), conversionEvent.getRequestParams(), conversionEvent.getBody());

if (logger.isDebugEnabled()) {
logger.debug("Dispatching conversion event to URL {} with params {} and payload \"{}\".",
conversionEvent.getEndpointUrl(), conversionEvent.getRequestParams(), conversionEvent.getBody());
}

try {
eventHandler.dispatchEvent(conversionEvent);
} catch (Exception e) {
Expand Down
19 changes: 14 additions & 5 deletions core-api/src/main/java/com/optimizely/ab/event/LogEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package com.optimizely.ab.event;

import com.optimizely.ab.event.internal.payload.EventBatch;
import com.optimizely.ab.event.internal.serializer.DefaultJsonSerializer;
import com.optimizely.ab.event.internal.serializer.Serializer;

import java.util.Map;

import javax.annotation.Nonnull;
Expand All @@ -30,16 +34,16 @@ public class LogEvent {
private final RequestMethod requestMethod;
private final String endpointUrl;
private final Map<String, String> requestParams;
private final String body;
private final EventBatch eventBatch;

public LogEvent(@Nonnull RequestMethod requestMethod,
@Nonnull String endpointUrl,
@Nonnull Map<String, String> requestParams,
@Nonnull String body) {
EventBatch eventBatch) {
this.requestMethod = requestMethod;
this.endpointUrl = endpointUrl;
this.requestParams = requestParams;
this.body = body;
this.eventBatch = eventBatch;
}

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

public String getBody() {
return body;
if (eventBatch == null) {
return "";
}

Serializer serializer = DefaultJsonSerializer.getInstance();
return serializer.serialize(eventBatch);
}

//======== Overriding method ========//
Expand All @@ -68,7 +77,7 @@ public String toString() {
"requestMethod=" + requestMethod +
", endpointUrl='" + endpointUrl + '\'' +
", requestParams=" + requestParams +
", body='" + body + '\'' +
", body='" + getBody() + '\'' +
'}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.optimizely.ab.event.internal.payload.Event;
import com.optimizely.ab.event.internal.payload.Snapshot;
import com.optimizely.ab.event.internal.payload.Visitor;
import com.optimizely.ab.event.internal.serializer.DefaultJsonSerializer;
import com.optimizely.ab.event.internal.serializer.Serializer;
import com.optimizely.ab.internal.EventTagUtils;
import com.optimizely.ab.internal.ControlAttribute;
import org.slf4j.Logger;
Expand All @@ -46,7 +44,6 @@ public class EventFactory {
static final String EVENT_ENDPOINT = "https://logx.optimizely.com/v1/events"; // Should be part of the datafile
static final String ACTIVATE_EVENT_KEY = "campaign_activated";

private Serializer serializer;
@VisibleForTesting
public final String clientVersion;
@VisibleForTesting
Expand All @@ -59,7 +56,6 @@ public EventFactory() {
public EventFactory(EventBatch.ClientEngine clientEngine, String clientVersion) {
this.clientEngine = clientEngine;
this.clientVersion = clientVersion;
this.serializer = DefaultJsonSerializer.getInstance();
}

public LogEvent createImpressionEvent(@Nonnull ProjectConfig projectConfig,
Expand Down Expand Up @@ -104,8 +100,7 @@ public LogEvent createImpressionEvent(@Nonnull ProjectConfig projectConfig,
.setRevision(projectConfig.getRevision())
.build();

String payload = this.serializer.serialize(eventBatch);
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), payload);
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), eventBatch);
}

public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig,
Expand Down Expand Up @@ -166,8 +161,7 @@ public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig,
.setRevision(projectConfig.getRevision())
.build();

String payload = this.serializer.serialize(eventBatch);
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), payload);
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), eventBatch);
}

private List<Attribute> buildAttributeList(ProjectConfig projectConfig, Map<String, String> attributes) {
Expand Down
Loading