-
Notifications
You must be signed in to change notification settings - Fork 172
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
Added default timestamp to JacksonEvent #943
Conversation
@@ -79,6 +87,9 @@ protected JacksonEvent(final Builder builder) { | |||
} | |||
|
|||
this.jsonNode = getInitialJsonNode(builder.data); | |||
|
|||
zonedDateTime = ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to point out that I used DateTimeFormatter
here because if we don't have it trailing zeros will be removed from timestamp during serialization as seen in below example. And it will return String
type instead of ZonedDateTime
object.
expected :{\"event_timestamp\":\"2022-01-26T13:35:32.270-06:00\"}
Actual :{\"event_timestamp\":\"2022-01-26T13:35:32.27-06:00\"}
@@ -66,6 +72,8 @@ | |||
|
|||
static final String EVENT_TYPE = "event"; | |||
|
|||
private static String zonedDateTime; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't need this field.
@@ -88,6 +99,10 @@ static Event fromMessage(String message) { | |||
.build(); | |||
} | |||
|
|||
static String getZonedDateTime() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe that we want this.
There could be value in having a getEventTimestamp()
field on Event
. But, that would be different. And I believe that it should pull from the Map, not have a duplicated field.
} | ||
|
||
@Test | ||
public void testGetAsMap_with_EmptyData() { | ||
final Map<String, Object> eventAsMap = event.toMap(); | ||
assertThat(eventAsMap, equalTo(Collections.emptyMap())); | ||
|
||
final Map<String, Object> mapObject = new HashMap<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably rename this to something like expectedMap
.
assertThat(eventAsMap, equalTo(Collections.emptyMap())); | ||
|
||
final Map<String, Object> mapObject = new HashMap<>(); | ||
mapObject.put("event_timestamp", JacksonEvent.getZonedDateTime()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should create your ZonedDateTime in this test. It can be in either the setUp or in this method. You don't need that static method on JacksonEvent
.
@@ -299,7 +301,7 @@ public void testKey_withNullKey_throwsNullPointerException() { | |||
public void testToString_withEmptyData() { | |||
final String result = event.toJsonString(); | |||
|
|||
assertThat(result, is(equalTo("{}"))); | |||
assertThat(result, is(equalTo("{\"event_timestamp\":\"" + JacksonEvent.getZonedDateTime() + "\"}"))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd call get("event_timestamp")
on the new Event.
Then you can compare against that instead of this static method.
@@ -310,13 +312,21 @@ public void testToString_withSimpleObject() { | |||
event.put("list", Arrays.asList(1, 4, 5)); | |||
final String result = event.toJsonString(); | |||
|
|||
assertThat(result, is(equalTo(String.format("{\"foo\":\"bar\",\"testObject\":{\"field1\":\"%s\"},\"list\":[1,4,5]}", value)))); | |||
String expectedResult = "{\"event_timestamp\":\"" + JacksonEvent.getZonedDateTime() + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar comment on calling get("event_timestamp")
and comparing against that.
I think there is an issue with either Jackson or Java Date Time according to this open issue. When we serialize a Date Time object, trailing zeros in milliseconds will be trimmed and So whenever we try to use get method to compare json string or map asserts will fail in unit tests. The workaround for this is to use |
That is a disappointing bug. Using An alternative is to change the tests to not test for string equality. Instead you can compare two Here is an example:
|
Please link to the issue in the PR description. |
@Test | ||
public void testTimestamp_isNotNullAndValid() { | ||
Assertions.assertNotNull(timestamp); | ||
Assertions.assertTrue(Duration.between(timestamp, OffsetDateTime.now()).getSeconds() < 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there ever a chance that all the tests take more than one second to run before this assertion is made? If so this could result in a flaky test. It may make more sense to make the timestamp here right before the assertion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. But, the timestamp
is not created separately, we are getting it from the event which will be created for each test. So technically it shouldn't take more than 1 second.
I'll try to increase it to 5 seconds just so that test won't be flaky.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is another reason why I like createObjectUnderTest()
methods instead of creating it in setUp()
.
Instant before = Instant.now();
Event objectUnderTest = createObjectUnderTest();
Instant after = Instant.now();
assertThat(objectUnderTest.get("event_timestamp"), lessThanOrEqualTo(after));
assertThat(objectUnderTest.get("event_timestamp"), greaterThanOrEqualTo(before));
The build is failing due to some tests in the |
Signed-off-by: Asif Sohail Mohammed <nsifmoh@amazon.com>
Signed-off-by: Asif Sohail Mohammed <nsifmoh@amazon.com>
Signed-off-by: Asif Sohail Mohammed <nsifmoh@amazon.com>
9191fb5
to
b9c0d09
Compare
Codecov Report
@@ Coverage Diff @@
## main #943 +/- ##
============================================
+ Coverage 91.40% 91.44% +0.03%
- Complexity 673 675 +2
============================================
Files 83 84 +1
Lines 1955 1964 +9
Branches 165 165
============================================
+ Hits 1787 1796 +9
Misses 129 129
Partials 39 39
Continue to review full report at Codecov.
|
b9c0d09
to
e03be1a
Compare
Signed-off-by: Asif Sohail Mohammed <nsifmoh@amazon.com>
e03be1a
to
8b2ad58
Compare
@@ -79,6 +89,7 @@ protected JacksonEvent(final Builder builder) { | |||
} | |||
|
|||
this.jsonNode = getInitialJsonNode(builder.data); | |||
put(TIMESTAMP_KEY, ZonedDateTime.now()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the user experience if the @timestamp
already existed in the event? It looks like the exsiting @timestamp
be overwritten.
I think we should update the JacksonEvent docs to indicate a @timestamp
field will be added to the object and the expected default behavior for an existing value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update the Java docs for JacksonEvent
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if overwriting the field is the correct thing to do here. This has the potential to erase customer data and impact peer forwarding.
Signed-off-by: Asif Sohail Mohammed <nsifmoh@amazon.com>
Description
@timestamp
key to JacksonEvent wtih a custom Serializer forZonedDateTime
Issue related to Serializaing DateTIme using Jackson
FasterXML/jackson-modules-java8#76
Issues Resolved
#930
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.