MetricLabel
A MetricLabel is a key-value pair that allows analyzing metrics at an additional level of granularity. For example, if a metric tracks the response time of a service, labels could be used to create separate versions that track response times for different clients.
ZIO Metrics has a label based dimensional data model where we have a metric name and just a list of key-value pairs attached to that metric name. So labels are the first-class citizen in ZIO Metrics. In monitoring dashboards, we can find or filter metrics using these labels.
For example, we can append following labels (dimensions) to our metric aspects:
- Endpoint (/api/users, /api/documents)
- HTTP Method (POST, GET)
- Deployment Environment (Staging, Production)
- HTTP Response Code
- Error code (404, 503)
- Datacenter Zone (us-east, eu-west)
import zio._
import zio.metrics._
val counter = Metric.counter("http_requests")
.tagged(
MetricLabel("env", "production"),
MetricLabel("method", "GET"),
MetricLabel("endpoint", "/api/users"),
MetricLabel("zone", "ap-northeast"),
)
By labeling metrics, we can query in a more granular way in monitoring dashboards, such as
- How many requests have been sent to each endpoint in total?
- In which zone are we about to violate SLAs?
- Which endpoint has the most latency?
Tagging a Metricโ
Metric#tagged returns a metric with extra labels. It takes a key and a value, one or more MetricLabel values as in the example above, or a Set[MetricLabel]:
import zio._
import zio.metrics._
val requests = Metric.counter("http_requests")
val byMethod = requests.tagged("method", "GET")
val byEndpoint = requests.tagged(MetricLabel("method", "GET"), MetricLabel("endpoint", "/api/users"))
val byZone = requests.tagged(Set(MetricLabel("zone", "eu-west")))
Each distinct set of labels is a separate metric with its own state. Updating byMethod does not change the untagged requests counter, and reading requests does not include the updates made through byMethod. The labels form a set, so tagging a metric with a key it already has, but with another value, keeps both labels.
Tags From the Inputโ
Metric#taggedWith computes the labels of each update from the update value. Its own Metric#value returns (), because the state depends on the labels of each update; to read the state, use Metric#tagged with the same labels:
import zio._
import zio.metrics._
val responsesByStatus: Metric[MetricKeyType.Counter, Int, Unit] =
Metric
.counterInt("http_responses")
.fromConst(1)
.taggedWith[Int](status => Set(MetricLabel("status", status.toString)))
val program: UIO[Double] =
for {
_ <- ZIO.succeed(200) @@ responsesByStatus
_ <- ZIO.succeed(404) @@ responsesByStatus
_ <- ZIO.succeed(200) @@ responsesByStatus
state <- Metric.counterInt("http_responses").tagged("status", "200").value
} yield state.count
Here program returns 2.0, the number of responses with the status 200.
Tags From the Fiberโ
Instead of tagging each metric, we can tag a region of code. ZIO.tagged runs an effect with extra labels, and every metric updated in that effect carries them. It takes the same three forms as Metric#tagged. ZIOAspect.tagged("key" -> "value") does the same as an aspect, ZIO.taggedScoped adds labels until the scope closes, and ZIO.tags returns the labels of the current fiber. Metric#value reads the state for the metric's own labels plus the labels of the current fiber:
import zio._
import zio.metrics._
val requests = Metric.counter("requests")
val program: UIO[(Double, Double)] =
for {
_ <- ZIO.tagged("env", "prod")(requests.increment)
untagged <- requests.value
prod <- requests.tagged("env", "prod").value
} yield (untagged.count, prod.count)
Here program returns (0.0, 1.0): the update inside ZIO.tagged went to the counter labeled env=prod, not to the untagged one.