diff --git a/dd-java-agent/agent-bootstrap/src/main/resources/META-INF/native-image/com.datadoghq/dd-java-agent/reflect-config.json b/dd-java-agent/agent-bootstrap/src/main/resources/META-INF/native-image/com.datadoghq/dd-java-agent/reflect-config.json index bbb45297fffd..dba8d64e6b47 100644 --- a/dd-java-agent/agent-bootstrap/src/main/resources/META-INF/native-image/com.datadoghq/dd-java-agent/reflect-config.json +++ b/dd-java-agent/agent-bootstrap/src/main/resources/META-INF/native-image/com.datadoghq/dd-java-agent/reflect-config.json @@ -47,6 +47,19 @@ {"name": "fromJson"} ] }, + { + "name" : "datadog.trace.agent.core.DDSpanLink$SpanLinkAdapter", + "methods": [ + {"name": "toSpanLinkJson"} + ] + }, + { + "name" : "datadog.trace.agent.core.DDSpanLink$SpanLinkJson", + "allDeclaredConstructors" : true, + "allPublicConstructors" : true, + "allDeclaredFields" : true, + "allPublicFields" : true + }, { "name" : "datadog.trace.instrumentation.springweb.HandlerMappingResourceNameFilter", "methods": [ diff --git a/dd-trace-core/src/main/java/datadog/trace/core/DDSpanLink.java b/dd-trace-core/src/main/java/datadog/trace/core/DDSpanLink.java index 69f0659c0873..f2b5c39fe45f 100644 --- a/dd-trace-core/src/main/java/datadog/trace/core/DDSpanLink.java +++ b/dd-trace-core/src/main/java/datadog/trace/core/DDSpanLink.java @@ -23,8 +23,6 @@ public class DDSpanLink extends SpanLink { private static final Logger LOGGER = LoggerFactory.getLogger(DDSpanLink.class); /** The maximum of characters a span tag value can hold. */ private static final int TAG_MAX_LENGTH = 25_000; - /** JSON encoder (lazily initialized) */ - private static JsonAdapter encoder; protected DDSpanLink( DDTraceId traceId, long spanId, byte traceFlags, String traceState, Attributes attributes) { @@ -95,11 +93,20 @@ public static String toTag(List links) { } private static JsonAdapter getEncoder() { - if (encoder == null) { + return EncoderHolder.ENCODER; + } + + /** + * JSON encoder (lazily initialized). This is not folded at native image build time, so it works + * as expected. + */ + private static class EncoderHolder { + static final JsonAdapter ENCODER = createEncoder(); + + private static JsonAdapter createEncoder() { Moshi moshi = new Moshi.Builder().add(new SpanLinkAdapter()).build(); - encoder = moshi.adapter(AgentSpanLink.class); + return moshi.adapter(AgentSpanLink.class); } - return encoder; } private static class SpanLinkAdapter {