From 7d74adafdc66615c122a4d06b156eb93366f0986 Mon Sep 17 00:00:00 2001 From: jack-berg <34418638+jack-berg@users.noreply.github.com> Date: Wed, 20 Nov 2024 10:59:08 -0600 Subject: [PATCH] Rename Marshaler#writeJsonToGenerator to allow jackson runtimeOnly dependency (#6896) --- .../opentelemetry/exporter/internal/marshal/Marshaler.java | 5 ++++- .../logging/otlp/internal/writer/LoggerJsonWriter.java | 2 +- .../logging/otlp/internal/writer/LoggerJsonWriterTest.java | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) 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 6201481c024..e942673da13 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 @@ -32,7 +32,10 @@ public final void writeJsonTo(OutputStream output) throws IOException { } /** Marshals into the {@link JsonGenerator} in proto JSON format. */ - public final void writeJsonTo(JsonGenerator output) throws IOException { + // Intentionally not overloading writeJsonTo(OutputStream) in order to avoid compilation + // dependency on jackson when using writeJsonTo(OutputStream). See: + // https://github.com/open-telemetry/opentelemetry-java-contrib/pull/1551#discussion_r1849064365 + public final void writeJsonToGenerator(JsonGenerator output) throws IOException { try (JsonSerializer serializer = new JsonSerializer(output)) { serializer.writeMessageValue(this); } diff --git a/exporters/logging-otlp/src/main/java/io/opentelemetry/exporter/logging/otlp/internal/writer/LoggerJsonWriter.java b/exporters/logging-otlp/src/main/java/io/opentelemetry/exporter/logging/otlp/internal/writer/LoggerJsonWriter.java index 1286e116f03..a95c5e0d2c1 100644 --- a/exporters/logging-otlp/src/main/java/io/opentelemetry/exporter/logging/otlp/internal/writer/LoggerJsonWriter.java +++ b/exporters/logging-otlp/src/main/java/io/opentelemetry/exporter/logging/otlp/internal/writer/LoggerJsonWriter.java @@ -33,7 +33,7 @@ public LoggerJsonWriter(Logger logger, String type) { public CompletableResultCode write(Marshaler exportRequest) { SegmentedStringWriter sw = new SegmentedStringWriter(JSON_FACTORY._getBufferRecycler()); try (JsonGenerator gen = JsonUtil.create(sw)) { - exportRequest.writeJsonTo(gen); + exportRequest.writeJsonToGenerator(gen); } catch (IOException e) { logger.log(Level.WARNING, "Unable to write OTLP JSON " + type, e); return CompletableResultCode.ofFailure(); diff --git a/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/internal/writer/LoggerJsonWriterTest.java b/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/internal/writer/LoggerJsonWriterTest.java index 8cba4ef9707..0810f3c7510 100644 --- a/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/internal/writer/LoggerJsonWriterTest.java +++ b/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/internal/writer/LoggerJsonWriterTest.java @@ -32,7 +32,9 @@ void testToString() { @Test void error() throws IOException { Marshaler marshaler = mock(Marshaler.class); - Mockito.doThrow(new IOException("test")).when(marshaler).writeJsonTo(any(JsonGenerator.class)); + Mockito.doThrow(new IOException("test")) + .when(marshaler) + .writeJsonToGenerator(any(JsonGenerator.class)); Logger logger = Logger.getLogger(LoggerJsonWriter.class.getName());