Skip to content

Commit a6b3302

Browse files
authored
bugfix: add newline in stdout exporter (#6848)
1 parent 98fa296 commit a6b3302

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/Marshaler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public final void writeJsonTo(JsonGenerator output) throws IOException {
3838
}
3939
}
4040

41+
/** Marshals into the {@link JsonGenerator} in proto JSON format and adds a newline. */
42+
public final void writeJsonWithNewline(JsonGenerator output) throws IOException {
43+
try (JsonSerializer serializer = new JsonSerializer(output)) {
44+
serializer.writeMessageValue(this);
45+
output.writeRaw('\n');
46+
}
47+
}
48+
4149
/** Returns the number of bytes this Marshaler will write in proto binary format. */
4250
public abstract int getBinarySerializedSize();
4351

exporters/logging-otlp/src/main/java/io/opentelemetry/exporter/logging/otlp/internal/writer/StreamJsonWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public StreamJsonWriter(OutputStream originalStream, String type) {
3838
@Override
3939
public CompletableResultCode write(Marshaler exportRequest) {
4040
try {
41-
exportRequest.writeJsonTo(
41+
exportRequest.writeJsonWithNewline(
4242
JSON_FACTORY
4343
.createGenerator(outputStream)
4444
.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET));

exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/AbstractOtlpStdoutExporterTest.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.util.function.Supplier;
3535
import java.util.stream.Stream;
3636
import javax.annotation.Nullable;
37-
import org.json.JSONException;
3837
import org.junit.jupiter.api.AfterAll;
3938
import org.junit.jupiter.api.BeforeAll;
4039
import org.junit.jupiter.api.BeforeEach;
@@ -57,6 +56,7 @@ abstract class AbstractOtlpStdoutExporterTest<T> {
5756
private static final PrintStream SYSTEM_OUT_PRINT_STREAM = new PrintStream(SYSTEM_OUT_STREAM);
5857

5958
@RegisterExtension LogCapturer logs;
59+
private int skipLogs;
6060
private final String defaultConfigString;
6161
private final TestDataExporter<? super T> testDataExporter;
6262
protected final Class<?> exporterClass;
@@ -87,21 +87,22 @@ protected abstract T createExporter(
8787
private String output(@Nullable OutputStream outputStream, @Nullable Path file) {
8888
if (outputStream == null) {
8989
return logs.getEvents().stream()
90+
.skip(skipLogs)
9091
.map(LoggingEvent::getMessage)
9192
.reduce("", (a, b) -> a + b + "\n")
9293
.trim();
9394
}
9495

9596
if (file != null) {
9697
try {
97-
return new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
98+
return new String(Files.readAllBytes(file), StandardCharsets.UTF_8).trim();
9899
} catch (IOException e) {
99100
throw new RuntimeException(e);
100101
}
101102
}
102103

103104
try {
104-
return SYSTEM_OUT_STREAM.toString(StandardCharsets.UTF_8.name());
105+
return SYSTEM_OUT_STREAM.toString(StandardCharsets.UTF_8.name()).trim();
105106
} catch (UnsupportedEncodingException e) {
106107
throw new RuntimeException(e);
107108
}
@@ -204,8 +205,7 @@ private static Arguments testCase(
204205
@SuppressWarnings("SystemOut")
205206
@ParameterizedTest(name = "{0}")
206207
@MethodSource("exportTestCases")
207-
void exportWithProgrammaticConfig(String name, TestCase testCase)
208-
throws JSONException, IOException {
208+
void exportWithProgrammaticConfig(String name, TestCase testCase) throws Exception {
209209
OutputStream outputStream;
210210
Path file = null;
211211
switch (testCase.getOutputType()) {
@@ -247,6 +247,26 @@ void exportWithProgrammaticConfig(String name, TestCase testCase)
247247
if (testCase.isWrapperJsonObject()) {
248248
assertThat(output).doesNotContain("\n");
249249
}
250+
251+
if (file == null) {
252+
// no need to test again for file - and it's not working with files
253+
assertDoubleOutput(exporter, expectedJson, outputStream);
254+
}
255+
}
256+
257+
private void assertDoubleOutput(
258+
Supplier<T> exporter, String expectedJson, @Nullable OutputStream outputStream)
259+
throws Exception {
260+
SYSTEM_OUT_STREAM.reset();
261+
skipLogs = logs.getEvents().size();
262+
testDataExporter.export(exporter.get());
263+
testDataExporter.export(exporter.get());
264+
265+
String[] lines = output(outputStream, null).split("\n");
266+
assertThat(lines).hasSize(2);
267+
for (String line : lines) {
268+
JSONAssert.assertEquals("Got \n" + line, expectedJson, line, false);
269+
}
250270
}
251271

252272
@Test

exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/internal/writer/StreamJsonWriterTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ void testToString() throws IOException {
4646
@Test
4747
void errorWriting() throws IOException {
4848
Marshaler marshaler = mock(Marshaler.class);
49-
Mockito.doThrow(new IOException("test")).when(marshaler).writeJsonTo(any(JsonGenerator.class));
49+
Mockito.doThrow(new IOException("test"))
50+
.when(marshaler)
51+
.writeJsonWithNewline(any(JsonGenerator.class));
5052

5153
StreamJsonWriter writer = new StreamJsonWriter(System.out, "type");
5254
writer.write(marshaler);

exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/PrometheusMetricReaderTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ private static void assertExponentialHistogramComplete(
616616
+ " value: 7.0\n"
617617
+ " timestamp {\n"
618618
+ " seconds: <timestamp>\n"
619-
+ " nanos: <timestamp>\n"
619+
+ " <maybeNanos>\n"
620620
+ " }\n"
621621
+ " }\n"
622622
+ " }\n"
@@ -661,7 +661,7 @@ private static void assertExponentialHistogramComplete(
661661
+ " value: 3.0\n"
662662
+ " timestamp {\n"
663663
+ " seconds: <timestamp>\n"
664-
+ " nanos: <timestamp>\n"
664+
+ " <maybeNanos>\n"
665665
+ " }\n"
666666
+ " }\n"
667667
+ " }\n"
@@ -1115,7 +1115,9 @@ private static void assertMatches(String expected, String actual) {
11151115
*/
11161116
private static String toPattern(String expected) {
11171117
Map<String, String> replacePatterns = new HashMap<>();
1118-
replacePatterns.put("timestamp", "[0-9]+(\\.[0-9]+)?");
1118+
String timestampPattern = "[0-9]+(\\.[0-9]+)?";
1119+
replacePatterns.put("timestamp", timestampPattern);
1120+
replacePatterns.put("maybeNanos", String.format("(nanos: %s)?", timestampPattern));
11191121
replacePatterns.put("spanId", "[a-z0-9]*");
11201122
replacePatterns.put("traceId", "[a-z0-9]*");
11211123
replacePatterns.put("measurement", "[0-9\\.]*");

0 commit comments

Comments
 (0)