diff --git a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayObjectMapper.java b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayObjectMapper.java deleted file mode 100644 index 7cb5eeb8..00000000 --- a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayObjectMapper.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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; - -import com.amazonaws.xray.entities.Cause; -import com.amazonaws.xray.serializers.CauseSerializer; -import com.amazonaws.xray.serializers.StackTraceElementSerializer; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.BeanDescription; -import com.fasterxml.jackson.databind.JsonSerializer; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; -import com.fasterxml.jackson.databind.SerializationConfig; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; - -public class AWSXRayObjectMapper extends ObjectMapper { - - private static AWSXRayObjectMapper instance; - - public static AWSXRayObjectMapper getInstance() { - return instance; - } - - static { - instance = new AWSXRayObjectMapper(); - - instance.findAndRegisterModules(); - instance.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); - instance.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); - instance.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); - - instance.registerModule(new SimpleModule() { - private static final long serialVersionUID = 545800949242254918L; - - @Override - public void setupModule(SetupContext setupContext) { - super.setupModule(setupContext); - setupContext.addBeanSerializerModifier(new BeanSerializerModifier() { - @SuppressWarnings("unchecked") - @Override - public JsonSerializer modifySerializer( - SerializationConfig serializationConfig, - BeanDescription beanDescription, - JsonSerializer jsonSerializer) { - Class beanClass = beanDescription.getBeanClass(); - if (Cause.class.isAssignableFrom(beanClass)) { - return new CauseSerializer((JsonSerializer) jsonSerializer); - } else if (StackTraceElement.class.isAssignableFrom(beanClass)) { - return new StackTraceElementSerializer(); - } - return jsonSerializer; - } - }); - } - }); - } -} 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 d71a04b5..7591deec 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 @@ -15,15 +15,24 @@ package com.amazonaws.xray.entities; -import com.amazonaws.xray.AWSXRayObjectMapper; import com.amazonaws.xray.AWSXRayRecorder; import com.amazonaws.xray.exceptions.AlreadyEmittedException; +import com.amazonaws.xray.serializers.CauseSerializer; +import com.amazonaws.xray.serializers.StackTraceElementSerializer; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.BeanDescription; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.SerializationConfig; +import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.node.NullNode; +import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import java.util.ArrayList; import java.util.List; @@ -41,6 +50,18 @@ * */ public abstract class EntityImpl implements Entity { + + /** + * @deprecated For internal use only. + */ + @SuppressWarnings("checkstyle:ConstantName") + @Deprecated + protected static final ObjectMapper mapper = new ObjectMapper() + .findAndRegisterModules() + .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) + .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES) + .setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + private static final Log logger = LogFactory.getLog(EntityImpl.class); private static final String DEFAULT_METADATA_NAMESPACE = "default"; @@ -100,6 +121,37 @@ public abstract class EntityImpl implements Entity { @JsonIgnore private boolean emitted = false; + static { + /* + * Inject the CauseSerializer and StackTraceElementSerializer classes into the local mapper such that they will serialize + * their respective object types. + */ + mapper.registerModule(new SimpleModule() { + private static final long serialVersionUID = 545800949242254918L; + + @Override + public void setupModule(SetupContext setupContext) { + super.setupModule(setupContext); + setupContext.addBeanSerializerModifier(new BeanSerializerModifier() { + @SuppressWarnings("unchecked") + @Override + public JsonSerializer modifySerializer( + SerializationConfig serializationConfig, + BeanDescription beanDescription, + JsonSerializer jsonSerializer) { + Class beanClass = beanDescription.getBeanClass(); + if (Cause.class.isAssignableFrom(beanClass)) { + return new CauseSerializer((JsonSerializer) jsonSerializer); + } else if (StackTraceElement.class.isAssignableFrom(beanClass)) { + return new StackTraceElementSerializer(); + } + return jsonSerializer; + } + }); + } + }); + } + // default constructor for Jackson, so it can understand the default values to compare when using the Include.NON_DEFAULT // annotation. @SuppressWarnings("nullness") @@ -560,7 +612,7 @@ public boolean compareAndSetEmitted(boolean current, boolean next) { @Override public String serialize() { try { - return AWSXRayObjectMapper.getInstance().writeValueAsString(this); + return mapper.writeValueAsString(this); } catch (JsonProcessingException jpe) { logger.error("Exception while serializing entity.", jpe); } @@ -570,7 +622,7 @@ public String serialize() { @Override public String prettySerialize() { try { - return AWSXRayObjectMapper.getInstance().writerWithDefaultPrettyPrinter().writeValueAsString(this); + return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this); } catch (JsonProcessingException jpe) { logger.error("Exception while serializing segment.", jpe); } diff --git a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SubsegmentImpl.java b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SubsegmentImpl.java index 4477eea1..f17fddc2 100644 --- a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SubsegmentImpl.java +++ b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SubsegmentImpl.java @@ -15,7 +15,6 @@ package com.amazonaws.xray.entities; -import com.amazonaws.xray.AWSXRayObjectMapper; import com.amazonaws.xray.AWSXRayRecorder; import com.amazonaws.xray.internal.SamplingStrategyOverride; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -146,7 +145,7 @@ public boolean shouldPropagate() { } private ObjectNode getStreamSerializeObjectNode() { - ObjectNode obj = (ObjectNode) AWSXRayObjectMapper.getInstance().valueToTree(this); + ObjectNode obj = (ObjectNode) mapper.valueToTree(this); obj.put("type", "subsegment"); obj.put("parent_id", getParent().getId()); obj.put("trace_id", parentSegment.getTraceId().toString()); @@ -157,7 +156,7 @@ private ObjectNode getStreamSerializeObjectNode() { public String streamSerialize() { String ret = ""; try { - ret = AWSXRayObjectMapper.getInstance().writeValueAsString(getStreamSerializeObjectNode()); + ret = mapper.writeValueAsString(getStreamSerializeObjectNode()); } catch (JsonProcessingException jpe) { logger.error("Exception while serializing entity.", jpe); } @@ -168,8 +167,7 @@ public String streamSerialize() { public String prettyStreamSerialize() { String ret = ""; try { - ret = AWSXRayObjectMapper.getInstance().writerWithDefaultPrettyPrinter() - .writeValueAsString(getStreamSerializeObjectNode()); + ret = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(getStreamSerializeObjectNode()); } catch (JsonProcessingException jpe) { logger.error("Exception while serializing entity.", jpe); }