diff --git a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/Marshaler.java b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/Marshaler.java index fe027fcdc07..2e6fba4644c 100644 --- a/exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/Marshaler.java +++ b/exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/Marshaler.java @@ -5,7 +5,6 @@ package io.opentelemetry.exporter.internal.marshal; -import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import java.io.IOException; import java.io.OutputStream; @@ -18,8 +17,6 @@ */ public abstract class Marshaler { - public static final JsonFactory JSON_FACTORY = new JsonFactory(); - /** Marshals into the {@link OutputStream} in proto binary format. */ public final void writeBinaryTo(OutputStream output) throws IOException { try (Serializer serializer = new ProtoSerializer(output)) { @@ -27,15 +24,6 @@ public final void writeBinaryTo(OutputStream output) throws IOException { } } - /** Marshals into the {@link OutputStream} in proto JSON format. */ - public final void writeJsonWithoutCloseTo(OutputStream output) throws IOException { - JsonGenerator generator = - JSON_FACTORY.createGenerator(output).disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); - try (JsonSerializer serializer = new JsonSerializer(generator)) { - serializer.writeMessageValue(this); - } - } - /** Marshals into the {@link OutputStream} in proto JSON format. */ public final void writeJsonTo(OutputStream output) throws IOException { try (JsonSerializer serializer = new JsonSerializer(output)) { diff --git a/exporters/logging-otlp/src/main/java/io/opentelemetry/exporter/logging/otlp/internal/writer/StreamJsonWriter.java b/exporters/logging-otlp/src/main/java/io/opentelemetry/exporter/logging/otlp/internal/writer/StreamJsonWriter.java index dab73a8b398..0674810fa5c 100644 --- a/exporters/logging-otlp/src/main/java/io/opentelemetry/exporter/logging/otlp/internal/writer/StreamJsonWriter.java +++ b/exporters/logging-otlp/src/main/java/io/opentelemetry/exporter/logging/otlp/internal/writer/StreamJsonWriter.java @@ -5,6 +5,8 @@ package io.opentelemetry.exporter.logging.otlp.internal.writer; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonGenerator; import io.opentelemetry.exporter.internal.marshal.Marshaler; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.internal.ThrottlingLogger; @@ -19,6 +21,8 @@ */ public class StreamJsonWriter implements JsonWriter { + public static final JsonFactory JSON_FACTORY = new JsonFactory(); + private static final Logger internalLogger = Logger.getLogger(StreamJsonWriter.class.getName()); private final ThrottlingLogger logger = new ThrottlingLogger(internalLogger); @@ -34,7 +38,10 @@ public StreamJsonWriter(OutputStream originalStream, String type) { @Override public CompletableResultCode write(Marshaler exportRequest) { try { - exportRequest.writeJsonWithoutCloseTo(outputStream); + exportRequest.writeJsonTo( + JSON_FACTORY + .createGenerator(outputStream) + .disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET)); return CompletableResultCode.ofSuccess(); } catch (IOException e) { logger.log(Level.WARNING, "Unable to write OTLP JSON " + type, e); diff --git a/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/internal/writer/StreamJsonWriterTest.java b/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/internal/writer/StreamJsonWriterTest.java index c1b4830c1b9..723db6004db 100644 --- a/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/internal/writer/StreamJsonWriterTest.java +++ b/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/internal/writer/StreamJsonWriterTest.java @@ -9,6 +9,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import com.fasterxml.jackson.core.JsonGenerator; import io.github.netmikey.logunit.api.LogCapturer; import io.opentelemetry.exporter.internal.marshal.Marshaler; import java.io.FilterOutputStream; @@ -45,9 +46,7 @@ void testToString() throws IOException { @Test void errorWriting() throws IOException { Marshaler marshaler = mock(Marshaler.class); - Mockito.doThrow(new IOException("test")) - .when(marshaler) - .writeJsonWithoutCloseTo(any(OutputStream.class)); + Mockito.doThrow(new IOException("test")).when(marshaler).writeJsonTo(any(JsonGenerator.class)); StreamJsonWriter writer = new StreamJsonWriter(System.out, "type"); writer.write(marshaler);