Skip to content

Commit

Permalink
feat: add error code and clickstream error event
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoweii authored and zxkane committed Sep 12, 2023
1 parent 088f89b commit ed2ff40
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void recordEvent(@NonNull AnalyticsEventBehavior analyticsEvent) {
final AnalyticsEvent clickstreamEvent =
analyticsClient.createEvent(analyticsEvent.getName());

if (analyticsEvent.getProperties() != null) {
if (clickstreamEvent != null && analyticsEvent.getProperties() != null) {
for (Map.Entry<String, AnalyticsPropertyBehavior<?>> entry : analyticsEvent.getProperties()) {
AnalyticsPropertyBehavior<?> property = entry.getValue();
clickstreamEvent.addAttribute(entry.getKey(), property.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
*/
public class AnalyticsClient {
private static final Log LOG = LogFactory.getLog(AnalyticsClient.class);
private static final int MAX_EVENT_TYPE_LENGTH = 50;
private final ClickstreamContext context;
private final Map<String, Object> globalAttributes = new ConcurrentHashMap<>();
private JSONObject userAttributes;
Expand Down Expand Up @@ -67,9 +66,10 @@ public void addGlobalAttribute(String name, Object value) {
if (value != null) {
Event.EventError error = Event.checkAttribute(globalAttributes.size(), name, value);
if (error != null) {
if (!globalAttributes.containsKey(error.getErrorType())) {
globalAttributes.put(error.getErrorType(), error.getErrorMessage());
}
final AnalyticsEvent event = createEvent(Event.PresetEvent.CLICKSTREAM_ERROR);
event.addAttribute(Event.ReservedAttribute.ERROR_CODE, error.getErrorCode());
event.addAttribute(Event.ReservedAttribute.ERROR_MESSAGE, error.getErrorMessage());
recordEvent(event);
return;
}
globalAttributes.put(name, value);
Expand Down Expand Up @@ -101,9 +101,10 @@ public void addUserAttribute(String name, Object value) {
if (value != null) {
Event.EventError error = Event.checkUserAttribute(userAttributes.length(), name, value);
if (error != null) {
if (!globalAttributes.containsKey(error.getErrorType())) {
globalAttributes.put(error.getErrorType(), error.getErrorMessage());
}
final AnalyticsEvent event = createEvent(Event.PresetEvent.CLICKSTREAM_ERROR);
event.addAttribute(Event.ReservedAttribute.ERROR_CODE, error.getErrorCode());
event.addAttribute(Event.ReservedAttribute.ERROR_MESSAGE, error.getErrorMessage());
recordEvent(event);
return;
}
try {
Expand Down Expand Up @@ -166,16 +167,19 @@ public void updateUserAttribute() {
* @throws IllegalArgumentException throws when fail to check the argument.
*/
public AnalyticsEvent createEvent(String eventType) {
if (eventType.length() > MAX_EVENT_TYPE_LENGTH) {
LOG.error("The event name is too long, the max event type length is " + MAX_EVENT_TYPE_LENGTH +
" characters. event name:" + eventType);
throw new IllegalArgumentException("The event name passed into create event was too long");
}
if (!Event.isValidName(eventType)) {
LOG.error("Event name can only contains uppercase and lowercase letters, underscores, number, " +
"and is not start with a number. event name:" + eventType);
throw new IllegalArgumentException("The event name was not valid");
Event.EventError error = Event.checkEventName(eventType);
if (error != null) {
LOG.error(error.getErrorMessage());
AnalyticsEvent event = createAnalyticsEvent(Event.PresetEvent.CLICKSTREAM_ERROR);
event.addAttribute(Event.ReservedAttribute.ERROR_CODE, error.getErrorCode());
event.addAttribute(Event.ReservedAttribute.ERROR_MESSAGE, error.getErrorMessage());
recordEvent(event);
return null;
}
return createAnalyticsEvent(eventType);
}

private AnalyticsEvent createAnalyticsEvent(String eventType) {
long timestamp = System.currentTimeMillis();
AnalyticsEvent event = new AnalyticsEvent(eventType, globalAttributes, userAttributes, timestamp, userUniqueId);
event.setDeviceId(this.context.getDeviceId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ public void addAttribute(final String name, final Object value) {
Event.EventError attributeError = Event.checkAttribute(getCurrentNumOfAttributes(), name, value);
try {
if (attributeError != null) {
if (!attributes.has(attributeError.getErrorType())) {
attributes.putOpt(attributeError.getErrorType(), attributeError.getErrorMessage());
if (!attributes.has(Event.ReservedAttribute.ERROR_CODE)) {
attributes.putOpt(Event.ReservedAttribute.ERROR_CODE, attributeError.getErrorCode());
attributes.putOpt(Event.ReservedAttribute.ERROR_MESSAGE, attributeError.getErrorMessage());
}
} else {
attributes.putOpt(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ public final class Event {
private Event() {
}

/**
* check the event type.
*
* @param eventName the event name
* @return the EventError object
*/
public static EventError checkEventName(String eventName) {
if (!isValidName(eventName)) {
return new EventError(ErrorCode.EVENT_NAME_INVALID,
"Event name can only contains uppercase and lowercase letters, " +
"underscores, number, and is not start with a number. event name: " + eventName);
} else if (eventName.length() > Limit.MAX_LENGTH_OF_NAME) {
return new EventError(ErrorCode.EVENT_NAME_LENGTH_EXCEED,
"Event name is too long, the max event type length is " +
Limit.MAX_LENGTH_OF_NAME + "characters. event name: " + eventName);
}
return null;
}

/**
* check the attribute error.
*
Expand All @@ -42,22 +61,22 @@ public static EventError checkAttribute(int currentNumber, String name, Object v
if (currentNumber >= Limit.MAX_NUM_OF_ATTRIBUTES) {
LOG.error("reached the max number of attributes limit ("
+ Limit.MAX_NUM_OF_ATTRIBUTES + "). and the attribute: " + name + " will not be recorded");
return new EventError(ErrorType.ATTRIBUTE_SIZE_EXCEED,
return new EventError(ErrorCode.ATTRIBUTE_SIZE_EXCEED,
StringUtil.clipString("attribute name: " + name, Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
}
if (name.length() > Limit.MAX_LENGTH_OF_NAME) {
LOG.error("attribute : " + name + ", reached the max length of attributes name limit("
+ Limit.MAX_LENGTH_OF_NAME + "). current length is:(" + name.length() +
") and the attribute will not be recorded");
return new EventError(ErrorType.ATTRIBUTE_NAME_LENGTH_EXCEED,
return new EventError(ErrorCode.ATTRIBUTE_NAME_LENGTH_EXCEED,
StringUtil.clipString("attribute name length is:(" + name.length() + ") name is:" + name,
Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
}
if (!isValidName(name)) {
LOG.error("attribute : " + name + ", was not valid, attribute name can only contains" +
" uppercase and lowercase letters, underscores, number, and is not start with a number." +
" so the attribute will not be recorded");
return new EventError(ErrorType.ATTRIBUTE_NAME_INVALID,
return new EventError(ErrorCode.ATTRIBUTE_NAME_INVALID,
StringUtil.clipString(name, Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
}

Expand All @@ -68,7 +87,7 @@ public static EventError checkAttribute(int currentNumber, String name, Object v
+ Limit.MAX_LENGTH_OF_VALUE + "). current length is:(" + valueLength +
"). and the attribute will not be recorded, attribute value:" + value);

return new EventError(ErrorType.ATTRIBUTE_VALUE_LENGTH_EXCEED,
return new EventError(ErrorCode.ATTRIBUTE_VALUE_LENGTH_EXCEED,
StringUtil.clipString("attribute name:" + name + ", attribute value:" + value,
Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
}
Expand All @@ -88,14 +107,14 @@ public static EventError checkUserAttribute(int currentNumber, String name, Obje
if (currentNumber >= Limit.MAX_NUM_OF_USER_ATTRIBUTES) {
LOG.error("reached the max number of user attributes limit ("
+ Limit.MAX_NUM_OF_USER_ATTRIBUTES + "). and the user attribute: " + name + " will not be recorded");
return new EventError(ErrorType.ATTRIBUTE_SIZE_EXCEED,
return new EventError(ErrorCode.USER_ATTRIBUTE_SIZE_EXCEED,
StringUtil.clipString("attribute name: " + name, Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
}
if (name.length() > Limit.MAX_LENGTH_OF_NAME) {
LOG.error("user attribute : " + name + ", reached the max length of attributes name limit("
+ Limit.MAX_LENGTH_OF_NAME + "). current length is:(" + name.length() +
") and the attribute will not be recorded");
return new EventError(ErrorType.ATTRIBUTE_NAME_LENGTH_EXCEED,
return new EventError(ErrorCode.USER_ATTRIBUTE_NAME_LENGTH_EXCEED,
StringUtil.clipString("user attribute name length is:(" + name.length() + ") name is:" + name,
Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
}
Expand All @@ -106,7 +125,7 @@ public static EventError checkUserAttribute(int currentNumber, String name, Obje
LOG.error("user attribute : " + name + ", was not valid, user attribute name can only contains" +
" uppercase and lowercase letters, underscores, number, and is not start with a number." +
" so the attribute will not be recorded");
return new EventError(ErrorType.ATTRIBUTE_NAME_INVALID,
return new EventError(ErrorCode.USER_ATTRIBUTE_NAME_INVALID,
StringUtil.clipString(name, Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
}
if (value instanceof String) {
Expand All @@ -115,7 +134,7 @@ public static EventError checkUserAttribute(int currentNumber, String name, Obje
LOG.error("user attribute : " + name + ", reached the max length of attributes value limit ("
+ Limit.MAX_LENGTH_OF_USER_VALUE + "). current length is:(" + valueLength +
"). and the attribute will not be recorded, attribute value:" + value);
return new EventError(ErrorType.ATTRIBUTE_VALUE_LENGTH_EXCEED,
return new EventError(ErrorCode.USER_ATTRIBUTE_VALUE_LENGTH_EXCEED,
StringUtil.clipString("user attribute name:" + name + ", attribute value:" + value,
Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
}
Expand Down Expand Up @@ -173,27 +192,63 @@ private Limit() {
}

/**
* event error type.
* the event error code constants.
*/
public static final class ErrorType {
private static final String ATTRIBUTE_NAME_INVALID = "_error_name_invalid";
private static final String ATTRIBUTE_NAME_LENGTH_EXCEED = "_error_name_length_exceed";
private static final String ATTRIBUTE_VALUE_LENGTH_EXCEED = "_error_value_length_exceed";
private static final String ATTRIBUTE_SIZE_EXCEED = "_error_attribute_size_exceed";
public static final class ErrorCode {
/**
* error code for event name invalid.
*/
public static final int EVENT_NAME_INVALID = 1001;
/**
* error code for event name length exceed the limit.
*/
public static final int EVENT_NAME_LENGTH_EXCEED = 1002;
/**
* error code for attribute name length exceed.
*/
public static final int ATTRIBUTE_NAME_LENGTH_EXCEED = 2001;
/**
* error code for attribute name invalid.
*/
public static final int ATTRIBUTE_NAME_INVALID = 2002;
/**
* error code for attribute value length exceed.
*/
public static final int ATTRIBUTE_VALUE_LENGTH_EXCEED = 2003;
/**
* error code for attribute size exceed.
*/
public static final int ATTRIBUTE_SIZE_EXCEED = 2004;
/**
* error code for user attribute size exceed.
*/
public static final int USER_ATTRIBUTE_SIZE_EXCEED = 3001;
/**
* error code for user attribute name length exceed.
*/
public static final int USER_ATTRIBUTE_NAME_LENGTH_EXCEED = 3002;
/**
* error code for user user attribute name invalid.
*/
public static final int USER_ATTRIBUTE_NAME_INVALID = 3003;
/**
* error code for user attribute value length exceed.
*/
public static final int USER_ATTRIBUTE_VALUE_LENGTH_EXCEED = 3004;

private ErrorType() {
private ErrorCode() {
}
}

/**
* Event for return.
*/
public static class EventError {
private final String errorType;
private final int errorCode;
private final String errorMessage;

EventError(String errorType, String errorMessage) {
this.errorType = errorType;
EventError(int errorType, String errorMessage) {
this.errorCode = errorType;
this.errorMessage = errorMessage;
}

Expand All @@ -202,8 +257,8 @@ public static class EventError {
*
* @return error type
*/
public String getErrorType() {
return errorType;
public int getErrorCode() {
return errorCode;
}

/**
Expand Down Expand Up @@ -287,6 +342,14 @@ public static final class ReservedAttribute {
* is the first time attribute.
*/
public static final String IS_FIRST_TIME = "_is_first_time";
/**
* is the error code attribute.
*/
public static final String ERROR_CODE = "_error_code";
/**
* is the error message attribute.
*/
public static final String ERROR_MESSAGE = "_error_message";

private ReservedAttribute() {
}
Expand Down Expand Up @@ -346,6 +409,11 @@ public static final class PresetEvent {
*/
public static final String PROFILE_SET = "_profile_set";

/**
* clickstream error event.
*/
public static final String CLICKSTREAM_ERROR = "_clickstream_error";

private PresetEvent() {
}
}
Expand Down
Loading

0 comments on commit ed2ff40

Please sign in to comment.