Search in sources :

Example 16 with LogData

use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java-instrumentation by open-telemetry.

the class OpenTelemetryAppenderConfigTest method logWithExtras.

@Test
void logWithExtras() {
    Instant start = Instant.now();
    logger.info("log message 1", new IllegalStateException("Error!"));
    List<LogData> logDataList = logExporter.getFinishedLogItems();
    assertThat(logDataList).hasSize(1);
    LogData logData = logDataList.get(0);
    assertThat(logData.getResource()).isEqualTo(resource);
    assertThat(logData.getInstrumentationLibraryInfo()).isEqualTo(instrumentationLibraryInfo);
    assertThat(logData.getBody().asString()).isEqualTo("log message 1");
    assertThat(logData.getEpochNanos()).isGreaterThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(start.toEpochMilli())).isLessThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(Instant.now().toEpochMilli()));
    assertThat(logData.getSeverity()).isEqualTo(Severity.INFO);
    assertThat(logData.getSeverityText()).isEqualTo("INFO");
    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");
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test)

Example 17 with LogData

use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java-instrumentation by open-telemetry.

the class OpenTelemetryAppenderConfigTest method logContextData.

@Test
void logContextData() {
    ThreadContext.put("key1", "val1");
    ThreadContext.put("key2", "val2");
    try {
        logger.info("log message 1");
    } finally {
        ThreadContext.clearMap();
    }
    List<LogData> logDataList = logExporter.getFinishedLogItems();
    assertThat(logDataList).hasSize(1);
    LogData logData = logDataList.get(0);
    assertThat(logData.getResource()).isEqualTo(resource);
    assertThat(logData.getInstrumentationLibraryInfo()).isEqualTo(instrumentationLibraryInfo);
    assertThat(logData.getBody().asString()).isEqualTo("log message 1");
    assertThat(logData.getAttributes().size()).isEqualTo(2);
    assertThat(logData.getAttributes().get(AttributeKey.stringKey("log4j.context_data.key1"))).isEqualTo("val1");
    assertThat(logData.getAttributes().get(AttributeKey.stringKey("log4j.context_data.key2"))).isEqualTo("val2");
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) Test(org.junit.jupiter.api.Test)

Example 18 with LogData

use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java-instrumentation by open-telemetry.

the class OpenTelemetryAppenderConfigTest method logStructuredDataMessage.

@Test
void logStructuredDataMessage() {
    StructuredDataMessage message = new StructuredDataMessage("an id", "a message", "a type");
    message.put("key1", "val1");
    message.put("key2", "val2");
    logger.info(message);
    List<LogData> logDataList = logExporter.getFinishedLogItems();
    assertThat(logDataList).hasSize(1);
    LogData logData = logDataList.get(0);
    assertThat(logData.getResource()).isEqualTo(resource);
    assertThat(logData.getInstrumentationLibraryInfo()).isEqualTo(instrumentationLibraryInfo);
    assertThat(logData.getBody().asString()).isEqualTo("a message");
    assertThat(logData.getAttributes().size()).isEqualTo(2);
    assertThat(logData.getAttributes().get(AttributeKey.stringKey("key1"))).isEqualTo("val1");
    assertThat(logData.getAttributes().get(AttributeKey.stringKey("key2"))).isEqualTo("val2");
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Test(org.junit.jupiter.api.Test)

Example 19 with LogData

use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java-instrumentation by open-telemetry.

the class OpenTelemetryAppenderConfigTest method logNoSpan.

@Test
void logNoSpan() {
    logger.info("log message 1");
    List<LogData> logDataList = logExporter.getFinishedLogItems();
    assertThat(logDataList).hasSize(1);
    LogData logData = logDataList.get(0);
    assertThat(logData.getResource()).isEqualTo(resource);
    assertThat(logData.getInstrumentationLibraryInfo()).isEqualTo(instrumentationLibraryInfo);
    assertThat(logData.getBody().asString()).isEqualTo("log message 1");
    assertThat(logData.getAttributes()).isEqualTo(Attributes.empty());
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) Test(org.junit.jupiter.api.Test)

Example 20 with LogData

use of io.opentelemetry.sdk.logs.data.LogData in project opentelemetry-java-instrumentation by open-telemetry.

the class OpenTelemetryAppenderConfigTest method logContextData.

@Test
void logContextData() {
    MDC.put("key1", "val1");
    MDC.put("key2", "val2");
    try {
        logger.info("log message 1");
    } finally {
        MDC.clear();
    }
    List<LogData> logDataList = logExporter.getFinishedLogItems();
    assertThat(logDataList).hasSize(1);
    LogData logData = logDataList.get(0);
    assertThat(logData.getResource()).isEqualTo(resource);
    assertThat(logData.getInstrumentationLibraryInfo()).isEqualTo(instrumentationLibraryInfo);
    assertThat(logData.getBody().asString()).isEqualTo("log message 1");
    assertThat(logData.getAttributes().size()).isEqualTo(2);
    AssertionsForClassTypes.assertThat(logData.getAttributes().get(AttributeKey.stringKey("logback.mdc.key1"))).isEqualTo("val1");
    AssertionsForClassTypes.assertThat(logData.getAttributes().get(AttributeKey.stringKey("logback.mdc.key2"))).isEqualTo("val2");
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) Test(org.junit.jupiter.api.Test)

Aggregations

LogData (io.opentelemetry.sdk.logs.data.LogData)40 Test (org.junit.jupiter.api.Test)34 Instant (java.time.Instant)8 SdkLogEmitterProvider (io.opentelemetry.sdk.logs.SdkLogEmitterProvider)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Attributes (io.opentelemetry.api.common.Attributes)5 SpanContext (io.opentelemetry.api.trace.SpanContext)4 SuppressLogger (io.opentelemetry.internal.testing.slf4j.SuppressLogger)4 Clock (io.opentelemetry.sdk.common.Clock)4 CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)4 Resource (io.opentelemetry.sdk.resources.Resource)4 TimeUnit (java.util.concurrent.TimeUnit)4 SpanLinkage (com.splunk.opentelemetry.profiler.context.SpanLinkage)3 EventPeriods (com.splunk.opentelemetry.profiler.events.EventPeriods)3 TraceFlags (io.opentelemetry.api.trace.TraceFlags)3 TraceState (io.opentelemetry.api.trace.TraceState)3 LogDataBuilder (io.opentelemetry.sdk.logs.data.LogDataBuilder)3 Mockito.when (org.mockito.Mockito.when)3 CONFIG_KEY_MEMORY_SAMPLER_INTERVAL (com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_MEMORY_SAMPLER_INTERVAL)2 CONFIG_KEY_TLAB_ENABLED (com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_TLAB_ENABLED)2