diff --git a/aws-xray-recorder-sdk-core/build.gradle.kts b/aws-xray-recorder-sdk-core/build.gradle.kts index b80adc7a..97708e21 100644 --- a/aws-xray-recorder-sdk-core/build.gradle.kts +++ b/aws-xray-recorder-sdk-core/build.gradle.kts @@ -9,6 +9,7 @@ dependencies { compileOnly("com.google.code.findbugs:jsr305:3.0.2") compileOnly("javax.servlet:javax.servlet-api:3.1.0") + testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.0") testImplementation("com.github.stefanbirkner:system-rules:1.16.0") testImplementation("com.github.tomakehurst:wiremock-jre8") testImplementation("org.openjdk.jmh:jmh-core:1.19") diff --git a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/EntityImpl.java b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/EntityImpl.java index 2e35f2f2..2d8b8dc1 100644 --- a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/EntityImpl.java +++ b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/EntityImpl.java @@ -57,6 +57,7 @@ public abstract class EntityImpl implements Entity { @SuppressWarnings("checkstyle:ConstantName") @Deprecated protected static final ObjectMapper mapper = new ObjectMapper() + .findAndRegisterModules() .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES) .setSerializationInclusion(JsonInclude.Include.NON_EMPTY); diff --git a/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/entities/EntityTest.java b/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/entities/EntityTest.java new file mode 100644 index 00000000..6a0d4edf --- /dev/null +++ b/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/entities/EntityTest.java @@ -0,0 +1,70 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + + +package com.amazonaws.xray.entities; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +import com.amazonaws.xray.AWSXRay; +import java.time.Duration; +import java.time.Instant; +import java.util.Date; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class EntityTest { + + @BeforeEach + void setup() { + AWSXRay.clearTraceEntity(); + } + + @Test + void testDurationSerialization() { + Segment seg = new SegmentImpl(AWSXRay.getGlobalRecorder(), "test"); + seg.putMetadata("millisecond", Duration.ofMillis(3)); + seg.putMetadata("second", Duration.ofSeconds(1)); + seg.putMetadata("minute", Duration.ofMinutes(55)); + String serializedSeg = seg.serialize(); + + String expected = "{\"default\":{\"millisecond\":0.003000000,\"second\":1.000000000,\"minute\":3300.000000000}}"; + assertThat(serializedSeg).contains(expected); + } + + @Test + void testTimestampSerialization() { + Segment seg = new SegmentImpl(AWSXRay.getGlobalRecorder(), "test"); + seg.putMetadata("instant", Instant.ofEpochSecond(1616559298)); + String serializedSeg = seg.serialize(); + + String expected = "{\"default\":{\"instant\":1616559298.000000000}}"; + assertThat(serializedSeg).contains(expected); + } + + /** + * Dates are serialized into millisecond integers rather than second doubles because anything beyond millisecond + * accuracy is meaningless for Dates: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html + */ + @Test + void testDateSerialization() { + Segment seg = new SegmentImpl(AWSXRay.getGlobalRecorder(), "test"); + seg.putMetadata("date", Date.from(Instant.ofEpochSecond(1616559298))); + String serializedSeg = seg.serialize(); + + String expected = "{\"default\":{\"date\":1616559298000}}"; + assertThat(serializedSeg).contains(expected); + } +} diff --git a/dependencyManagement/build.gradle.kts b/dependencyManagement/build.gradle.kts index dbe3ca11..087a0b06 100644 --- a/dependencyManagement/build.gradle.kts +++ b/dependencyManagement/build.gradle.kts @@ -15,6 +15,11 @@ val DEPENDENCY_SETS = listOf( "2.5.1", listOf("error_prone_annotations") ), + DependencySet( + "com.fasterxml.jackson.datatype", + "2.11.0", + listOf("jackson-datatype-jsr310") + ), DependencySet( "com.github.tomakehurst", "2.26.3",