|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.api.incubator.semconv.faas.internal; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.api.internal.SemconvExceptionSignal.emitExceptionAsLogs; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; |
| 10 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 11 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; |
| 12 | +import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_MESSAGE; |
| 13 | +import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_STACKTRACE; |
| 14 | +import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_TYPE; |
| 15 | + |
| 16 | +import io.opentelemetry.api.logs.Severity; |
| 17 | +import io.opentelemetry.context.Context; |
| 18 | +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; |
| 19 | +import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; |
| 20 | +import io.opentelemetry.sdk.logs.data.LogRecordData; |
| 21 | +import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; |
| 22 | +import java.util.List; |
| 23 | +import org.assertj.core.api.AbstractAssert; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 26 | + |
| 27 | +class FaasExceptionEventExtractorsTest { |
| 28 | + |
| 29 | + @RegisterExtension |
| 30 | + static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); |
| 31 | + |
| 32 | + @Test |
| 33 | + void faasInvocationExceptionLog() { |
| 34 | + InstrumenterBuilder<String, String> builder = |
| 35 | + Instrumenter.builder(otelTesting.getOpenTelemetry(), "test", unused -> "span"); |
| 36 | + FaasExceptionEventExtractors.setFaasInvocationExceptionEventExtractor(builder); |
| 37 | + Instrumenter<String, String> instrumenter = builder.buildInstrumenter(); |
| 38 | + |
| 39 | + Context context = instrumenter.start(Context.root(), "request"); |
| 40 | + IllegalStateException error = new IllegalStateException("test"); |
| 41 | + instrumenter.end(context, "request", "response", error); |
| 42 | + |
| 43 | + List<LogRecordData> logs = otelTesting.getLogRecords(); |
| 44 | + if (emitExceptionAsLogs()) { |
| 45 | + assertThat(logs).hasSize(1); |
| 46 | + assertThat(logs.get(0)) |
| 47 | + .hasSeverity(Severity.ERROR) |
| 48 | + .hasEventName("faas.invocation.exception") |
| 49 | + .hasAttributesSatisfyingExactly( |
| 50 | + equalTo(EXCEPTION_TYPE, "java.lang.IllegalStateException"), |
| 51 | + equalTo(EXCEPTION_MESSAGE, "test"), |
| 52 | + satisfies(EXCEPTION_STACKTRACE, AbstractAssert::isNotNull)); |
| 53 | + } else { |
| 54 | + assertThat(logs).isEmpty(); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments