Created
May 13, 2021 09:01
-
-
Save itang/fd02c6f75a898621d1e28bcababe1bd2 to your computer and use it in GitHub Desktop.
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
sealed trait Metric(val name: String, val description: String) | |
case class Counter(override val name: String, | |
override val description: String, | |
value: Double | |
) extends Metric(name, description) | |
trait ToText[T <: Metric]: | |
def toText(t: T): String | |
object ToTexts: | |
given ToText[Counter] with | |
def toText(t: Counter) = | |
s"""|# HELP ${t.name} ${t.description}. | |
|# TYPE ${t.name} counter | |
|${t.name} ${t.value} | |
|""".stripMargin | |
import ToTexts.given_ToText_Counter | |
def toText[A <: Metric](a: A)(using s: ToText[A]): String = summon[ToText[A]].toText(a) | |
@main | |
def main(): Unit = time { | |
val c = Counter("cpu_total_seconds", "CPU total seconds", 60 * 20) | |
println(toText(c)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment