Minor log4j instrumentation updates (#4938)

* Minor log4j instrumentation updates

* Remove duplicate line

* Use semantic attributes for exceptions
This commit is contained in:
Trask Stalnaker 2021-12-19 19:32:46 -08:00 committed by GitHub
parent a1f08494e6
commit a76c35e33f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 17 deletions

View File

@ -5,12 +5,14 @@
package io.opentelemetry.instrumentation.log4j.v2_16;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.appender.LogBuilder;
import io.opentelemetry.instrumentation.api.appender.Severity;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
@ -19,10 +21,6 @@ import org.apache.logging.log4j.message.Message;
final class LogEventMapper {
// Visible for testing
static final AttributeKey<String> ATTR_THROWABLE_MESSAGE =
AttributeKey.stringKey("throwable.message");
/**
* Map the {@link LogEvent} data model onto the {@link LogBuilder}. Unmapped fields include:
*
@ -31,17 +29,12 @@ final class LogEventMapper {
* <li>Thread name - {@link LogEvent#getThreadName()}
* <li>Thread id - {@link LogEvent#getThreadId()}
* <li>Thread priority - {@link LogEvent#getThreadPriority()}
* <li>Thread priority - {@link LogEvent#getThreadPriority()}
* <li>Thrown details (stack trace, class name) - {@link LogEvent#getThrown()}
* <li>Marker - {@link LogEvent#getMarker()}
* <li>Nested diagnostic context - {@link LogEvent#getContextStack()}
* <li>Mapped diagnostic context - {@link LogEvent#getContextData()}
* </ul>
*/
static void mapLogEvent(LogBuilder builder, LogEvent logEvent) {
// TODO: map the LogEvent more completely when semantic conventions allow it
AttributesBuilder attributes = Attributes.builder();
// message
Message message = logEvent.getMessage();
if (message != null) {
@ -67,21 +60,27 @@ final class LogEventMapper {
// throwable
Throwable throwable = logEvent.getThrown();
if (throwable != null) {
attributes.put(ATTR_THROWABLE_MESSAGE, throwable.getMessage());
AttributesBuilder attributes = Attributes.builder();
// TODO (trask) extract method for recording exception into instrumentation-api-appender
attributes.put(SemanticAttributes.EXCEPTION_TYPE, throwable.getClass().getName());
attributes.put(SemanticAttributes.EXCEPTION_MESSAGE, throwable.getMessage());
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
attributes.put(SemanticAttributes.EXCEPTION_STACKTRACE, writer.toString());
builder.setAttributes(attributes.build());
}
// span context
builder.setContext(Context.current());
builder.setAttributes(attributes.build());
}
private static Severity levelToSeverity(Level level) {
switch (level.getStandardLevel()) {
case ALL:
return Severity.TRACE;
case TRACE:
return Severity.TRACE2;
return Severity.TRACE;
case DEBUG:
return Severity.DEBUG;
case INFO:

View File

@ -5,7 +5,6 @@
package io.opentelemetry.instrumentation.log4j.v2_16;
import static io.opentelemetry.instrumentation.log4j.v2_16.LogEventMapper.ATTR_THROWABLE_MESSAGE;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import io.opentelemetry.api.common.Attributes;
@ -22,6 +21,7 @@ import io.opentelemetry.sdk.logs.export.InMemoryLogExporter;
import io.opentelemetry.sdk.logs.export.SimpleLogProcessor;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -116,6 +116,12 @@ class OpenTelemetryAppenderConfigTest {
.isLessThan(TimeUnit.MILLISECONDS.toNanos(Instant.now().toEpochMilli()));
assertThat(logData.getSeverity()).isEqualTo(Severity.INFO);
assertThat(logData.getSeverityText()).isEqualTo("INFO");
assertThat(logData.getAttributes()).isEqualTo(Attributes.of(ATTR_THROWABLE_MESSAGE, "Error!"));
assertThat(logData.getAttributes().size()).isEqualTo(3);
assertThat(logData.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE))
.isEqualTo(IllegalStateException.class.getName());
assertThat(logData.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE))
.isEqualTo("Error!");
assertThat(logData.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE))
.contains("logWithExtras");
}
}