Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Sep 11, 2024
1 parent febd77c commit 0f88cb2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,24 +17,13 @@
*/
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)) {
writeTo(serializer);
}
}

/** 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0f88cb2

Please sign in to comment.