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.
Change package name, port http4s (#22)
* Change package name, port http4s * Port http4s tests * Move dirs to match packages * format * Update readme --------- Co-authored-by: Keir Lawson <[email protected]>
- Loading branch information
1 parent
6502ff5
commit 52c8782
Showing
16 changed files
with
1,219 additions
and
65 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.
2 changes: 1 addition & 1 deletion
2
...ala/com/ovoenergy/meters4s/Reporter.scala → core/src/main/scala/meters4s/Reporter.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
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._ | ||
|
2 changes: 1 addition & 1 deletion
2
...scala/com/ovoenergy/meters4s/syntax.scala → core/src/main/scala/meters4s/syntax.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.ovoenergy.meters4s | ||
package meters4s | ||
|
||
import scala.concurrent.duration._ | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...com/ovoenergy/meters4s/ReporterTest.scala → ...rc/test/scala/meters4s/ReporterTest.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
4 changes: 2 additions & 2 deletions
4
.../ovoenergy/meters4s/datadog/DataDog.scala → ...main/scala/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,129 @@ | ||
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 | ||
) | ||
|
||
} | ||
} |
Oops, something went wrong.