Skip to content

Commit c8e2222

Browse files
authored
Make log record asserts similar to trace asserts (#12164)
1 parent 9dd11ff commit c8e2222

File tree

14 files changed

+690
-616
lines changed

14 files changed

+690
-616
lines changed

instrumentation/jboss-logmanager/jboss-logmanager-appender-1.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jbosslogmanager/appender/v1_1/JbossLogmanagerTest.java

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@
99
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
1010
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
1111
import static java.util.concurrent.TimeUnit.MILLISECONDS;
12-
import static org.assertj.core.api.Assertions.assertThat;
1312

1413
import io.opentelemetry.api.common.AttributeKey;
1514
import io.opentelemetry.api.logs.Severity;
15+
import io.opentelemetry.api.trace.SpanContext;
1616
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
1717
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
1818
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
1919
import io.opentelemetry.sdk.logs.data.LogRecordData;
20+
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
2021
import io.opentelemetry.semconv.ExceptionAttributes;
2122
import io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes;
2223
import java.time.Instant;
24+
import java.util.ArrayList;
25+
import java.util.Arrays;
26+
import java.util.List;
2327
import java.util.stream.Stream;
28+
import org.assertj.core.api.AssertAccess;
2429
import org.jboss.logmanager.Level;
2530
import org.jboss.logmanager.LogContext;
2631
import org.jboss.logmanager.Logger;
@@ -127,40 +132,45 @@ private static void test(
127132
}
128133

129134
if (expectedSeverity != null) {
130-
LogRecordData log = testing.waitForLogRecords(1).get(0);
131-
assertThat(log)
132-
.hasBody(withParam ? "xyz: 123" : "xyz")
133-
.hasInstrumentationScope(InstrumentationScopeInfo.builder(expectedLoggerName).build())
134-
.hasSeverity(expectedSeverity)
135-
.hasSeverityText(expectedSeverityText);
136-
137-
assertThat(log.getTimestampEpochNanos())
138-
.isGreaterThanOrEqualTo(MILLISECONDS.toNanos(start.toEpochMilli()))
139-
.isLessThanOrEqualTo(MILLISECONDS.toNanos(Instant.now().toEpochMilli()));
140-
141-
if (logException) {
142-
assertThat(log)
143-
.hasAttributesSatisfyingExactly(
144-
equalTo(ThreadIncubatingAttributes.THREAD_NAME, Thread.currentThread().getName()),
145-
equalTo(ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId()),
146-
equalTo(ExceptionAttributes.EXCEPTION_TYPE, IllegalStateException.class.getName()),
147-
equalTo(ExceptionAttributes.EXCEPTION_MESSAGE, "hello"),
148-
satisfies(
149-
ExceptionAttributes.EXCEPTION_STACKTRACE,
150-
v -> v.contains(JbossLogmanagerTest.class.getName())));
151-
} else {
152-
assertThat(log)
153-
.hasAttributesSatisfyingExactly(
154-
equalTo(ThreadIncubatingAttributes.THREAD_NAME, Thread.currentThread().getName()),
155-
equalTo(ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId()));
156-
}
157-
158-
if (withParent) {
159-
assertThat(log).hasSpanContext(testing.spans().get(0).getSpanContext());
160-
} else {
161-
assertThat(log.getSpanContext().isValid()).isFalse();
162-
}
163-
135+
testing.waitAndAssertLogRecords(
136+
logRecord -> {
137+
logRecord
138+
.hasBody(withParam ? "xyz: 123" : "xyz")
139+
.hasInstrumentationScope(
140+
InstrumentationScopeInfo.builder(expectedLoggerName).build())
141+
.hasSeverity(expectedSeverity)
142+
.hasSeverityText(expectedSeverityText)
143+
.hasSpanContext(
144+
withParent
145+
? testing.spans().get(0).getSpanContext()
146+
: SpanContext.getInvalid());
147+
148+
List<AttributeAssertion> attributeAsserts =
149+
new ArrayList<>(
150+
Arrays.asList(
151+
equalTo(
152+
ThreadIncubatingAttributes.THREAD_NAME,
153+
Thread.currentThread().getName()),
154+
equalTo(
155+
ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId())));
156+
if (logException) {
157+
attributeAsserts.addAll(
158+
Arrays.asList(
159+
equalTo(
160+
ExceptionAttributes.EXCEPTION_TYPE,
161+
IllegalStateException.class.getName()),
162+
equalTo(ExceptionAttributes.EXCEPTION_MESSAGE, "hello"),
163+
satisfies(
164+
ExceptionAttributes.EXCEPTION_STACKTRACE,
165+
v -> v.contains(JbossLogmanagerTest.class.getName()))));
166+
}
167+
logRecord.hasAttributesSatisfyingExactly(attributeAsserts);
168+
169+
LogRecordData logRecordData = AssertAccess.getActual(logRecord);
170+
assertThat(logRecordData.getTimestampEpochNanos())
171+
.isGreaterThanOrEqualTo(MILLISECONDS.toNanos(start.toEpochMilli()))
172+
.isLessThanOrEqualTo(MILLISECONDS.toNanos(Instant.now().toEpochMilli()));
173+
});
164174
} else {
165175
Thread.sleep(500); // sleep a bit just to make sure no log is captured
166176
assertThat(testing.logRecords()).isEmpty();
@@ -199,17 +209,19 @@ void testMdc() {
199209
MDC.remove("key2");
200210
}
201211

202-
LogRecordData log = testing.waitForLogRecords(1).get(0);
203-
assertThat(log)
204-
.hasBody("xyz")
205-
.hasInstrumentationScope(InstrumentationScopeInfo.builder("abc").build())
206-
.hasSeverity(Severity.INFO)
207-
.hasSeverityText("INFO")
208-
.hasAttributesSatisfyingExactly(
209-
equalTo(AttributeKey.stringKey("key1"), "val1"),
210-
equalTo(AttributeKey.stringKey("key2"), "val2"),
211-
equalTo(ThreadIncubatingAttributes.THREAD_NAME, Thread.currentThread().getName()),
212-
equalTo(ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId()));
212+
testing.waitAndAssertLogRecords(
213+
logRecord ->
214+
logRecord
215+
.hasBody("xyz")
216+
.hasInstrumentationScope(InstrumentationScopeInfo.builder("abc").build())
217+
.hasSeverity(Severity.INFO)
218+
.hasSeverityText("INFO")
219+
.hasAttributesSatisfyingExactly(
220+
equalTo(AttributeKey.stringKey("key1"), "val1"),
221+
equalTo(AttributeKey.stringKey("key2"), "val2"),
222+
equalTo(
223+
ThreadIncubatingAttributes.THREAD_NAME, Thread.currentThread().getName()),
224+
equalTo(ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId())));
213225
}
214226

215227
@FunctionalInterface

instrumentation/log4j/log4j-appender-1.2/javaagent/src/test/java/io/opentelemetry/instrumentation/log4j/appender/v1_2/Log4j1Test.java

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@
1212

1313
import io.opentelemetry.api.common.AttributeKey;
1414
import io.opentelemetry.api.logs.Severity;
15+
import io.opentelemetry.api.trace.SpanContext;
1516
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
1617
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
1718
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
1819
import io.opentelemetry.sdk.logs.data.LogRecordData;
20+
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
1921
import io.opentelemetry.semconv.ExceptionAttributes;
2022
import io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes;
2123
import java.lang.reflect.Field;
2224
import java.time.Instant;
25+
import java.util.ArrayList;
26+
import java.util.Arrays;
27+
import java.util.List;
2328
import java.util.stream.Stream;
2429
import org.apache.log4j.Logger;
2530
import org.apache.log4j.MDC;
2631
import org.apache.log4j.helpers.Loader;
32+
import org.assertj.core.api.AssertAccess;
2733
import org.junit.jupiter.api.Test;
2834
import org.junit.jupiter.api.extension.RegisterExtension;
2935
import org.junit.jupiter.params.ParameterizedTest;
@@ -98,40 +104,45 @@ private static void test(
98104
}
99105

100106
if (expectedSeverity != null) {
101-
LogRecordData log = testing.waitForLogRecords(1).get(0);
102-
assertThat(log)
103-
.hasBody("xyz")
104-
.hasInstrumentationScope(InstrumentationScopeInfo.builder(expectedLoggerName).build())
105-
.hasSeverity(expectedSeverity)
106-
.hasSeverityText(expectedSeverityText);
107-
108-
assertThat(log.getTimestampEpochNanos())
109-
.isGreaterThanOrEqualTo(MILLISECONDS.toNanos(start.toEpochMilli()))
110-
.isLessThanOrEqualTo(MILLISECONDS.toNanos(Instant.now().toEpochMilli()));
111-
112-
if (logException) {
113-
assertThat(log)
114-
.hasAttributesSatisfyingExactly(
115-
equalTo(ThreadIncubatingAttributes.THREAD_NAME, Thread.currentThread().getName()),
116-
equalTo(ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId()),
117-
equalTo(ExceptionAttributes.EXCEPTION_TYPE, IllegalStateException.class.getName()),
118-
equalTo(ExceptionAttributes.EXCEPTION_MESSAGE, "hello"),
119-
satisfies(
120-
ExceptionAttributes.EXCEPTION_STACKTRACE,
121-
v -> v.contains(Log4j1Test.class.getName())));
122-
} else {
123-
assertThat(log)
124-
.hasAttributesSatisfyingExactly(
125-
equalTo(ThreadIncubatingAttributes.THREAD_NAME, Thread.currentThread().getName()),
126-
equalTo(ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId()));
127-
}
128-
129-
if (withParent) {
130-
assertThat(log).hasSpanContext(testing.spans().get(0).getSpanContext());
131-
} else {
132-
assertThat(log.getSpanContext().isValid()).isFalse();
133-
}
134-
107+
testing.waitAndAssertLogRecords(
108+
logRecord -> {
109+
logRecord
110+
.hasBody("xyz")
111+
.hasInstrumentationScope(
112+
InstrumentationScopeInfo.builder(expectedLoggerName).build())
113+
.hasSeverity(expectedSeverity)
114+
.hasSeverityText(expectedSeverityText)
115+
.hasSpanContext(
116+
withParent
117+
? testing.spans().get(0).getSpanContext()
118+
: SpanContext.getInvalid());
119+
120+
List<AttributeAssertion> attributeAsserts =
121+
new ArrayList<>(
122+
Arrays.asList(
123+
equalTo(
124+
ThreadIncubatingAttributes.THREAD_NAME,
125+
Thread.currentThread().getName()),
126+
equalTo(
127+
ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId())));
128+
if (logException) {
129+
attributeAsserts.addAll(
130+
Arrays.asList(
131+
equalTo(
132+
ExceptionAttributes.EXCEPTION_TYPE,
133+
IllegalStateException.class.getName()),
134+
equalTo(ExceptionAttributes.EXCEPTION_MESSAGE, "hello"),
135+
satisfies(
136+
ExceptionAttributes.EXCEPTION_STACKTRACE,
137+
v -> v.contains(Log4j1Test.class.getName()))));
138+
}
139+
logRecord.hasAttributesSatisfyingExactly(attributeAsserts);
140+
141+
LogRecordData logRecordData = AssertAccess.getActual(logRecord);
142+
assertThat(logRecordData.getTimestampEpochNanos())
143+
.isGreaterThanOrEqualTo(MILLISECONDS.toNanos(start.toEpochMilli()))
144+
.isLessThanOrEqualTo(MILLISECONDS.toNanos(Instant.now().toEpochMilli()));
145+
});
135146
} else {
136147
Thread.sleep(500); // sleep a bit just to make sure no log is captured
137148
assertThat(testing.logRecords()).isEmpty();
@@ -149,17 +160,19 @@ void testMdc() {
149160
MDC.remove("key2");
150161
}
151162

152-
LogRecordData log = testing.waitForLogRecords(1).get(0);
153-
assertThat(log)
154-
.hasBody("xyz")
155-
.hasInstrumentationScope(InstrumentationScopeInfo.builder("abc").build())
156-
.hasSeverity(Severity.INFO)
157-
.hasSeverityText("INFO")
158-
.hasAttributesSatisfyingExactly(
159-
equalTo(AttributeKey.stringKey("key1"), "val1"),
160-
equalTo(AttributeKey.stringKey("key2"), "val2"),
161-
equalTo(ThreadIncubatingAttributes.THREAD_NAME, Thread.currentThread().getName()),
162-
equalTo(ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId()));
163+
testing.waitAndAssertLogRecords(
164+
logRecord ->
165+
logRecord
166+
.hasBody("xyz")
167+
.hasInstrumentationScope(InstrumentationScopeInfo.builder("abc").build())
168+
.hasSeverity(Severity.INFO)
169+
.hasSeverityText("INFO")
170+
.hasAttributesSatisfyingExactly(
171+
equalTo(AttributeKey.stringKey("key1"), "val1"),
172+
equalTo(AttributeKey.stringKey("key2"), "val2"),
173+
equalTo(
174+
ThreadIncubatingAttributes.THREAD_NAME, Thread.currentThread().getName()),
175+
equalTo(ThreadIncubatingAttributes.THREAD_ID, Thread.currentThread().getId())));
163176
}
164177

165178
private static void performLogging(

0 commit comments

Comments
 (0)