|
9 | 9 | import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
|
10 | 10 | import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
|
11 | 11 | import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
12 |
| -import static org.assertj.core.api.Assertions.assertThat; |
13 | 12 |
|
14 | 13 | import io.opentelemetry.api.common.AttributeKey;
|
15 | 14 | import io.opentelemetry.api.logs.Severity;
|
| 15 | +import io.opentelemetry.api.trace.SpanContext; |
16 | 16 | import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
|
17 | 17 | import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
|
18 | 18 | import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
|
19 | 19 | import io.opentelemetry.sdk.logs.data.LogRecordData;
|
| 20 | +import io.opentelemetry.sdk.testing.assertj.AttributeAssertion; |
20 | 21 | import io.opentelemetry.semconv.ExceptionAttributes;
|
21 | 22 | import io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes;
|
22 | 23 | import java.time.Instant;
|
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.List; |
23 | 27 | import java.util.stream.Stream;
|
| 28 | +import org.assertj.core.api.AssertAccess; |
24 | 29 | import org.jboss.logmanager.Level;
|
25 | 30 | import org.jboss.logmanager.LogContext;
|
26 | 31 | import org.jboss.logmanager.Logger;
|
@@ -127,40 +132,45 @@ private static void test(
|
127 | 132 | }
|
128 | 133 |
|
129 | 134 | 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 | + }); |
164 | 174 | } else {
|
165 | 175 | Thread.sleep(500); // sleep a bit just to make sure no log is captured
|
166 | 176 | assertThat(testing.logRecords()).isEmpty();
|
@@ -199,17 +209,19 @@ void testMdc() {
|
199 | 209 | MDC.remove("key2");
|
200 | 210 | }
|
201 | 211 |
|
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()))); |
213 | 225 | }
|
214 | 226 |
|
215 | 227 | @FunctionalInterface
|
|
0 commit comments