Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Replace Reporter with the one from meters4s
Browse files Browse the repository at this point in the history
Require #minor version bump
  • Loading branch information
rajit-kaluza authored and filosganga committed Jan 24, 2023
1 parent 0f14053 commit d56eaf5
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 249 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
lazy val http4sVersion = "0.23.17"
lazy val micrometerVersion = "1.7.5"
lazy val meters4sVersion = "1.1.2"
lazy val catsEffectVersion = "3.3.0"
lazy val scalaTestVersion = "3.2.10"
lazy val munitVersion = "1.0.0-M7"
Expand Down Expand Up @@ -57,6 +58,7 @@ lazy val `http4s-micrometer-metrics` = (project in file("."))
"org.typelevel" %% "cats-effect" % catsEffectVersion,
"org.http4s" %% "http4s-core" % http4sVersion,
"io.micrometer" % "micrometer-core" % micrometerVersion,
"com.ovoenergy" %% "meters4s" % meters4sVersion,
"org.http4s" %% "http4s-laws" % http4sVersion % Test,
"org.http4s" %% "http4s-server" % http4sVersion % Test,
"org.http4s" %% "http4s-dsl" % http4sVersion % Test,
Expand Down

This file was deleted.

This file was deleted.

186 changes: 0 additions & 186 deletions src/main/scala/com/ovotech/micrometer/Reporter.scala

This file was deleted.

11 changes: 0 additions & 11 deletions src/main/scala/com/ovotech/micrometer/package.scala

This file was deleted.

59 changes: 27 additions & 32 deletions src/main/scala/org/http4s/metrics/micrometer/Micrometer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import org.http4s.metrics.{MetricsOps, TerminationType}
import org.http4s.metrics.TerminationType._
import org.http4s.{Method, Status}

import io.micrometer.core.instrument.{MeterRegistry, Tags}
import io.micrometer.core.instrument.MeterRegistry

import com.ovotech.micrometer.Reporter
import com.ovoenergy.meters4s.{MetricsConfig, Reporter}

case class Config(
prefix: String,
tags: Tags = Tags.empty
tags: Map[String, String] = Map.empty
)

object Micrometer {
Expand All @@ -35,12 +35,12 @@ object Micrometer {
config: Config
): F[MetricsOps[F]] =
Reporter
.fromRegistry[F](registry, config.prefix, config.tags)
.fromRegistry[F](registry, MetricsConfig(config.prefix, config.tags))
.map(fromReporter(_))

def fromReporter[F[_]: FlatMap](
reporter: Reporter[F],
extraTags: Tags = Tags.empty
extraTags: Map[String, String] = Map.empty
): MetricsOps[F] =
new MetricsOps[F] {

Expand All @@ -54,18 +54,18 @@ object Micrometer {
private def name(classifier: Option[String], key: String): String =
s"${namespace(classifier)}.$key"

private def tags(classifier: Option[String]): Tags = {
extraTags and classifier
private def tags(classifier: Option[String]): Map[String, String] = {
extraTags ++ classifier
.collect {
case TagsReg(tagsString) if tagsString.trim.nonEmpty =>
tagsString
.split(",")
.collect { case TagReg(key, value) =>
Tags.of(key, value)
Map(key -> value)
}
.reduce(_ and _)
.reduce(_ ++ _)
}
.getOrElse(Tags.empty)
.getOrElse(Map.empty)

}

Expand All @@ -83,7 +83,7 @@ object Micrometer {
reporter
.timer(
name(classifier, "response-headers-time"),
tags(classifier) and methodTags(method)
tags(classifier) ++ methodTags(method)
)
.flatMap(_.record(elapsed.nanos))

Expand All @@ -93,15 +93,15 @@ object Micrometer {
classifier: Option[String]
): F[Unit] = {
val terminationTags = terminationType match {
case Abnormal(_) => Tags.of("termination", "abnormal")
case Error(_) => Tags.of("termination", "error")
case Canceled => Tags.of("termination", "cancelled")
case Timeout => Tags.of("termination", "timeout")
case Abnormal(_) => "termination" -> "abnormal"
case Error(_) => "termination" -> "error"
case Canceled => "termination" -> "cancelled"
case Timeout => "termination" -> "timeout"
}

recordResponseTime(
classifier,
tags(classifier) and terminationTags,
tags(classifier) ++ Map(terminationTags),
elapsed
)
}
Expand All @@ -112,20 +112,14 @@ object Micrometer {
classifier: Option[String]
): F[Unit] = {
val statusTags = status.responseClass match {
case Status.Informational =>
Tags.of("status-code", "1xx")
case Status.Successful =>
Tags.of("status-code", "2xx")
case Status.Redirection =>
Tags.of("status-code", "3xx")
case Status.ClientError =>
Tags.of("status-code", "4xx")
case Status.ServerError =>
Tags.of("status-code", "5xx")
case Status.Informational => "status-code" -> "1xx"
case Status.Successful => "status-code" -> "2xx"
case Status.Redirection => "status-code" -> "3xx"
case Status.ClientError => "status-code" -> "4xx"
case Status.ServerError => "status-code" -> "5xx"
}
val allTags = tags(classifier) and
Tags.of("termination", "normal") and
statusTags and
val allTags = tags(classifier) ++
Map("termination" -> "normal", statusTags) ++
methodTags(method)

recordResponseTime(
Expand All @@ -137,15 +131,16 @@ object Micrometer {

private def recordResponseTime(
classifier: Option[String],
tags: Tags,
tags: Map[String, String],
elapsed: Long
): F[Unit] =
reporter
.timer(name(classifier, "response-time"), tags)
.flatMap(_.record(elapsed.nanos))

private def methodTags(method: Method): Tags =
Tags.of("method", method.name.toLowerCase)
private def methodTags(method: Method): Map[String, String] = Map(
"method" -> method.name.toLowerCase
)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import java.util.concurrent.TimeoutException
import scala.concurrent.duration._

import cats.effect.IO
import cats.syntax.all._

import org.http4s._
import org.http4s.syntax.all._
import org.http4s.Uri.uri
import org.http4s.dsl.io._
import org.http4s.client._
import org.http4s.client.middleware.Metrics
Expand Down
Loading

0 comments on commit d56eaf5

Please sign in to comment.