Skip to content

Commit 2b29c60

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: add error code and clickstream error event (#37)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 088f89b commit 2b29c60

File tree

7 files changed

+236
-102
lines changed

7 files changed

+236
-102
lines changed

clickstream/src/main/java/software/aws/solution/clickstream/AWSClickstreamPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void recordEvent(@NonNull AnalyticsEventBehavior analyticsEvent) {
9898
final AnalyticsEvent clickstreamEvent =
9999
analyticsClient.createEvent(analyticsEvent.getName());
100100

101-
if (analyticsEvent.getProperties() != null) {
101+
if (clickstreamEvent != null && analyticsEvent.getProperties() != null) {
102102
for (Map.Entry<String, AnalyticsPropertyBehavior<?>> entry : analyticsEvent.getProperties()) {
103103
AnalyticsPropertyBehavior<?> property = entry.getValue();
104104
clickstreamEvent.addAttribute(entry.getKey(), property.getValue());

clickstream/src/main/java/software/aws/solution/clickstream/client/AnalyticsClient.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
*/
3434
public class AnalyticsClient {
3535
private static final Log LOG = LogFactory.getLog(AnalyticsClient.class);
36-
private static final int MAX_EVENT_TYPE_LENGTH = 50;
3736
private final ClickstreamContext context;
3837
private final Map<String, Object> globalAttributes = new ConcurrentHashMap<>();
3938
private JSONObject userAttributes;
@@ -67,9 +66,10 @@ public void addGlobalAttribute(String name, Object value) {
6766
if (value != null) {
6867
Event.EventError error = Event.checkAttribute(globalAttributes.size(), name, value);
6968
if (error != null) {
70-
if (!globalAttributes.containsKey(error.getErrorType())) {
71-
globalAttributes.put(error.getErrorType(), error.getErrorMessage());
72-
}
69+
final AnalyticsEvent event = createEvent(Event.PresetEvent.CLICKSTREAM_ERROR);
70+
event.addAttribute(Event.ReservedAttribute.ERROR_CODE, error.getErrorCode());
71+
event.addAttribute(Event.ReservedAttribute.ERROR_MESSAGE, error.getErrorMessage());
72+
recordEvent(event);
7373
return;
7474
}
7575
globalAttributes.put(name, value);
@@ -101,9 +101,10 @@ public void addUserAttribute(String name, Object value) {
101101
if (value != null) {
102102
Event.EventError error = Event.checkUserAttribute(userAttributes.length(), name, value);
103103
if (error != null) {
104-
if (!globalAttributes.containsKey(error.getErrorType())) {
105-
globalAttributes.put(error.getErrorType(), error.getErrorMessage());
106-
}
104+
final AnalyticsEvent event = createEvent(Event.PresetEvent.CLICKSTREAM_ERROR);
105+
event.addAttribute(Event.ReservedAttribute.ERROR_CODE, error.getErrorCode());
106+
event.addAttribute(Event.ReservedAttribute.ERROR_MESSAGE, error.getErrorMessage());
107+
recordEvent(event);
107108
return;
108109
}
109110
try {
@@ -166,16 +167,19 @@ public void updateUserAttribute() {
166167
* @throws IllegalArgumentException throws when fail to check the argument.
167168
*/
168169
public AnalyticsEvent createEvent(String eventType) {
169-
if (eventType.length() > MAX_EVENT_TYPE_LENGTH) {
170-
LOG.error("The event name is too long, the max event type length is " + MAX_EVENT_TYPE_LENGTH +
171-
" characters. event name:" + eventType);
172-
throw new IllegalArgumentException("The event name passed into create event was too long");
173-
}
174-
if (!Event.isValidName(eventType)) {
175-
LOG.error("Event name can only contains uppercase and lowercase letters, underscores, number, " +
176-
"and is not start with a number. event name:" + eventType);
177-
throw new IllegalArgumentException("The event name was not valid");
170+
Event.EventError error = Event.checkEventName(eventType);
171+
if (error != null) {
172+
LOG.error(error.getErrorMessage());
173+
AnalyticsEvent event = createAnalyticsEvent(Event.PresetEvent.CLICKSTREAM_ERROR);
174+
event.addAttribute(Event.ReservedAttribute.ERROR_CODE, error.getErrorCode());
175+
event.addAttribute(Event.ReservedAttribute.ERROR_MESSAGE, error.getErrorMessage());
176+
recordEvent(event);
177+
return null;
178178
}
179+
return createAnalyticsEvent(eventType);
180+
}
181+
182+
private AnalyticsEvent createAnalyticsEvent(String eventType) {
179183
long timestamp = System.currentTimeMillis();
180184
AnalyticsEvent event = new AnalyticsEvent(eventType, globalAttributes, userAttributes, timestamp, userUniqueId);
181185
event.setDeviceId(this.context.getDeviceId());

clickstream/src/main/java/software/aws/solution/clickstream/client/AnalyticsEvent.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,9 @@ public void addAttribute(final String name, final Object value) {
252252
Event.EventError attributeError = Event.checkAttribute(getCurrentNumOfAttributes(), name, value);
253253
try {
254254
if (attributeError != null) {
255-
if (!attributes.has(attributeError.getErrorType())) {
256-
attributes.putOpt(attributeError.getErrorType(), attributeError.getErrorMessage());
255+
if (!attributes.has(Event.ReservedAttribute.ERROR_CODE)) {
256+
attributes.putOpt(Event.ReservedAttribute.ERROR_CODE, attributeError.getErrorCode());
257+
attributes.putOpt(Event.ReservedAttribute.ERROR_MESSAGE, attributeError.getErrorMessage());
257258
}
258259
} else {
259260
attributes.putOpt(name, value);

clickstream/src/main/java/software/aws/solution/clickstream/client/Event.java

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ public final class Event {
3030
private Event() {
3131
}
3232

33+
/**
34+
* check the event type.
35+
*
36+
* @param eventName the event name
37+
* @return the EventError object
38+
*/
39+
public static EventError checkEventName(String eventName) {
40+
if (!isValidName(eventName)) {
41+
return new EventError(ErrorCode.EVENT_NAME_INVALID,
42+
"Event name can only contains uppercase and lowercase letters, " +
43+
"underscores, number, and is not start with a number. event name: " + eventName);
44+
} else if (eventName.length() > Limit.MAX_LENGTH_OF_NAME) {
45+
return new EventError(ErrorCode.EVENT_NAME_LENGTH_EXCEED,
46+
"Event name is too long, the max event type length is " +
47+
Limit.MAX_LENGTH_OF_NAME + "characters. event name: " + eventName);
48+
}
49+
return null;
50+
}
51+
3352
/**
3453
* check the attribute error.
3554
*
@@ -42,22 +61,22 @@ public static EventError checkAttribute(int currentNumber, String name, Object v
4261
if (currentNumber >= Limit.MAX_NUM_OF_ATTRIBUTES) {
4362
LOG.error("reached the max number of attributes limit ("
4463
+ Limit.MAX_NUM_OF_ATTRIBUTES + "). and the attribute: " + name + " will not be recorded");
45-
return new EventError(ErrorType.ATTRIBUTE_SIZE_EXCEED,
64+
return new EventError(ErrorCode.ATTRIBUTE_SIZE_EXCEED,
4665
StringUtil.clipString("attribute name: " + name, Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
4766
}
4867
if (name.length() > Limit.MAX_LENGTH_OF_NAME) {
4968
LOG.error("attribute : " + name + ", reached the max length of attributes name limit("
5069
+ Limit.MAX_LENGTH_OF_NAME + "). current length is:(" + name.length() +
5170
") and the attribute will not be recorded");
52-
return new EventError(ErrorType.ATTRIBUTE_NAME_LENGTH_EXCEED,
71+
return new EventError(ErrorCode.ATTRIBUTE_NAME_LENGTH_EXCEED,
5372
StringUtil.clipString("attribute name length is:(" + name.length() + ") name is:" + name,
5473
Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
5574
}
5675
if (!isValidName(name)) {
5776
LOG.error("attribute : " + name + ", was not valid, attribute name can only contains" +
5877
" uppercase and lowercase letters, underscores, number, and is not start with a number." +
5978
" so the attribute will not be recorded");
60-
return new EventError(ErrorType.ATTRIBUTE_NAME_INVALID,
79+
return new EventError(ErrorCode.ATTRIBUTE_NAME_INVALID,
6180
StringUtil.clipString(name, Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
6281
}
6382

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

71-
return new EventError(ErrorType.ATTRIBUTE_VALUE_LENGTH_EXCEED,
90+
return new EventError(ErrorCode.ATTRIBUTE_VALUE_LENGTH_EXCEED,
7291
StringUtil.clipString("attribute name:" + name + ", attribute value:" + value,
7392
Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
7493
}
@@ -88,14 +107,14 @@ public static EventError checkUserAttribute(int currentNumber, String name, Obje
88107
if (currentNumber >= Limit.MAX_NUM_OF_USER_ATTRIBUTES) {
89108
LOG.error("reached the max number of user attributes limit ("
90109
+ Limit.MAX_NUM_OF_USER_ATTRIBUTES + "). and the user attribute: " + name + " will not be recorded");
91-
return new EventError(ErrorType.ATTRIBUTE_SIZE_EXCEED,
110+
return new EventError(ErrorCode.USER_ATTRIBUTE_SIZE_EXCEED,
92111
StringUtil.clipString("attribute name: " + name, Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
93112
}
94113
if (name.length() > Limit.MAX_LENGTH_OF_NAME) {
95114
LOG.error("user attribute : " + name + ", reached the max length of attributes name limit("
96115
+ Limit.MAX_LENGTH_OF_NAME + "). current length is:(" + name.length() +
97116
") and the attribute will not be recorded");
98-
return new EventError(ErrorType.ATTRIBUTE_NAME_LENGTH_EXCEED,
117+
return new EventError(ErrorCode.USER_ATTRIBUTE_NAME_LENGTH_EXCEED,
99118
StringUtil.clipString("user attribute name length is:(" + name.length() + ") name is:" + name,
100119
Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
101120
}
@@ -106,7 +125,7 @@ public static EventError checkUserAttribute(int currentNumber, String name, Obje
106125
LOG.error("user attribute : " + name + ", was not valid, user attribute name can only contains" +
107126
" uppercase and lowercase letters, underscores, number, and is not start with a number." +
108127
" so the attribute will not be recorded");
109-
return new EventError(ErrorType.ATTRIBUTE_NAME_INVALID,
128+
return new EventError(ErrorCode.USER_ATTRIBUTE_NAME_INVALID,
110129
StringUtil.clipString(name, Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
111130
}
112131
if (value instanceof String) {
@@ -115,7 +134,7 @@ public static EventError checkUserAttribute(int currentNumber, String name, Obje
115134
LOG.error("user attribute : " + name + ", reached the max length of attributes value limit ("
116135
+ Limit.MAX_LENGTH_OF_USER_VALUE + "). current length is:(" + valueLength +
117136
"). and the attribute will not be recorded, attribute value:" + value);
118-
return new EventError(ErrorType.ATTRIBUTE_VALUE_LENGTH_EXCEED,
137+
return new EventError(ErrorCode.USER_ATTRIBUTE_VALUE_LENGTH_EXCEED,
119138
StringUtil.clipString("user attribute name:" + name + ", attribute value:" + value,
120139
Limit.MAX_LENGTH_OF_ERROR_VALUE, true));
121140
}
@@ -173,27 +192,63 @@ private Limit() {
173192
}
174193

175194
/**
176-
* event error type.
195+
* the event error code constants.
177196
*/
178-
public static final class ErrorType {
179-
private static final String ATTRIBUTE_NAME_INVALID = "_error_name_invalid";
180-
private static final String ATTRIBUTE_NAME_LENGTH_EXCEED = "_error_name_length_exceed";
181-
private static final String ATTRIBUTE_VALUE_LENGTH_EXCEED = "_error_value_length_exceed";
182-
private static final String ATTRIBUTE_SIZE_EXCEED = "_error_attribute_size_exceed";
197+
public static final class ErrorCode {
198+
/**
199+
* error code for event name invalid.
200+
*/
201+
public static final int EVENT_NAME_INVALID = 1001;
202+
/**
203+
* error code for event name length exceed the limit.
204+
*/
205+
public static final int EVENT_NAME_LENGTH_EXCEED = 1002;
206+
/**
207+
* error code for attribute name length exceed.
208+
*/
209+
public static final int ATTRIBUTE_NAME_LENGTH_EXCEED = 2001;
210+
/**
211+
* error code for attribute name invalid.
212+
*/
213+
public static final int ATTRIBUTE_NAME_INVALID = 2002;
214+
/**
215+
* error code for attribute value length exceed.
216+
*/
217+
public static final int ATTRIBUTE_VALUE_LENGTH_EXCEED = 2003;
218+
/**
219+
* error code for attribute size exceed.
220+
*/
221+
public static final int ATTRIBUTE_SIZE_EXCEED = 2004;
222+
/**
223+
* error code for user attribute size exceed.
224+
*/
225+
public static final int USER_ATTRIBUTE_SIZE_EXCEED = 3001;
226+
/**
227+
* error code for user attribute name length exceed.
228+
*/
229+
public static final int USER_ATTRIBUTE_NAME_LENGTH_EXCEED = 3002;
230+
/**
231+
* error code for user user attribute name invalid.
232+
*/
233+
public static final int USER_ATTRIBUTE_NAME_INVALID = 3003;
234+
/**
235+
* error code for user attribute value length exceed.
236+
*/
237+
public static final int USER_ATTRIBUTE_VALUE_LENGTH_EXCEED = 3004;
183238

184-
private ErrorType() {
239+
private ErrorCode() {
185240
}
186241
}
187242

188243
/**
189244
* Event for return.
190245
*/
191246
public static class EventError {
192-
private final String errorType;
247+
private final int errorCode;
193248
private final String errorMessage;
194249

195-
EventError(String errorType, String errorMessage) {
196-
this.errorType = errorType;
250+
EventError(int errorType, String errorMessage) {
251+
this.errorCode = errorType;
197252
this.errorMessage = errorMessage;
198253
}
199254

@@ -202,8 +257,8 @@ public static class EventError {
202257
*
203258
* @return error type
204259
*/
205-
public String getErrorType() {
206-
return errorType;
260+
public int getErrorCode() {
261+
return errorCode;
207262
}
208263

209264
/**
@@ -287,6 +342,14 @@ public static final class ReservedAttribute {
287342
* is the first time attribute.
288343
*/
289344
public static final String IS_FIRST_TIME = "_is_first_time";
345+
/**
346+
* is the error code attribute.
347+
*/
348+
public static final String ERROR_CODE = "_error_code";
349+
/**
350+
* is the error message attribute.
351+
*/
352+
public static final String ERROR_MESSAGE = "_error_message";
290353

291354
private ReservedAttribute() {
292355
}
@@ -346,6 +409,11 @@ public static final class PresetEvent {
346409
*/
347410
public static final String PROFILE_SET = "_profile_set";
348411

412+
/**
413+
* clickstream error event.
414+
*/
415+
public static final String CLICKSTREAM_ERROR = "_clickstream_error";
416+
349417
private PresetEvent() {
350418
}
351419
}

0 commit comments

Comments
 (0)