|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.ktor.v3_0 |
| 7 | + |
| 8 | +import io.ktor.http.* |
| 9 | +import io.ktor.server.application.* |
| 10 | +import io.ktor.server.engine.* |
| 11 | +import io.ktor.server.netty.* |
| 12 | +import io.ktor.server.response.* |
| 13 | +import io.ktor.server.routing.* |
| 14 | +import io.opentelemetry.instrumentation.ktor.v2_0.common.internal.Experimental |
| 15 | +import io.opentelemetry.instrumentation.ktor.v3_0.InstrumentationProperties.INSTRUMENTATION_NAME |
| 16 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension |
| 17 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerUsingTest |
| 18 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension |
| 19 | +import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint |
| 20 | +import io.opentelemetry.sdk.metrics.data.MetricData |
| 21 | +import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions |
| 22 | +import io.opentelemetry.semconv.HttpAttributes |
| 23 | +import io.opentelemetry.semconv.UrlAttributes |
| 24 | +import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpRequest |
| 25 | +import io.opentelemetry.testing.internal.armeria.common.HttpMethod |
| 26 | +import org.assertj.core.api.ThrowingConsumer |
| 27 | +import org.junit.jupiter.api.AfterAll |
| 28 | +import org.junit.jupiter.api.BeforeAll |
| 29 | +import org.junit.jupiter.api.extension.RegisterExtension |
| 30 | +import org.junit.jupiter.params.ParameterizedTest |
| 31 | +import org.junit.jupiter.params.provider.Arguments |
| 32 | +import org.junit.jupiter.params.provider.Arguments.arguments |
| 33 | +import org.junit.jupiter.params.provider.MethodSource |
| 34 | +import java.util.concurrent.TimeUnit |
| 35 | +import java.util.stream.Stream |
| 36 | + |
| 37 | +class KtorServerMetricsTest : AbstractHttpServerUsingTest<EmbeddedServer<*, *>>() { |
| 38 | + companion object { |
| 39 | + @JvmStatic |
| 40 | + @RegisterExtension |
| 41 | + val testing: InstrumentationExtension = HttpServerInstrumentationExtension.forLibrary() |
| 42 | + } |
| 43 | + |
| 44 | + private val errorDuringSendEndpoint = ServerEndpoint("errorDuringSend", "error-during-send", 500, "") |
| 45 | + private val errorAfterSendEndpoint = ServerEndpoint("errorAfterSend", "error-after-send", 200, "") |
| 46 | + |
| 47 | + @BeforeAll |
| 48 | + fun setupOptions() { |
| 49 | + startServer() |
| 50 | + } |
| 51 | + |
| 52 | + @AfterAll |
| 53 | + fun cleanup() { |
| 54 | + cleanupServer() |
| 55 | + } |
| 56 | + |
| 57 | + override fun getContextPath() = "" |
| 58 | + |
| 59 | + override fun setupServer(): EmbeddedServer<*, *> = embeddedServer(Netty, port = port) { |
| 60 | + install(KtorServerTelemetry) { |
| 61 | + setOpenTelemetry(testing.openTelemetry) |
| 62 | + Experimental.emitExperimentalTelemetry(this) |
| 63 | + } |
| 64 | + |
| 65 | + routing { |
| 66 | + get(errorDuringSendEndpoint.path) { |
| 67 | + call.respondBytesWriter { |
| 68 | + throw IllegalArgumentException("exception") |
| 69 | + } |
| 70 | + } |
| 71 | + get(errorAfterSendEndpoint.path) { |
| 72 | + call.respondText(errorAfterSendEndpoint.body, status = HttpStatusCode.fromValue(errorAfterSendEndpoint.status)) |
| 73 | + throw IllegalArgumentException("exception") |
| 74 | + } |
| 75 | + } |
| 76 | + }.start() |
| 77 | + |
| 78 | + override fun stopServer(server: EmbeddedServer<*, *>) { |
| 79 | + server.stop(0, 10, TimeUnit.SECONDS) |
| 80 | + } |
| 81 | + |
| 82 | + @ParameterizedTest |
| 83 | + @MethodSource("provideArguments") |
| 84 | + fun testActiveRequestsMetric(endpoint: ServerEndpoint) { |
| 85 | + val request = AggregatedHttpRequest.of(HttpMethod.valueOf("GET"), resolveAddress(endpoint)) |
| 86 | + try { |
| 87 | + client.execute(request).aggregate().join() |
| 88 | + } catch (_: Throwable) { |
| 89 | + // we expect server error |
| 90 | + } |
| 91 | + |
| 92 | + testing.waitAndAssertMetrics( |
| 93 | + INSTRUMENTATION_NAME, |
| 94 | + "http.server.active_requests" |
| 95 | + ) { metrics -> |
| 96 | + metrics!!.anySatisfy(ThrowingConsumer { metric: MetricData? -> |
| 97 | + OpenTelemetryAssertions.assertThat(metric) |
| 98 | + .hasDescription("Number of active HTTP server requests.") |
| 99 | + .hasUnit("{requests}") |
| 100 | + .hasLongSumSatisfying { sum -> |
| 101 | + sum.hasPointsSatisfying({ point -> |
| 102 | + point.hasValue(0) |
| 103 | + .hasAttributesSatisfying { |
| 104 | + OpenTelemetryAssertions.equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "GET") |
| 105 | + OpenTelemetryAssertions.equalTo(UrlAttributes.URL_PATH, endpoint.path) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | + }) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private fun provideArguments(): Stream<Arguments> = Stream.of( |
| 114 | + arguments(errorDuringSendEndpoint), |
| 115 | + arguments(errorAfterSendEndpoint), |
| 116 | + ) |
| 117 | +} |
0 commit comments