Skip to content

Commit

Permalink
fix async test
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Nov 8, 2024
1 parent e49e669 commit b357c0e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
Expand Down Expand Up @@ -102,12 +106,10 @@ void logNoSpan() {
.hasInstrumentationScope(instrumentationScopeInfo)
.hasBody("log message 1")
.hasAttributesSatisfyingExactly(
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(CODE_NAMESPACE, AbstractOpenTelemetryAppenderTest.class.getName()),
equalTo(CODE_FUNCTION, "logNoSpan"),
satisfies(CODE_LINENO, AbstractLongAssert::isPositive),
equalTo(CODE_FILEPATH, "AbstractOpenTelemetryAppenderTest.java")));
addLocationAttributes(
"logNoSpan",
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()))));
}

@Test
Expand Down Expand Up @@ -137,15 +139,13 @@ void logWithExtras() {
.hasSeverity(Severity.INFO)
.hasSeverityText("INFO")
.hasAttributesSatisfyingExactly(
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(CODE_NAMESPACE, AbstractOpenTelemetryAppenderTest.class.getName()),
equalTo(CODE_FUNCTION, "logWithExtras"),
satisfies(CODE_LINENO, AbstractLongAssert::isPositive),
equalTo(CODE_FILEPATH, "AbstractOpenTelemetryAppenderTest.java"),
equalTo(EXCEPTION_TYPE, IllegalStateException.class.getName()),
equalTo(EXCEPTION_MESSAGE, "Error!"),
satisfies(EXCEPTION_STACKTRACE, v -> v.contains("logWithExtras")));
addLocationAttributes(
"logWithExtras",
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(EXCEPTION_TYPE, IllegalStateException.class.getName()),
equalTo(EXCEPTION_MESSAGE, "Error!"),
satisfies(EXCEPTION_STACKTRACE, v -> v.contains("logWithExtras"))));

LogRecordData logRecordData = AssertAccess.getActual(logRecord);
assertThat(logRecordData.getTimestampEpochNanos())
Expand Down Expand Up @@ -174,14 +174,12 @@ void logContextData() {
.hasInstrumentationScope(instrumentationScopeInfo)
.hasBody("log message 1")
.hasAttributesSatisfyingExactly(
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(CODE_NAMESPACE, AbstractOpenTelemetryAppenderTest.class.getName()),
equalTo(CODE_FUNCTION, "logContextData"),
satisfies(CODE_LINENO, AbstractLongAssert::isPositive),
equalTo(CODE_FILEPATH, "AbstractOpenTelemetryAppenderTest.java"),
equalTo(stringKey("key1"), "val1"),
equalTo(stringKey("key2"), "val2")));
addLocationAttributes(
"logContextData",
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(stringKey("key1"), "val1"),
equalTo(stringKey("key2"), "val2"))));
}

@Test
Expand All @@ -200,14 +198,12 @@ void logStringMapMessage() {
.hasResource(resource)
.hasInstrumentationScope(instrumentationScopeInfo)
.hasAttributesSatisfyingExactly(
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(CODE_NAMESPACE, AbstractOpenTelemetryAppenderTest.class.getName()),
equalTo(CODE_FUNCTION, "logStringMapMessage"),
satisfies(CODE_LINENO, AbstractLongAssert::isPositive),
equalTo(CODE_FILEPATH, "AbstractOpenTelemetryAppenderTest.java"),
equalTo(stringKey("log4j.map_message.key1"), "val1"),
equalTo(stringKey("log4j.map_message.key2"), "val2")));
addLocationAttributes(
"logStringMapMessage",
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(stringKey("log4j.map_message.key1"), "val1"),
equalTo(stringKey("log4j.map_message.key2"), "val2"))));
}

@Test
Expand All @@ -227,13 +223,11 @@ void logStringMapMessageWithSpecialAttribute() {
.hasInstrumentationScope(instrumentationScopeInfo)
.hasBody("val2")
.hasAttributesSatisfyingExactly(
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(CODE_NAMESPACE, AbstractOpenTelemetryAppenderTest.class.getName()),
equalTo(CODE_FUNCTION, "logStringMapMessageWithSpecialAttribute"),
satisfies(CODE_LINENO, AbstractLongAssert::isPositive),
equalTo(CODE_FILEPATH, "AbstractOpenTelemetryAppenderTest.java"),
equalTo(stringKey("log4j.map_message.key1"), "val1")));
addLocationAttributes(
"logStringMapMessageWithSpecialAttribute",
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(stringKey("log4j.map_message.key1"), "val1"))));
}

