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

Commit

Permalink
Change package name, port http4s
Browse files Browse the repository at this point in the history
  • Loading branch information
Keir Lawson committed Oct 31, 2023
1 parent 6502ff5 commit 1fb7085
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 62 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ For comprehensive API documentation check [the scaladoc](https://ovotech.github.
A simple usage example for incrementing a counter, backed by a Micrometer `SimpleMeterRegistry`:

```scala
import com.ovoenergy.meters4s.{Reporter, MetricsConfig}
import meters4s.{Reporter, MetricsConfig}
import cats.effect.IO

val config = MetricsConfig()
Expand All @@ -60,8 +60,8 @@ for {
### With Datadog

```scala
import com.ovoenergy.meters4s.{MetricsConfig, Reporter}
import com.ovoenergy.meters4s.datadog.{DataDog, DataDogConfig}
import meters4s.{MetricsConfig, Reporter}
import meters4s.datadog.{DataDog, DataDogConfig}
import cats.effect.IO

val datadog =
Expand All @@ -81,8 +81,8 @@ import cats.effect._
import cats.effect.std.Console
import cats.effect.syntax.all._
import cats.syntax.all._
import com.ovoenergy.meter4s.prometheus._
import com.ovoenergy.meters4s.{MetricsConfig, Reporter}
import meter4s.prometheus._
import meters4s.{MetricsConfig, Reporter}
import io.micrometer.core.instrument.binder.system.ProcessorMetrics

import scala.concurrent.duration._
Expand Down
13 changes: 12 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ReleaseTransformations._

lazy val additionalSupportedScalaVersions = List("2.13.10", "2.12.17")
lazy val additionalSupportedScalaVersions = List("2.13.12", "2.12.18")

lazy val root = (project in file("."))
.settings(
Expand Down Expand Up @@ -120,6 +120,17 @@ lazy val prometheus = project
)
.dependsOn(core)

lazy val http4s = project
.settings(
name := "meters4s-http4s",
commonSettings,
publishSettings,
libraryDependencies ++= commonDependencies ++ Seq(
"org.http4s" %% "http4s-core" % "0.23.17",
)
)
.dependsOn(core)

lazy val docs = project
.settings(
commonSettings,
Expand Down
39 changes: 0 additions & 39 deletions build/tag.sh

This file was deleted.

2 changes: 1 addition & 1 deletion core/src/main/scala/com/ovoenergy/meters4s/Reporter.scala
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._
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/com/ovoenergy/meters4s/syntax.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ovoenergy.meters4s
package meters4s

import scala.concurrent.duration._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ovoenergy.meters4s
package meters4s

import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import cats.effect.IO
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.ovoenergy.meters4s.datadog
package meters4s.datadog

import cats.effect.{Async, Resource, Sync}
import cats.implicits._
import com.ovoenergy.meters4s.{MetricsConfig, Reporter}
import meters4s.{MetricsConfig, Reporter}
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.datadog.{
DatadogMeterRegistry,
Expand Down
6 changes: 3 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ For comprehensive API documentation check [the scaladoc](https://ovotech.github.
A simple usage example for incrementing a counter, backed by a Micrometer `SimpleMeterRegistry`:

```scala mdoc:silent
import com.ovoenergy.meters4s.{Reporter, MetricsConfig}
import meters4s.{Reporter, MetricsConfig}
import cats.effect.IO
import scala.concurrent.ExecutionContext.Implicits.global

Expand All @@ -61,8 +61,8 @@ for {
### With Datadog

```scala mdoc:silent
import com.ovoenergy.meters4s.{MetricsConfig, Reporter}
import com.ovoenergy.meters4s.datadog.{DataDog, DataDogConfig}
import meters4s.{MetricsConfig, Reporter}
import meters4s.datadog.{DataDog, DataDogConfig}
import cats.effect.IO

val datadog = DataDog.createReporter[IO](DataDogConfig(apiKey = "1234"), MetricsConfig())
Expand Down
124 changes: 124 additions & 0 deletions http4s/src/main/scala/Meters4s.scala
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
)

}
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.8.3
sbt.version=1.9.7
8 changes: 4 additions & 4 deletions project/plugins.sbt
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")
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

package com.ovoenergy.meter4s.prometheus
package meter4s.prometheus
import cats.effect.{Async, Resource, Sync}
import com.ovoenergy.meters4s.{MetricsConfig, Reporter}
import meters4s.{MetricsConfig, Reporter}
import io.micrometer.prometheus
import io.micrometer.prometheus.{
PrometheusMeterRegistry,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ovoenergy.meters4s.statsd
package meters4s.statsd

import cats.effect.{Resource, Sync, Async}
import com.ovoenergy.meters4s.{MetricsConfig, Reporter}
import meters4s.{MetricsConfig, Reporter}
import io.micrometer.statsd.{StatsdConfig => MmStatsdConfig}
import io.micrometer.core.instrument.MeterRegistry
import scala.concurrent.duration.FiniteDuration
Expand Down

0 comments on commit 1fb7085

Please sign in to comment.