Search in sources :

Example 1 with LogBuilder

use of io.opentelemetry.instrumentation.api.appender.internal.LogBuilder in project opentelemetry-java-instrumentation by open-telemetry.

the class Log4jHelper method capture.

public static void capture(Logger logger, Level level, Message message, Throwable throwable) {
    String instrumentationName = logger.getName();
    if (instrumentationName == null || instrumentationName.isEmpty()) {
        instrumentationName = "ROOT";
    }
    LogBuilder builder = AgentLogEmitterProvider.get().logEmitterBuilder(instrumentationName).build().logBuilder();
    Map<String, String> contextData = ThreadContext.getImmutableContext();
    mapper.mapLogEvent(builder, message, level, throwable, null, contextData);
    builder.emit();
}
Also used : LogBuilder(io.opentelemetry.instrumentation.api.appender.internal.LogBuilder)

Example 2 with LogBuilder

use of io.opentelemetry.instrumentation.api.appender.internal.LogBuilder in project opentelemetry-java-instrumentation by open-telemetry.

the class LoggingEventMapper method emit.

public void emit(LogEmitterProvider logEmitterProvider, ILoggingEvent event) {
    String instrumentationName = event.getLoggerName();
    if (instrumentationName == null || instrumentationName.isEmpty()) {
        instrumentationName = "ROOT";
    }
    LogBuilder builder = logEmitterProvider.logEmitterBuilder(instrumentationName).build().logBuilder();
    mapLoggingEvent(builder, event);
    builder.emit();
}
Also used : LogBuilder(io.opentelemetry.instrumentation.api.appender.internal.LogBuilder)

Example 3 with LogBuilder

use of io.opentelemetry.instrumentation.api.appender.internal.LogBuilder in project opentelemetry-java-instrumentation by open-telemetry.

the class OpenTelemetryAppender method append.

@Override
public void append(LogEvent event) {
    String instrumentationName = event.getLoggerName();
    if (instrumentationName == null || instrumentationName.isEmpty()) {
        instrumentationName = "ROOT";
    }
    LogBuilder builder = logEmitterProviderHolder.get().logEmitterBuilder(instrumentationName).build().logBuilder();
    ReadOnlyStringMap contextData = event.getContextData();
    mapper.mapLogEvent(builder, event.getMessage(), event.getLevel(), event.getThrown(), event.getInstant(), contextData);
    builder.emit();
}
Also used : ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) LogBuilder(io.opentelemetry.instrumentation.api.appender.internal.LogBuilder)

Example 4 with LogBuilder

use of io.opentelemetry.instrumentation.api.appender.internal.LogBuilder in project opentelemetry-java-instrumentation by open-telemetry.

the class LogEventMapperTest method testCaptureStructuredDataMessage.

@Test
void testCaptureStructuredDataMessage() {
    // given
    LogEventMapper<Map<String, String>> mapper = new LogEventMapper<>(ContextDataAccessorImpl.INSTANCE, true, singletonList("*"));
    StructuredDataMessage message = new StructuredDataMessage("an id", "a message", "a type");
    message.put("key1", "value1");
    message.put("message", "value2");
    LogBuilder logBuilder = mock(LogBuilder.class);
    AttributesBuilder attributes = Attributes.builder();
    // when
    mapper.captureMessage(logBuilder, attributes, message);
    // then
    verify(logBuilder).setBody("a message");
    assertThat(attributes.build()).containsOnly(entry(AttributeKey.stringKey("key1"), "value1"), entry(AttributeKey.stringKey("message"), "value2"));
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) LogBuilder(io.opentelemetry.instrumentation.api.appender.internal.LogBuilder) HashMap(java.util.HashMap) Map(java.util.Map) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.Test)

Example 5 with LogBuilder

use of io.opentelemetry.instrumentation.api.appender.internal.LogBuilder in project opentelemetry-java-instrumentation by open-telemetry.

the class LogEventMapperTest method testCaptureMapMessageDisabled.

@Test
void testCaptureMapMessageDisabled() {
    // given
    LogEventMapper<Map<String, String>> mapper = new LogEventMapper<>(ContextDataAccessorImpl.INSTANCE, false, singletonList("*"));
    StringMapMessage message = new StringMapMessage();
    message.put("key1", "value1");
    message.put("message", "value2");
    LogBuilder logBuilder = mock(LogBuilder.class);
    AttributesBuilder attributes = Attributes.builder();
    // when
    mapper.captureMessage(logBuilder, attributes, message);
    // then
    verify(logBuilder).setBody("value2");
    assertThat(attributes.build()).isEmpty();
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogBuilder(io.opentelemetry.instrumentation.api.appender.internal.LogBuilder) HashMap(java.util.HashMap) Map(java.util.Map) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.Test)

Aggregations

LogBuilder (io.opentelemetry.instrumentation.api.appender.internal.LogBuilder)9 AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Test (org.junit.Test)4 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)3 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)1 ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)1