Skip to content

Commit

Permalink
Handle http4s ember server metrics in error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrdom committed Apr 12, 2023
1 parent 642fab4 commit 1bbbb86
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package io.scalac.mesmer.otelextension.instrumentations.http4s.ember.server.advi

import cats.data.Kleisli
import cats.effect.IO
import cats.implicits._
import io.opentelemetry.api.GlobalOpenTelemetry
import io.opentelemetry.api.common.Attributes
import org.http4s.HttpApp
import org.http4s.Request
import org.http4s.Response

import scala.util.Try

object ServerHelpersRunAppAdviceHelper {

private val meter = GlobalOpenTelemetry.getMeter("mesmer")
Expand Down Expand Up @@ -40,16 +43,22 @@ object ServerHelpersRunAppAdviceHelper {
httpApp
.asInstanceOf[HttpApp[IO]]
.run(request)
.map { response =>
val allAttributes = requestAttributes.toBuilder.putAll(attributesForResponse(response).build()).build()

requestsTotal.add(1, allAttributes)
.attemptTap { response =>
IO.fromTry(Try {
val allAttributes = requestAttributes.toBuilder
.putAll(
response
.map(attributesForResponse(_).build())
.getOrElse(Attributes.empty())
)
.build()

requestDuration.record((System.nanoTime() - startTime) / 1e9d, allAttributes)
requestsTotal.add(1, allAttributes)

concurrentRequests.add(-1, requestAttributes)
requestDuration.record((System.nanoTime() - startTime) / 1e9d, allAttributes)

response
concurrentRequests.add(-1, requestAttributes)
})
}
}
}

0 comments on commit 1bbbb86

Please sign in to comment.