Skip to content

Commit

Permalink
gauges
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil committed Oct 16, 2023
1 parent 4c6eb76 commit 975eaf4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
28 changes: 28 additions & 0 deletions kyo-core/shared/src/main/scala/kyo/stats/MetricReceiver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ trait MetricReceiver {
unit: String,
a: Attributes
): Histogram

def gauge(
scope: List[String],
name: String,
description: String,
unit: String,
a: Attributes
)(f: => Double): Gauge
}

object MetricReceiver {
Expand Down Expand Up @@ -54,10 +62,20 @@ object MetricReceiver {
a: Attributes
) =
Histogram.noop

def gauge(
scope: List[String],
name: String,
description: String,
unit: String,
a: Attributes
)(f: => Double) =
Gauge.noop
}

def all(receivers: List[MetricReceiver]): MetricReceiver =
new MetricReceiver {

def counter(
scope: List[String],
name: String,
Expand All @@ -66,6 +84,7 @@ object MetricReceiver {
a: Attributes
) =
Counter.all(receivers.map(_.counter(scope, name, description, unit, a)))

def histogram(
scope: List[String],
name: String,
Expand All @@ -74,5 +93,14 @@ object MetricReceiver {
a: Attributes
) =
Histogram.all(receivers.map(_.histogram(scope, name, description, unit, a)))

def gauge(
scope: List[String],
name: String,
description: String,
unit: String,
a: Attributes
)(f: => Double) =
Gauge.all(receivers.map(_.gauge(scope, name, description, unit, a)(f)))
}
}
3 changes: 3 additions & 0 deletions kyo-core/shared/src/main/scala/kyo/stats/Stats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kyo.stats.metrics._
import kyo.stats.traces._

trait Stats {

def scope(name: String): Stats

def initCounter(
Expand Down Expand Up @@ -84,5 +85,7 @@ object Stats {
attributes: Attributes
)(v: => T > S): T > (IOs with S) =
Traces.span(path.reverse, name, attributes)(v)

override def toString = s"Stats(scope = ${path})"
}
}
25 changes: 25 additions & 0 deletions kyo-core/shared/src/main/scala/kyo/stats/metrics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ object metrics {
a: Attributes = Attributes.empty
): Histogram =
MetricReceiver.get.histogram(scope, name, description, unit, a)

def initGauge(
scope: List[String],
name: String,
description: String = "",
unit: String = "",
a: Attributes = Attributes.empty
)(f: => Double): Gauge =
MetricReceiver.get.gauge(scope, name, description, unit, a)(f)
}

trait Counter {
Expand Down Expand Up @@ -77,4 +86,20 @@ object metrics {
def attributes(b: Attributes) = all(l.map(_.attributes(b)))
}
}

trait Gauge {
def close: Unit > IOs
}

object Gauge {
val noop: Gauge =
new Gauge {
def close = ()
}

def all(l: List[Gauge]): Gauge =
new Gauge {
def close = Choices.traverseUnit(l)(_.close)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,24 @@ class OTelMetricReceiver extends MetricReceiver {
histogram(scope, name, description, unit, a.add(b))
}

def gauge(
scope: List[String],
name: String,
description: String,
unit: String,
a: Attributes
)(f: => Double) =
new Gauge {

val impl =
otel.getMeter(scope.mkString("_"))
.gaugeBuilder(name)
.setDescription(description)
.setUnit(unit)
.buildWithCallback(m => m.record(f))

def close =
IOs(impl.close())
}

}

0 comments on commit 975eaf4

Please sign in to comment.