From 21d174f52acd463c25108cda1b9af8018829b06c Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Sat, 16 Nov 2024 21:32:34 +0530 Subject: [PATCH] Update metrics/opentelemetry-metrics/src/main/scala/sttp/tapir/server/metrics/opentelemetry/OpenTelemetryMetrics.scala --- .../opentelemetry/OpenTelemetryMetrics.scala | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/metrics/opentelemetry-metrics/src/main/scala/sttp/tapir/server/metrics/opentelemetry/OpenTelemetryMetrics.scala b/metrics/opentelemetry-metrics/src/main/scala/sttp/tapir/server/metrics/opentelemetry/OpenTelemetryMetrics.scala index cbb0d34018..cf496239e8 100644 --- a/metrics/opentelemetry-metrics/src/main/scala/sttp/tapir/server/metrics/opentelemetry/OpenTelemetryMetrics.scala +++ b/metrics/opentelemetry-metrics/src/main/scala/sttp/tapir/server/metrics/opentelemetry/OpenTelemetryMetrics.scala @@ -50,16 +50,20 @@ object OpenTelemetryMetrics { "path" -> { case (ep, _) => ep.showPathTemplate(showQueryParam = None) } ), forResponse = List( - "http.response.status_code" -> { - case Right(r) => r.code.code.toString - // Default to 500 for exceptions - case Left(_) => "500" + "http.response.status_code" -> { response: Either[Throwable, ServerResponse[_]] => + response match { + case Right(r) => r.code.code.toString + // Default to 500 for exceptions + case Left(_) => "500" + } }, - "error.type" -> { - case Left(ex) => Some(ex.getClass.getName) // Exception class name for pre-response errors - case Right(_) => None // No error.type for successful responses + "error.type" -> { response: Either[Throwable, ServerResponse[_]] => + response match { + case Left(ex) => Some(ex.getClass.getName) // Exception class name for pre-response errors + case Right(_) => None // No error.type for successful responses + } } - ).collect { case (k, Some(v)) => k -> v } + ).collect { case (k, v) if v(response) != None => k -> v(response).get } ) def apply[F[_]](meter: Meter): OpenTelemetryMetrics[F] = apply(meter, Nil)