Skip to content

Commit c928c0c

Browse files
authored
Merge pull request #45640 from brunobat/44440-otel-log-formatting
Fix Otel logging message formatting
2 parents ff1cccb + 81f3e20 commit c928c0c

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/logs/OtelLoggingTest.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,80 @@ public void testException() {
172172
.doesNotContainKey("sampled"));
173173
}
174174

175+
@Test
176+
public void testLogFormatingData() {
177+
final String message = "Replacement string";
178+
final String expected = "infof " + message;
179+
assertEquals("hello " + message, jBossLoggingBean.helloLogFormating(message));
180+
181+
List<LogRecordData> finishedLogRecordItems = logRecordExporter.getFinishedLogRecordItemsAtLeast(1);
182+
LogRecordData last = finishedLogRecordItems.get(finishedLogRecordItems.size() - 1);
183+
184+
assertThat(last.getSpanContext().getSpanId()).isEqualTo("0000000000000000");
185+
assertThat(last.getSpanContext().getTraceId()).isEqualTo("00000000000000000000000000000000");
186+
assertThat(last.getSpanContext().getTraceFlags().asHex()).isEqualTo("00");
187+
assertThat(last.getTimestampEpochNanos()).isNotNull().isLessThan(System.currentTimeMillis() * 1_000_000);
188+
189+
assertThat(last)
190+
.hasSeverity(Severity.INFO)
191+
.hasSeverityText("INFO")
192+
.hasBody(expected)
193+
.hasAttributesSatisfying(
194+
attributes -> assertThat(attributes)
195+
.containsEntry(CODE_NAMESPACE.getKey(),
196+
"io.quarkus.opentelemetry.deployment.logs.OtelLoggingTest$JBossLoggingBean")
197+
.containsEntry(CODE_FUNCTION.getKey(), "helloLogFormating")
198+
.containsEntry(THREAD_NAME.getKey(), Thread.currentThread().getName())
199+
.containsEntry(THREAD_ID.getKey(), Thread.currentThread().getId())
200+
.containsEntry("log.logger.namespace", "org.jboss.logging.Logger")
201+
.containsKey(CODE_LINENO.getKey())
202+
.doesNotContainKey(EXCEPTION_TYPE)
203+
.doesNotContainKey(EXCEPTION_MESSAGE)
204+
.doesNotContainKey(EXCEPTION_STACKTRACE)
205+
.doesNotContainKey(LOG_FILE_PATH)
206+
// attributed do not duplicate tracing data
207+
.doesNotContainKey("spanId")
208+
.doesNotContainKey("traceId")
209+
.doesNotContainKey("sampled"));
210+
}
211+
212+
@Test
213+
public void testLogParameterValue() {
214+
final String message = "Replacement parameter value";
215+
final String expected = "infov " + message;
216+
assertEquals("hello " + message, jBossLoggingBean.helloLogParameterValue(message));
217+
218+
List<LogRecordData> finishedLogRecordItems = logRecordExporter.getFinishedLogRecordItemsAtLeast(1);
219+
LogRecordData last = finishedLogRecordItems.get(finishedLogRecordItems.size() - 1);
220+
221+
assertThat(last.getSpanContext().getSpanId()).isEqualTo("0000000000000000");
222+
assertThat(last.getSpanContext().getTraceId()).isEqualTo("00000000000000000000000000000000");
223+
assertThat(last.getSpanContext().getTraceFlags().asHex()).isEqualTo("00");
224+
assertThat(last.getTimestampEpochNanos()).isNotNull().isLessThan(System.currentTimeMillis() * 1_000_000);
225+
226+
assertThat(last)
227+
.hasSeverity(Severity.INFO)
228+
.hasSeverityText("INFO")
229+
.hasBody(expected)
230+
.hasAttributesSatisfying(
231+
attributes -> assertThat(attributes)
232+
.containsEntry(CODE_NAMESPACE.getKey(),
233+
"io.quarkus.opentelemetry.deployment.logs.OtelLoggingTest$JBossLoggingBean")
234+
.containsEntry(CODE_FUNCTION.getKey(), "helloLogParameterValue")
235+
.containsEntry(THREAD_NAME.getKey(), Thread.currentThread().getName())
236+
.containsEntry(THREAD_ID.getKey(), Thread.currentThread().getId())
237+
.containsEntry("log.logger.namespace", "org.jboss.logging.Logger")
238+
.containsKey(CODE_LINENO.getKey())
239+
.doesNotContainKey(EXCEPTION_TYPE)
240+
.doesNotContainKey(EXCEPTION_MESSAGE)
241+
.doesNotContainKey(EXCEPTION_STACKTRACE)
242+
.doesNotContainKey(LOG_FILE_PATH)
243+
// attributed do not duplicate tracing data
244+
.doesNotContainKey("spanId")
245+
.doesNotContainKey("traceId")
246+
.doesNotContainKey("sampled"));
247+
}
248+
175249
private String extractStackTrace(final Throwable throwable) {
176250
try (StringWriter sw = new StringWriter(1024); PrintWriter pw = new PrintWriter(sw)) {
177251
throwable.printStackTrace(pw);
@@ -201,5 +275,15 @@ public boolean logException(final Throwable throwable) {
201275
LOG.error("logging an exception", throwable);
202276
return true;
203277
}
278+
279+
public String helloLogFormating(final String replacement) {
280+
LOG.infof("infof %s", replacement);
281+
return "hello " + replacement;
282+
}
283+
284+
public String helloLogParameterValue(final String replacement) {
285+
LOG.infov("infov {0}", replacement);
286+
return "hello " + replacement;
287+
}
204288
}
205289
}

extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/logs/OpenTelemetryLogHandler.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.time.Instant;
1414
import java.util.Map;
1515
import java.util.Optional;
16+
import java.util.logging.Formatter;
1617
import java.util.logging.Level;
1718

1819
import org.eclipse.microprofile.config.Config;
@@ -52,7 +53,15 @@ protected void doPublish(ExtLogRecord record) {
5253
}
5354

5455
if (record.getMessage() != null) {
55-
logRecordBuilder.setBody(record.getMessage());
56+
// Get the message
57+
final Formatter formatter = getFormatter();
58+
String logMsg;
59+
if (formatter != null) {
60+
logMsg = formatter.format(record);
61+
} else {
62+
logMsg = record.getFormattedMessage();
63+
}
64+
logRecordBuilder.setBody(logMsg);
5665
}
5766

5867
final AttributesBuilder attributes = Attributes.builder();

0 commit comments

Comments
 (0)