Skip to content

Commit

Permalink
Merge pull request #2 from wlk/wlk/project-cleanup
Browse files Browse the repository at this point in the history
routine project cleanup
  • Loading branch information
wlk authored Oct 6, 2021
2 parents 3881e0e + 98844f6 commit d96bc88
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 63 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/scala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Scala
uses: olafurpg/setup-scala@v10
uses: olafurpg/setup-scala@v11
with:
java-version: [email protected]
- name: Cache sbt
uses: actions/cache@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# Artifacts
target
project/project

.bsp
38 changes: 20 additions & 18 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,34 @@ publishTo := sonatypePublishToBundle.value

version := "0.5.2-SNAPSHOT"

scalaVersion := "2.13.5"
val scala2Version = "2.13.6"
val scala3Version = "3.0.2"

scalaVersion := scala3Version
crossScalaVersions := Seq(scala3Version, scala2Version)

libraryDependencies ++= {
val simpleclientVersion = "0.8.1"
val akkaVersion = "2.6.14"
val akkaHttpVersion = "10.2.4"
val scalaTestVersion = "3.1.4"
val scalamockVersion = "4.4.0"
val simpleclientVersion = "0.12.0"
val akkaVersion = "2.6.16"
val akkaHttpVersion = "10.2.6"
val scalaTestVersion = "3.2.10"

Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion % Provided,
"com.typesafe.akka" %% "akka-stream" % akkaVersion % Provided,
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion % Provided,
"com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion % Provided,
"io.prometheus" % "simpleclient" % simpleclientVersion,
"io.prometheus" % "simpleclient_common" % simpleclientVersion,
"org.scalamock" %% "scalamock" % scalamockVersion % Test,
"com.typesafe.akka" %% "akka-testkit" % akkaVersion % Test,
"com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion % Test,
"org.scalatest" %% "scalatest" % scalaTestVersion % Test
("com.typesafe.akka" %% "akka-actor" % akkaVersion % Provided).cross(CrossVersion.for3Use2_13),
("com.typesafe.akka" %% "akka-stream" % akkaVersion % Provided).cross(CrossVersion.for3Use2_13),
("com.typesafe.akka" %% "akka-http" % akkaHttpVersion % Provided).cross(CrossVersion.for3Use2_13),
("com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion % Provided).cross(CrossVersion.for3Use2_13),
"io.prometheus" % "simpleclient" % simpleclientVersion,
"io.prometheus" % "simpleclient_common" % simpleclientVersion,
("com.typesafe.akka" %% "akka-testkit" % akkaVersion % Test).cross(CrossVersion.for3Use2_13),
("com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion % Test).cross(CrossVersion.for3Use2_13),
"org.scalatest" %% "scalatest" % scalaTestVersion % Test
)
}

lazy val `root` = (project in file("."))
.settings(
addCommandAlias("testAll", ";test"),
addCommandAlias("formatAll", ";scalafmt;test:scalafmt;scalafmtSbt"),
addCommandAlias("compileAll", ";compile;test:compile")
addCommandAlias("formatAll", ";scalafmt;Test/scalafmt;scalafmtSbt"),
addCommandAlias("compileAll", ";compile;Test/compile")
)
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.5.1
sbt.version = 1.5.5
6 changes: 3 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
logLevel := Level.Warn

addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.7")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.10")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.1.16")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.2")
addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.1.20")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3")
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PrometheusEventObserver(

val counter: Counter = buildCounter.register(registry)

private def buildCounter =
private def buildCounter: Counter.Builder =
Counter
.build()
.name(metricName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PrometheusResponseTimeRecorder(
override def recordResponseTime(endpoint: String, responseTime: FiniteDuration): Unit =
responseTimes.labels(endpoint).observe(responseTime.toUnit(timeUnit))

private def buildHistogram =
private def buildHistogram: Histogram.Builder =
Histogram
.build()
.name(metricName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.varwise.akka.http.prometheus.api

import java.io.{StringWriter, Writer}
import java.util

import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model._
import io.prometheus.client.Collector.MetricFamilySamples
import io.prometheus.client.CollectorRegistry
import io.prometheus.client.exporter.common.TextFormat

import java.io.{StringWriter, Writer}
import java.util

case class MetricFamilySamplesEntity(samples: util.Enumeration[MetricFamilySamples])

object MetricFamilySamplesEntity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.varwise.akka.http.prometheus.api

import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import io.prometheus.client.CollectorRegistry

class MetricsEndpoint(registry: CollectorRegistry) {

val routes = {
val routes: Route = {
get {
path("metrics") {
complete {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.varwise.akka.http.prometheus.directives

import akka.http.scaladsl.server.{Directive, ExceptionHandler}
import akka.http.scaladsl.server.directives.{BasicDirectives, ExecutionDirectives}
import akka.http.scaladsl.server.{Directive, ExceptionHandler}
import com.varwise.akka.http.prometheus.ResponseTimeRecorder

import scala.concurrent.duration
Expand Down Expand Up @@ -46,7 +46,7 @@ trait ResponseTimeRecordingDirectives {

object ResponseTimeRecordingDirectives {

def apply(r: ResponseTimeRecorder) =
def apply(r: ResponseTimeRecorder): ResponseTimeRecordingDirectives =
new ResponseTimeRecordingDirectives with ResponseTimeRecorderProvider {
override def recorder: ResponseTimeRecorder = r
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.varwise.akka.http.prometheus

import Utils._
import com.varwise.akka.http.prometheus.Utils._
import io.prometheus.client.CollectorRegistry
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
Expand All @@ -25,9 +25,9 @@ class PrometheusEventObserverSpec extends AnyFlatSpec with Matchers {
registry
)

def getCounterValue =
def getCounterValue: java.lang.Double =
registry.getSampleValue(
randomMetricName,
randomMetricName + "_total",
Array(randomEventLabelName, randomEventDetailsLabelName),
Array(randomEventName, randomEventDetails)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package com.varwise.akka.http.prometheus

import com.varwise.akka.http.prometheus.Utils._
import io.prometheus.client.{Collector, CollectorRegistry}
import org.scalamock.scalatest.MockFactory
import Utils._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.concurrent.duration
import scala.concurrent.duration.FiniteDuration
import scala.util.Random

class PrometheusResponseTimeRecorderSpec extends AnyFlatSpec with Matchers with MockFactory {
class PrometheusResponseTimeRecorderSpec extends AnyFlatSpec with Matchers {

private def getBucketValue(
registry: CollectorRegistry,
metricName: String,
labelNames: List[String],
labelValues: List[String],
bucket: Double
): Int = {
val name = metricName + "_bucket"

// 'le' should be the first label in the list
val allLabelNames = (Array("le") ++ labelNames).reverse
val allLabelValues = (Array(Collector.doubleToGoString(bucket)) ++ labelValues).reverse
registry.getSampleValue(name, allLabelNames, allLabelValues).intValue()
}

"PrometheusLatencyRecorder" should "register a histogram and record request latencies" in {
val registry = new CollectorRegistry()
Expand Down Expand Up @@ -50,20 +64,4 @@ class PrometheusResponseTimeRecorderSpec extends AnyFlatSpec with Matchers with
second shouldBe 1
positiveInf shouldBe 1
}

private def getBucketValue(
registry: CollectorRegistry,
metricName: String,
labelNames: List[String],
labelValues: List[String],
bucket: Double
) = {
val name = metricName + "_bucket"

// 'le' should be the first label in the list
val allLabelNames = (Array("le") ++ labelNames).reverse
val allLabelValues = (Array(Collector.doubleToGoString(bucket)) ++ labelValues).reverse
registry.getSampleValue(name, allLabelNames, allLabelValues).intValue()
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.varwise.akka.http.prometheus.api

import java.io.StringWriter

import akka.http.scaladsl.model.HttpCharsets
import akka.http.scaladsl.testkit.ScalatestRouteTest
import com.varwise.akka.http.prometheus.Utils._
Expand All @@ -10,10 +8,14 @@ import io.prometheus.client.{CollectorRegistry, Histogram}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import java.io.StringWriter
import scala.util.Random

class MetricsEndpointSpec extends AnyFlatSpec with Matchers with ScalatestRouteTest {

private def createEndpoint(collectorRegistry: CollectorRegistry) =
new MetricsEndpoint(collectorRegistry)

"Metrics endpoint" should "return the correct media type and charset" in {
val api = createEndpoint(CollectorRegistry.defaultRegistry)
Get("/metrics") ~> api.routes ~> check {
Expand All @@ -27,6 +29,8 @@ class MetricsEndpointSpec extends AnyFlatSpec with Matchers with ScalatestRouteT
it should "return serialized metrics in the prometheus text format" in {
val registry = new CollectorRegistry()
val api = createEndpoint(registry)
val RandomTestName = generateRandomStringOfLength(16)
val RandomTestHelp = generateRandomStringOfLength(16)
val hist = Histogram.build().name(RandomTestName).help(RandomTestHelp).linearBuckets(0, 1, 10).register(registry)

hist.observe(Math.abs(Random.nextDouble()))
Expand All @@ -39,11 +43,4 @@ class MetricsEndpointSpec extends AnyFlatSpec with Matchers with ScalatestRouteT
resp shouldBe writer.toString
}
}

private val RandomTestName = generateRandomStringOfLength(16)
private val RandomTestHelp = generateRandomStringOfLength(16)

private def createEndpoint(collectorRegistry: CollectorRegistry) =
new MetricsEndpoint(collectorRegistry)

}

0 comments on commit d96bc88

Please sign in to comment.