|
5 | 5 |
|
6 | 6 | package io.opentelemetry.instrumentation.awslambdacore.v1_0; |
7 | 7 |
|
| 8 | +import static io.opentelemetry.instrumentation.api.internal.SemconvExceptionSignal.emitExceptionAsLogs; |
| 9 | +import static io.opentelemetry.instrumentation.api.internal.SemconvExceptionSignal.emitExceptionAsSpanEvents; |
8 | 10 | import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; |
9 | 11 | import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 12 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; |
| 13 | +import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_MESSAGE; |
| 14 | +import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_STACKTRACE; |
| 15 | +import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_TYPE; |
10 | 16 | import static io.opentelemetry.semconv.incubating.FaasIncubatingAttributes.FAAS_INVOCATION_ID; |
| 17 | +import static java.util.stream.Collectors.toList; |
11 | 18 | import static org.assertj.core.api.Assertions.catchThrowable; |
12 | 19 | import static org.mockito.Mockito.when; |
13 | 20 |
|
14 | 21 | import com.amazonaws.services.lambda.runtime.Context; |
15 | 22 | import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 23 | +import io.opentelemetry.api.logs.Severity; |
16 | 24 | import io.opentelemetry.api.trace.SpanKind; |
17 | 25 | import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 26 | +import io.opentelemetry.sdk.logs.data.LogRecordData; |
18 | 27 | import io.opentelemetry.sdk.trace.data.StatusData; |
| 28 | +import java.util.List; |
| 29 | +import org.awaitility.Awaitility; |
19 | 30 | import org.junit.jupiter.api.AfterEach; |
20 | 31 | import org.junit.jupiter.api.BeforeEach; |
21 | 32 | import org.junit.jupiter.api.Test; |
@@ -80,13 +91,36 @@ void handlerTracedWithException() { |
80 | 91 | .waitAndAssertTraces( |
81 | 92 | trace -> |
82 | 93 | trace.hasSpansSatisfyingExactly( |
83 | | - span -> |
84 | | - span.hasName("my_function") |
85 | | - .hasKind(SpanKind.SERVER) |
86 | | - .hasStatus(StatusData.error()) |
87 | | - .hasException(thrown) |
88 | | - .hasAttributesSatisfyingExactly( |
89 | | - equalTo(FAAS_INVOCATION_ID, "1-22-333")))); |
| 94 | + span -> { |
| 95 | + span.hasName("my_function") |
| 96 | + .hasKind(SpanKind.SERVER) |
| 97 | + .hasStatus(StatusData.error()) |
| 98 | + .hasAttributesSatisfyingExactly(equalTo(FAAS_INVOCATION_ID, "1-22-333")); |
| 99 | + span.hasException(emitExceptionAsSpanEvents() ? thrown : null); |
| 100 | + })); |
| 101 | + |
| 102 | + if (emitExceptionAsLogs()) { |
| 103 | + assertInvocationExceptionLog(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private void assertInvocationExceptionLog() { |
| 108 | + Awaitility.await() |
| 109 | + .untilAsserted( |
| 110 | + () -> { |
| 111 | + List<LogRecordData> logs = |
| 112 | + testing().logRecords().stream() |
| 113 | + .filter(log -> "faas.invocation.exception".equals(log.getEventName())) |
| 114 | + .collect(toList()); |
| 115 | + assertThat(logs).hasSize(1); |
| 116 | + assertThat(logs.get(0)) |
| 117 | + .hasSeverity(Severity.ERROR) |
| 118 | + .hasEventName("faas.invocation.exception") |
| 119 | + .hasAttributesSatisfyingExactly( |
| 120 | + satisfies(EXCEPTION_TYPE, val -> val.isNotNull()), |
| 121 | + satisfies(EXCEPTION_MESSAGE, val -> val.isNotNull()), |
| 122 | + satisfies(EXCEPTION_STACKTRACE, val -> val.isNotNull())); |
| 123 | + }); |
90 | 124 | } |
91 | 125 |
|
92 | 126 | /** |
|
0 commit comments