From e35b9f01c572022370b52bae29a0f9fbec7091af Mon Sep 17 00:00:00 2001 From: "igor.suhorukov" Date: Thu, 25 Jul 2024 22:00:06 +0300 Subject: [PATCH] log.body.parameters as stringArray --- .../v1_0/internal/LoggingEventMapper.java | 9 +++++---- .../logback/appender/v1_0/Slf4j2Test.java | 17 ++++++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java b/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java index e2e2997b8153..28b42ac32e22 100644 --- a/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java +++ b/instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java @@ -24,9 +24,11 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import org.slf4j.Marker; import org.slf4j.event.KeyValuePair; @@ -202,10 +204,9 @@ void captureMdcAttributes(AttributesBuilder attributes, Map mdcP void captureArguments(AttributesBuilder attributes, String message, Object[] arguments) { String bodyKey = "log.body.template"; attributes.put(bodyKey, message); - for (int idx = 0; idx < arguments.length; idx++) { - Object argument = arguments[idx]; - propagateAttribute(attributes, String.format("log.body.parameters.%d", idx), argument); - } + attributes.put( + AttributeKey.stringArrayKey("log.body.parameters"), + Arrays.stream(arguments).map(String::valueOf).collect(Collectors.toList())); } public static AttributeKey getMdcAttributeKey(String key) { diff --git a/instrumentation/logback/logback-appender-1.0/library/src/slf4j2ApiTest/java/io/opentelemetry/instrumentation/logback/appender/v1_0/Slf4j2Test.java b/instrumentation/logback/logback-appender-1.0/library/src/slf4j2ApiTest/java/io/opentelemetry/instrumentation/logback/appender/v1_0/Slf4j2Test.java index 6a973451e825..af17aeaedd84 100644 --- a/instrumentation/logback/logback-appender-1.0/library/src/slf4j2ApiTest/java/io/opentelemetry/instrumentation/logback/appender/v1_0/Slf4j2Test.java +++ b/instrumentation/logback/logback-appender-1.0/library/src/slf4j2ApiTest/java/io/opentelemetry/instrumentation/logback/appender/v1_0/Slf4j2Test.java @@ -133,13 +133,20 @@ void arguments() { assertThat(logData.getBody().asString()) .isEqualTo( "log message 'world' and 3.141592653589793, bool true, long 9223372036854775807"); - assertThat(logData.getAttributes().size()).isEqualTo(9); + assertThat(logData.getAttributes().size()).isEqualTo(6); + assertThat(logData.getAttributes()) + .hasEntrySatisfying( + AttributeKey.stringArrayKey("log.body.parameters"), + value -> + assertThat(value) + .isEqualTo( + Arrays.asList( + "'world'", + String.valueOf(Math.PI), + String.valueOf(true), + String.valueOf(Long.MAX_VALUE)))); assertThat(logData) .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("log.body.parameters.0"), "'world'"), - equalTo(AttributeKey.doubleKey("log.body.parameters.1"), Math.PI), - equalTo(AttributeKey.booleanKey("log.body.parameters.2"), true), - equalTo(AttributeKey.longKey("log.body.parameters.3"), Long.MAX_VALUE), equalTo( AttributeKey.stringKey("log.body.template"), "log message {} and {}, bool {}, long {}"));