@Test
Expand Down Expand Up @@ -268,13 +262,35 @@ void logStructuredDataMessage() {
.hasInstrumentationScope(instrumentationScopeInfo)
.hasBody("a message")
.hasAttributesSatisfyingExactly(
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(CODE_NAMESPACE, AbstractOpenTelemetryAppenderTest.class.getName()),
equalTo(CODE_FUNCTION, "logStructuredDataMessage"),
satisfies(CODE_LINENO, AbstractLongAssert::isPositive),
equalTo(CODE_FILEPATH, "AbstractOpenTelemetryAppenderTest.java"),
equalTo(stringKey("log4j.map_message.key1"), "val1"),
equalTo(stringKey("log4j.map_message.key2"), "val2")));
addLocationAttributes(
"logStructuredDataMessage",
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(stringKey("log4j.map_message.key1"), "val1"),
equalTo(stringKey("log4j.map_message.key2"), "val2"))));
}

private static List<AttributeAssertion> addLocationAttributes(
String methodName, AttributeAssertion... assertions) {
return addLocationAttributes(AbstractOpenTelemetryAppenderTest.class, methodName, assertions);
}

protected static List<AttributeAssertion> addLocationAttributes(
Class<?> testClass, String methodName, AttributeAssertion... assertions) {
String selector = System.getProperty("Log4j2.contextSelector");
boolean async = selector != null && selector.endsWith("AsyncLoggerContextSelector");
if (async) {
// code attributes as not added by default when async logger is used
return Arrays.asList(assertions);
}

List<AttributeAssertion> result = new ArrayList<>(Arrays.asList(assertions));
result.addAll(
Arrays.asList(
equalTo(CODE_NAMESPACE, testClass.getName()),
equalTo(CODE_FUNCTION, methodName),
satisfies(CODE_LINENO, AbstractLongAssert::isPositive),
equalTo(CODE_FILEPATH, testClass.getSimpleName() + ".java")));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FILEPATH;
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION;
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_LINENO;
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE;
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_ID;
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_NAME;

import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import java.util.List;
import org.apache.logging.log4j.message.StringMapMessage;
import org.apache.logging.log4j.message.StructuredDataMessage;
import org.assertj.core.api.AbstractLongAssert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -102,14 +98,12 @@ void twoLogsStringMapMessage() {
.hasResource(resource)
.hasInstrumentationScope(instrumentationScopeInfo)
.hasAttributesSatisfyingExactly(
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(CODE_NAMESPACE, LogReplayOpenTelemetryAppenderTest.class.getName()),
equalTo(CODE_FUNCTION, "twoLogsStringMapMessage"),
satisfies(CODE_LINENO, AbstractLongAssert::isPositive),
equalTo(CODE_FILEPATH, "LogReplayOpenTelemetryAppenderTest.java"),
equalTo(stringKey("log4j.map_message.key1"), "val1"),
equalTo(stringKey("log4j.map_message.key2"), "val2")));
addLocationAttributes(
"twoLogsStringMapMessage",
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(stringKey("log4j.map_message.key1"), "val1"),
equalTo(stringKey("log4j.map_message.key2"), "val2"))));
}

@Test
Expand Down Expand Up @@ -138,13 +132,16 @@ void twoLogsStructuredDataMessage() {
.hasInstrumentationScope(instrumentationScopeInfo)
.hasBody("a message")
.hasAttributesSatisfyingExactly(
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(CODE_NAMESPACE, LogReplayOpenTelemetryAppenderTest.class.getName()),
equalTo(CODE_FUNCTION, "twoLogsStructuredDataMessage"),
satisfies(CODE_LINENO, AbstractLongAssert::isPositive),
equalTo(CODE_FILEPATH, "LogReplayOpenTelemetryAppenderTest.java"),
equalTo(stringKey("log4j.map_message.key1"), "val1"),
equalTo(stringKey("log4j.map_message.key2"), "val2")));
addLocationAttributes(
"twoLogsStructuredDataMessage",
equalTo(THREAD_NAME, Thread.currentThread().getName()),
equalTo(THREAD_ID, Thread.currentThread().getId()),
equalTo(stringKey("log4j.map_message.key1"), "val1"),
equalTo(stringKey("log4j.map_message.key2"), "val2"))));
}

private static List<AttributeAssertion> addLocationAttributes(
String methodName, AttributeAssertion... assertions) {
return addLocationAttributes(LogReplayOpenTelemetryAppenderTest.class, methodName, assertions);
}
}

0 comments on commit b357c0e

Please sign in to comment.