|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.awslambdacore.v1_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | +import static org.assertj.core.api.Assertions.catchThrowable; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | + |
| 13 | +import com.amazonaws.services.lambda.runtime.Context; |
| 14 | +import com.amazonaws.services.lambda.runtime.RequestStreamHandler; |
| 15 | +import io.opentelemetry.api.trace.SpanKind; |
| 16 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 17 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 18 | +import io.opentelemetry.sdk.trace.data.StatusData; |
| 19 | +import io.opentelemetry.semconv.incubating.FaasIncubatingAttributes; |
| 20 | +import java.io.BufferedReader; |
| 21 | +import java.io.BufferedWriter; |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.io.InputStreamReader; |
| 27 | +import java.io.OutputStream; |
| 28 | +import java.io.OutputStreamWriter; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import org.junit.jupiter.api.AfterEach; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 34 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 35 | +import org.mockito.Mock; |
| 36 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 37 | + |
| 38 | +@ExtendWith(MockitoExtension.class) |
| 39 | +public class AwsLambdaStreamHandlerTest { |
| 40 | + |
| 41 | + @RegisterExtension |
| 42 | + public static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 43 | + |
| 44 | + @Mock private Context context; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void setUp() { |
| 48 | + when(context.getFunctionName()).thenReturn("my_function"); |
| 49 | + when(context.getAwsRequestId()).thenReturn("1-22-333"); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterEach |
| 53 | + void tearDown() { |
| 54 | + assertThat(testing.forceFlushCalled()).isTrue(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void handlerTraced() throws Exception { |
| 59 | + InputStream input = new ByteArrayInputStream("hello\n".getBytes(StandardCharsets.UTF_8)); |
| 60 | + OutputStream output = new ByteArrayOutputStream(); |
| 61 | + RequestStreamHandlerTestImpl handler = new RequestStreamHandlerTestImpl(); |
| 62 | + handler.handleRequest(input, output, context); |
| 63 | + |
| 64 | + testing.waitAndAssertTraces( |
| 65 | + trace -> |
| 66 | + trace.hasSpansSatisfyingExactly( |
| 67 | + span -> |
| 68 | + span.hasName("my_function") |
| 69 | + .hasKind(SpanKind.SERVER) |
| 70 | + .hasAttributesSatisfyingExactly( |
| 71 | + equalTo(FaasIncubatingAttributes.FAAS_INVOCATION_ID, "1-22-333")))); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void handlerTracedWithException() { |
| 76 | + InputStream input = new ByteArrayInputStream("bye\n".getBytes(StandardCharsets.UTF_8)); |
| 77 | + OutputStream output = new ByteArrayOutputStream(); |
| 78 | + RequestStreamHandlerTestImpl handler = new RequestStreamHandlerTestImpl(); |
| 79 | + |
| 80 | + Throwable thrown = catchThrowable(() -> handler.handleRequest(input, output, context)); |
| 81 | + assertThat(thrown).isInstanceOf(IllegalArgumentException.class); |
| 82 | + |
| 83 | + testing.waitAndAssertTraces( |
| 84 | + trace -> |
| 85 | + trace.hasSpansSatisfyingExactly( |
| 86 | + span -> |
| 87 | + span.hasName("my_function") |
| 88 | + .hasKind(SpanKind.SERVER) |
| 89 | + .hasStatus(StatusData.error()) |
| 90 | + .hasException(thrown) |
| 91 | + .hasAttributesSatisfyingExactly( |
| 92 | + equalTo(FaasIncubatingAttributes.FAAS_INVOCATION_ID, "1-22-333")))); |
| 93 | + } |
| 94 | + |
| 95 | + static final class RequestStreamHandlerTestImpl implements RequestStreamHandler { |
| 96 | + @Override |
| 97 | + public void handleRequest(InputStream input, OutputStream output, Context context) |
| 98 | + throws IOException { |
| 99 | + BufferedReader reader = |
| 100 | + new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); |
| 101 | + BufferedWriter writer = |
| 102 | + new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8)); |
| 103 | + String line = reader.readLine(); |
| 104 | + if (line.equals("hello")) { |
| 105 | + writer.write("world"); |
| 106 | + writer.flush(); |
| 107 | + writer.close(); |
| 108 | + } else { |
| 109 | + throw new IllegalArgumentException("bad argument"); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments