This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Keir Lawson
committed
Oct 31, 2023
1 parent
6502ff5
commit 1fb7085
Showing
13 changed files
with
158 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.ovoenergy.meters4s | ||
package meters4s | ||
|
||
import cats.effect.Sync | ||
import cats.implicits._ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.ovoenergy.meters4s | ||
package meters4s | ||
|
||
import scala.concurrent.duration._ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
datadog/src/main/scala/com/ovoenergy/meters4s/datadog/DataDog.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package meters4s.http4s | ||
|
||
import scala.concurrent.duration._ | ||
|
||
import cats.effect._ | ||
import cats.syntax.all._ | ||
|
||
import org.http4s.metrics.TerminationType._ | ||
import org.http4s.metrics.{MetricsOps, TerminationType} | ||
import org.http4s.{Method, Status} | ||
import meters4s.Reporter | ||
|
||
object Meters4s { | ||
|
||
private val TagsReg = """.*?\[([^\]]*)\]""".r | ||
private val TagReg = """([^:]*)\s*:\s*(.*)""".r | ||
|
||
def apply[F[_]: Async]( | ||
reporter: Reporter[F], | ||
percentiles: Set[Double] = Set.empty | ||
): MetricsOps[F] = | ||
new MetricsOps[F] { | ||
|
||
private def namespace(classifier: Option[String]): String = { | ||
classifier | ||
.map(_.takeWhile(_ != '[').trim) | ||
.filter(_.nonEmpty) | ||
.getOrElse("default") | ||
} | ||
|
||
private def name(classifier: Option[String], key: String): String = | ||
s"${namespace(classifier)}.$key" | ||
|
||
private def tags(classifier: Option[String]): Map[String, String] = { | ||
classifier | ||
.collect { | ||
case TagsReg(tagsString) if tagsString.trim.nonEmpty => | ||
tagsString | ||
.split(",") | ||
.collect { case TagReg(key, value) => | ||
Map(key -> value) | ||
} | ||
.reduce(_ ++ _) | ||
} | ||
.getOrElse(Map.empty) | ||
|
||
} | ||
|
||
def increaseActiveRequests(classifier: Option[String]): F[Unit] = | ||
reporter.gauge(name(classifier, "active-requests"), tags(classifier)).flatMap(_.increment) | ||
|
||
def decreaseActiveRequests(classifier: Option[String]): F[Unit] = | ||
reporter.gauge(name(classifier, "active-requests"), tags(classifier)).flatMap(_.decrement) | ||
|
||
def recordHeadersTime( | ||
method: Method, | ||
elapsed: Long, | ||
classifier: Option[String] | ||
): F[Unit] = | ||
reporter | ||
.timer( | ||
name(classifier, "response-headers-time"), | ||
tags(classifier) ++ methodTags(method), | ||
percentiles | ||
) | ||
.flatMap(_.record(elapsed.nanos)) | ||
|
||
def recordAbnormalTermination( | ||
elapsed: Long, | ||
terminationType: TerminationType, | ||
classifier: Option[String] | ||
): F[Unit] = { | ||
val terminationTags = terminationType match { | ||
case Abnormal(_) => "termination" -> "abnormal" | ||
case Error(_) => "termination" -> "error" | ||
case Canceled => "termination" -> "cancelled" | ||
case Timeout => "termination" -> "timeout" | ||
} | ||
|
||
recordResponseTime( | ||
classifier, | ||
tags(classifier) ++ Map(terminationTags), | ||
elapsed | ||
) | ||
} | ||
def recordTotalTime( | ||
method: Method, | ||
status: Status, | ||
elapsed: Long, | ||
classifier: Option[String] | ||
): F[Unit] = { | ||
val statusTags = status.responseClass match { | ||
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) ++ | ||
Map("termination" -> "normal", statusTags) ++ | ||
methodTags(method) | ||
|
||
recordResponseTime( | ||
classifier, | ||
allTags, | ||
elapsed | ||
) | ||
} | ||
|
||
private def recordResponseTime( | ||
classifier: Option[String], | ||
tags: Map[String, String], | ||
elapsed: Long | ||
): F[Unit] = | ||
reporter | ||
.timer(name(classifier, "response-time"), tags, percentiles) | ||
.flatMap(_.record(elapsed.nanos)) | ||
|
||
private def methodTags(method: Method): Map[String, String] = Map( | ||
"method" -> method.name.toLowerCase | ||
) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.8.3 | ||
sbt.version=1.9.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.2") | ||
addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.4.1") | ||
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.6") | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.6.3") | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "1.4.1") | ||
addSbtPlugin("com.github.sbt" % "sbt-ghpages" % "0.8.0") | ||
addSbtPlugin("com.github.sbt" % "sbt-site" % "1.5.0") | ||
addSbtPlugin("com.eed3si9n" % "sbt-unidoc" % "0.4.3") | ||
addSbtPlugin("com.dwijnand" % "sbt-dynver" % "4.1.1") | ||
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.7.0") | ||
// addSbtPlugin("com.dwijnand" % "sbt-dynver" % "4.1.1") | ||
// addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.7.0") | ||
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.13") | ||
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.1.1") | ||
addSbtPlugin("com.github.sbt" % "sbt-release" % "1.1.0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
statsd/src/main/scala/com/ovoenergy/meters4s/statsd/StatsD.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters