|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0 |
| 7 | + |
| 8 | +import io.opentelemetry.api.trace.SpanKind |
| 9 | +import io.opentelemetry.instrumentation.testing.junit.{ |
| 10 | + AgentInstrumentationExtension, |
| 11 | + InstrumentationExtension |
| 12 | +} |
| 13 | +import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo |
| 14 | +import io.opentelemetry.sdk.testing.assertj.{SpanDataAssert, TraceAssert} |
| 15 | +import io.opentelemetry.semconv.HttpAttributes |
| 16 | +import org.apache.pekko.actor.ActorSystem |
| 17 | +import org.apache.pekko.http.scaladsl.Http |
| 18 | +import org.apache.pekko.http.scaladsl.model.{HttpRequest, HttpResponse} |
| 19 | +import org.assertj.core.api.Assertions.assertThat |
| 20 | +import org.junit.jupiter.api.Test |
| 21 | +import org.junit.jupiter.api.extension.RegisterExtension |
| 22 | + |
| 23 | +import java.io.{BufferedReader, InputStreamReader, OutputStreamWriter} |
| 24 | +import java.net.{ServerSocket, Socket} |
| 25 | +import java.nio.charset.StandardCharsets |
| 26 | +import java.util.function.Consumer |
| 27 | +import scala.collection.mutable.ListBuffer |
| 28 | +import scala.concurrent.Await |
| 29 | +import scala.concurrent.duration._ |
| 30 | + |
| 31 | +/** A request that pekko-http rejects while parsing it never becomes an |
| 32 | + * HttpRequest and never reaches the user handler, so it is traced through the |
| 33 | + * parsing error handler rather than through the regular server |
| 34 | + * instrumentation. |
| 35 | + */ |
| 36 | +class PekkoHttpServerParsingErrorTest { |
| 37 | + |
| 38 | + @RegisterExtension val testing: InstrumentationExtension = |
| 39 | + AgentInstrumentationExtension.create() |
| 40 | + |
| 41 | + @Test def illegalRequestTargetIsTraced(): Unit = { |
| 42 | + // this test does not share the web server used by the other tests, it needs a server that is |
| 43 | + // guaranteed to be running and no other test may terminate its actor system |
| 44 | + implicit val system: ActorSystem = ActorSystem("parsing-error-test") |
| 45 | + try { |
| 46 | + val port = { |
| 47 | + val socket = new ServerSocket(0) |
| 48 | + try socket.getLocalPort |
| 49 | + finally socket.close() |
| 50 | + } |
| 51 | + val handler: HttpRequest => HttpResponse = _ => HttpResponse() |
| 52 | + val binding = |
| 53 | + Await.result( |
| 54 | + Http().bindAndHandleSync(handler, "localhost", port), |
| 55 | + 10.seconds |
| 56 | + ) |
| 57 | + |
| 58 | + try { |
| 59 | + val response = send( |
| 60 | + port, |
| 61 | + "GET /%% HTTP/1.1\r\nHost: localhost:" + port + "\r\n\r\n" |
| 62 | + ) |
| 63 | + assertThat(response.head).contains("400 Bad Request") |
| 64 | + |
| 65 | + testing.waitAndAssertTraces(new Consumer[TraceAssert] { |
| 66 | + override def accept(trace: TraceAssert): Unit = |
| 67 | + trace.hasSpansSatisfyingExactly(new Consumer[SpanDataAssert] { |
| 68 | + override def accept(span: SpanDataAssert): Unit = { |
| 69 | + span |
| 70 | + .hasName("HTTP") |
| 71 | + .hasKind(SpanKind.SERVER) |
| 72 | + .hasNoParent() |
| 73 | + .hasAttributesSatisfying( |
| 74 | + equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "_OTHER"), |
| 75 | + equalTo( |
| 76 | + HttpAttributes.HTTP_RESPONSE_STATUS_CODE, |
| 77 | + java.lang.Long.valueOf(400) |
| 78 | + ) |
| 79 | + ) |
| 80 | + () |
| 81 | + } |
| 82 | + }) |
| 83 | + }) |
| 84 | + } finally Await.result(binding.unbind(), 10.seconds) |
| 85 | + } finally Await.result(system.terminate(), 10.seconds) |
| 86 | + } |
| 87 | + |
| 88 | + private def send(port: Int, request: String): List[String] = { |
| 89 | + val socket = new Socket("localhost", port) |
| 90 | + try { |
| 91 | + val out = |
| 92 | + new OutputStreamWriter( |
| 93 | + socket.getOutputStream, |
| 94 | + StandardCharsets.US_ASCII |
| 95 | + ) |
| 96 | + out.write(request) |
| 97 | + out.flush() |
| 98 | + |
| 99 | + val in = new BufferedReader( |
| 100 | + new InputStreamReader(socket.getInputStream, StandardCharsets.US_ASCII) |
| 101 | + ) |
| 102 | + val lines = ListBuffer[String]() |
| 103 | + var line = in.readLine() |
| 104 | + while (line != null) { |
| 105 | + lines += line |
| 106 | + line = in.readLine() |
| 107 | + } |
| 108 | + lines.toList |
| 109 | + } finally socket.close() |
| 110 | + } |
| 111 | +} |
0 commit comments