Skip to content

Instantly share code, notes, and snippets.

@itang
Created May 13, 2021 09:01
Show Gist options
  • Save itang/fd02c6f75a898621d1e28bcababe1bd2 to your computer and use it in GitHub Desktop.
Save itang/fd02c6f75a898621d1e28bcababe1bd2 to your computer and use it in GitHub Desktop.
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