Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -130,6 +131,39 @@ private static Visitor createVisitor(ImpressionEvent impressionEvent) {
.build();
}

/**
* Event tags holding non-finite numbers (NaN, Infinity) can not be represented in JSON and make
* strict serializers throw for the whole batch, so they are left out of the event.
*
* @param tags The event tags
* @return The event tags without non-finite numeric values
*/
private static Map<String, ?> removeNonFiniteTags(Map<String, ?> tags) {
if (tags == null) {
return null;
}

Map<String, Object> filteredTags = null;
for (Map.Entry<String, ?> entry : tags.entrySet()) {
Object value = entry.getValue();
if (!(value instanceof Double || value instanceof Float)) {
continue;
}

double doubleValue = ((Number) value).doubleValue();
if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
logger.warn("Event tag \"{}\" has a non-finite value \"{}\" and will be omitted from the event.",
entry.getKey(), value);
if (filteredTags == null) {
filteredTags = new HashMap<String, Object>(tags);
}
filteredTags.remove(entry.getKey());
}
}

return filteredTags == null ? tags : filteredTags;
}

private static Visitor createVisitor(ConversionEvent conversionEvent) {
if (conversionEvent == null) {
return null;
Expand All @@ -143,7 +177,7 @@ private static Visitor createVisitor(ConversionEvent conversionEvent) {
.setEntityId(conversionEvent.getEventId())
.setKey(conversionEvent.getEventKey())
.setRevenue(conversionEvent.getRevenue())
.setTags(conversionEvent.getTags())
.setTags(removeNonFiniteTags(conversionEvent.getTags()))
.setType(conversionEvent.getEventKey())
.setValue(conversionEvent.getValue())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,47 @@ public static LogEvent createImpressionEvent(ProjectConfig projectConfig,

}

/**
* Verify that event tags holding non-finite numbers (NaN, Infinity) are omitted from the
* conversion event, since they have no JSON representation and make strict serializers
* (Gson, org.json) throw for the whole batch.
*/
@Test
public void createConversionEventOmitsNonFiniteEventTags() throws Exception {
EventType eventType = validProjectConfig.getEventTypes().get(0);

Map<String, Object> eventTagMap = new HashMap<String, Object>();
eventTagMap.put("string_param", "123");
eventTagMap.put("double_param", 1.5);
eventTagMap.put("nan_param", Double.NaN);
eventTagMap.put("infinite_param", Double.POSITIVE_INFINITY);
eventTagMap.put("negative_infinite_param", Float.NEGATIVE_INFINITY);

LogEvent conversionEvent = createConversionEvent(
validProjectConfig,
"userId",
eventType.getId(),
eventType.getKey(),
Collections.<String, String>emptyMap(),
eventTagMap);

Map<String, Object> expectedTags = new HashMap<String, Object>();
expectedTags.put("string_param", "123");
expectedTags.put("double_param", 1.5);

Map<String, ?> tags = conversionEvent.getEventBatch()
.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTags();
assertEquals(expectedTags, tags);

// the caller's map must not be modified
assertEquals(5, eventTagMap.size());

// Gson rejects NaN and Infinity by default, so this used to throw IllegalArgumentException
String body = new Gson().toJson(conversionEvent.getEventBatch());
assertTrue(body.contains("double_param"));
assertFalse(body.contains("nan_param"));
}

/**
* Helper method for generating a conversion based LogEvent.
*/
Expand Down