Skip to content
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 JavaTimeModule for entity object mapper #267

Merged
merged 5 commits into from
Mar 24, 2021

Conversation

willarmiros
Copy link
Contributor

Description of changes:
Adds a JavaTimeModule to our ObjectMapper for serializing entities, so that we can support Duration and other Time-based attributes being added to Metadata. We had a customer feature request for this

@anuraaga I know the tests will fail for this with ClassDefNotFoundErrors, I wasn't sure how to resolve this without just adding a test dependency in a bunch of modules on the new com.fasterxml.jackson.datatype:jackson-datatype-jsr310 dependency.

I'm also curious to hear if you think this is even a fair expectation of the SDK, or if we should just require customers to cast their timestamps to primitives before adding it as metadata.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@willarmiros willarmiros requested a review from anuraaga as a code owner March 22, 2021 22:17
Copy link
Contributor

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would definitely be nice if our API had strict types like OTel but given we accept Object everywhere the expectations are challenging. That being said, we can't support any possible type naturally. Supporting these sort of primitives seems ok to me though, we need to add unit tests probably using test-sets to add the dependency.

@@ -57,6 +58,7 @@
@SuppressWarnings("checkstyle:ConstantName")
@Deprecated
protected static final ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we can use the autoFindModule thing that object mapper has to deal with this situation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you confirm the values are formatted correctly? I think JavaTimeModule uses ISO format IIRC, which I don't think we want in x-ray model.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we can use the autoFindModule thing that object mapper has to deal with this situation

Yeah that would actually be better, then we don't have to add the dependency ourselves. I thought it looked too magic at first, but now I see the benefits.

Can you confirm the values are formatted correctly?

Yeah sorry forgot to mention I tested on a sample app and they work as expected.

@willarmiros
Copy link
Contributor Author

@anuraaga addressed comments

@Test
void testTimeModuleSerialization() {
Segment seg = new SegmentImpl(AWSXRay.getGlobalRecorder(),"test");
Duration duration = Duration.ofSeconds(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do some test cases for smaller and larger values too? And also timestamps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!

@Test
void testTimestampSerialization() {
Segment seg = new SegmentImpl(AWSXRay.getGlobalRecorder(), "test");
seg.putMetadata("date", Date.from(Instant.ofEpochSecond(1616559298)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the module allow serializing Instant without converting to Date?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, wasn't thinking. Changed to Instant

seg.putMetadata("date", Date.from(Instant.ofEpochSecond(1616559298)));
String serializedSeg = seg.serialize();

String expected = "{\"default\":{\"date\":1616559298000}}";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is a millisecond int but usually we serialize times as second double. Is this format ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh yeah Instant serializes to a seconds double but Date doesn't. All I found was the default behavior of Jackson is to serialize Dates as timestamps rather than ISO strings, but didn't explain why they are int instead of double: https://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/SerializationFeature.html#WRITE_DATES_AS_TIMESTAMPS

I guess maybe because Dates are only accurate to the nearest millisecond? First line of Javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a separate test for Date if you want, to indicate the int format is expected

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah let's go ahead and add a test for Date as well at least for documentation. This PR didn't change it's behavior anyways so it's ok.

Copy link
Contributor

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just small comment for one more test

@willarmiros willarmiros merged commit b68e743 into aws:master Mar 24, 2021
@willarmiros willarmiros deleted the jackson-modules branch March 24, 2021 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants