blob: b7552303d6b7e86238228d0d9e2ffe01ab19c2a2 [file] [log] [blame]
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +02001package eu.mulk.quarkus.googlecloud.jsonlogging;
2
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +02003import static org.junit.jupiter.api.Assertions.assertLinesMatch;
4
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +02005import jakarta.json.Json;
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +02006import java.util.Collection;
7import java.util.List;
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +02008import org.jboss.logmanager.ExtLogRecord;
9import org.jboss.logmanager.Level;
10import org.junit.jupiter.api.Test;
11
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +020012class FormatterTest {
13
14 @Test
15 void simpleRecord() {
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +020016 var logRecord = makeSimpleRecord();
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +020017
18 var formatter = new Formatter(List.of(), List.of());
19 var formattingResult = formatter.format(logRecord);
20
21 assertLinesMatch(
22 List.of(
23 "\\{"
24 + "\"message\":\"Hello, world!\","
25 + "\"severity\":\"INFO\","
26 + "\"timestamp\":\\{\"seconds\":\\d+,\"nanos\":\\d+\\},"
27 + "\"logging.googleapis.com/sourceLocation\":"
28 + "\\{\"file\":\"ReflectionUtils.java\","
29 + "\"line\":\"\\d+\","
30 + "\"function\":\"org.junit.platform.commons.util.ReflectionUtils.invokeMethod\""
31 + "\\}"
32 + "\\}\n"),
33 List.of(formattingResult));
34 }
35
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +020036 static ExtLogRecord makeSimpleRecord() {
37 return new ExtLogRecord(Level.INFO, "Hello, world!", FormatterTest.class.getName());
38 }
39
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +020040 @Test
41 void structuredRecord() {
42 var parameterProvider =
43 new StructuredParameterProvider() {
44 @Override
45 public StructuredParameter getParameter() {
46 var b = Json.createObjectBuilder();
47 b.add("traceId", "39f9a49a9567a8bd7087b708f8932550");
48 b.add("spanId", "c7431b14630b633d");
49 return () -> b;
50 }
51 };
52
53 var labelProvider =
54 new LabelProvider() {
55 @Override
56 public Collection<Label> getLabels() {
57 return List.of(Label.of("requestId", "123"));
58 }
59 };
60
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +020061 var logRecord = makeStructuredRecord();
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +020062
63 var formatter = new Formatter(List.of(parameterProvider), List.of(labelProvider));
64 var formattingResult = formatter.format(logRecord);
65 assertLinesMatch(
66 List.of(
67 "\\{"
68 + "\"logging.googleapis.com/labels\":\\{\"a\":\"b\",\"requestId\":\"123\"\\},"
69 + "\"message\":\"Hello, world!\","
70 + "\"severity\":\"INFO\","
71 + "\"timestamp\":\\{\"seconds\":\\d+,\"nanos\":\\d+\\},"
72 + "\"logging.googleapis.com/sourceLocation\":"
73 + "\\{\"file\":\"ReflectionUtils.java\","
74 + "\"line\":\"\\d+\","
75 + "\"function\":\"org.junit.platform.commons.util.ReflectionUtils.invokeMethod\""
76 + "\\},"
77 + "\"traceId\":\"39f9a49a9567a8bd7087b708f8932550\","
78 + "\"spanId\":\"c7431b14630b633d\","
79 + "\"one\":1,"
80 + "\"two\":2.0,"
81 + "\"yes\":true"
82 + "\\}\n"),
83 List.of(formattingResult));
84 }
Matthias Andreas Benkard47df8be2024-06-23 16:24:11 +020085
86 static ExtLogRecord makeStructuredRecord() {
87 var logRecord = makeSimpleRecord();
88 logRecord.setParameters(
89 new Object[] {
90 (StructuredParameter)
91 () -> Json.createObjectBuilder().add("one", 1).add("two", 2.0).add("yes", true),
92 Label.of("a", "b")
93 });
94 return logRecord;
95 }
Matthias Andreas Benkardf90824b2024-06-23 16:42:11 +020096
97 @Test
98 void massivelyStructuredRecord() {
99 var logRecord = makeMassivelyStructuredRecord();
100
101 var formatter = new Formatter(List.of(), List.of());
102 var formattingResult = formatter.format(logRecord);
103 assertLinesMatch(
104 List.of(
105 "\\{"
106 + "\"message\":\"Hello, world!\","
107 + "\"severity\":\"INFO\","
108 + "\"timestamp\":\\{\"seconds\":\\d+,\"nanos\":\\d+\\},"
109 + "\"logging.googleapis.com/sourceLocation\":"
110 + "\\{\"file\":\"ReflectionUtils.java\","
111 + "\"line\":\"\\d+\","
112 + "\"function\":\"org.junit.platform.commons.util.ReflectionUtils.invokeMethod\""
113 + "\\},"
114 + "\"int-0\":0,\"int-1\":1,\"int-2\":2,\"int-3\":3,\"int-4\":4,\"int-5\":5,\"int-6\":6,\"int-7\":7,\"int-8\":8,\"int-9\":9,"
115 + "\"double-10\":10.0,\"double-11\":11.0,\"double-12\":12.0,\"double-13\":13.0,\"double-14\":14.0,"
116 + "\"double-15\":15.0,\"double-16\":16.0,\"double-17\":17.0,\"double-18\":18.0,\"double-19\":19.0,"
117 + "\"boolean-20\":true,\"boolean-21\":false,\"boolean-22\":true,\"boolean-23\":false,\"boolean-24\":true,"
118 + "\"boolean-25\":false,\"boolean-26\":true,\"boolean-27\":false,\"boolean-28\":true,\"boolean-29\":false,"
119 + "\"string-30\":\"30\",\"string-31\":\"31\",\"string-32\":\"32\",\"string-33\":\"33\",\"string-34\":\"34\","
120 + "\"string-35\":\"35\",\"string-36\":\"36\",\"string-37\":\"37\",\"string-38\":\"38\",\"string-39\":\"39\""
121 + "\\}\n"),
122 List.of(formattingResult));
123 }
124
125 static ExtLogRecord makeMassivelyStructuredRecord() {
126 var logRecord = FormatterTest.makeSimpleRecord();
127 logRecord.setParameters(
128 new Object[] {
129 (StructuredParameter)
130 () -> {
131 var b = Json.createObjectBuilder();
132 for (int i = 0; i < 10; i++) {
133 b.add("int-" + i, i);
134 }
135 for (int i = 10; i < 20; i++) {
136 b.add("double-" + i, (double) i);
137 }
138 for (int i = 20; i < 30; i++) {
139 b.add("boolean-" + i, i % 2 == 0);
140 }
141 for (int i = 30; i < 40; i++) {
142 b.add("string-" + i, String.valueOf(i));
143 }
144 return b;
145 }
146 });
147 return logRecord;
148 }
Matthias Andreas Benkardb69b3012024-06-23 15:48:49 +0200149}