Skip to content

Commit

Permalink
metric log level label, metric docs fixes (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon authored Dec 28, 2022
1 parent 92ed4c0 commit 5b57954
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
10 changes: 5 additions & 5 deletions core/jvm/src/test/scala/zio/logging/MetricsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ object MetricsSpec extends ZIOSpecDefault {
_ <- TestService.testInfo
_ <- TestService.testWarning
_ <- TestService.testError
debugCounter <- loggedTotalMetric.tagged(DefaultLogLevelLabel, LogLevel.Debug.label.toLowerCase).value
infoCounter <- loggedTotalMetric.tagged(DefaultLogLevelLabel, LogLevel.Info.label.toLowerCase).value
warnCounter <- loggedTotalMetric.tagged(DefaultLogLevelLabel, LogLevel.Warning.label.toLowerCase).value
errorCounter <- loggedTotalMetric.tagged(DefaultLogLevelLabel, LogLevel.Error.label.toLowerCase).value
fatalCounter <- loggedTotalMetric.tagged(DefaultLogLevelLabel, LogLevel.Fatal.label.toLowerCase).value
debugCounter <- loggedTotalMetric.tagged(logLevelMetricLabel, LogLevel.Debug.label.toLowerCase).value
infoCounter <- loggedTotalMetric.tagged(logLevelMetricLabel, LogLevel.Info.label.toLowerCase).value
warnCounter <- loggedTotalMetric.tagged(logLevelMetricLabel, LogLevel.Warning.label.toLowerCase).value
errorCounter <- loggedTotalMetric.tagged(logLevelMetricLabel, LogLevel.Error.label.toLowerCase).value
fatalCounter <- loggedTotalMetric.tagged(logLevelMetricLabel, LogLevel.Fatal.label.toLowerCase).value
} yield assertTrue(debugCounter.count == 2d) && assertTrue(infoCounter.count == 2d) && assertTrue(
warnCounter.count == 2d
) && assertTrue(errorCounter.count == 1d) && assertTrue(fatalCounter.count == 0d)).provideLayer(logMetrics)
Expand Down
7 changes: 4 additions & 3 deletions core/shared/src/main/scala/zio/logging/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ package object logging {
FiberRef.unsafe.make(LogContext.empty, ZIO.identityFn[LogContext], (old, newV) => old ++ newV)
}

private[logging] val DefaultLogLevelLabel = "level"
private[logging] val logLevelMetricLabel = "level"

private[logging] val loggedTotalMetric =
Metric.counter(name = "zio_log_total")
Expand Down Expand Up @@ -324,8 +324,9 @@ package object logging {
}
}

val logMetrics: ZLayer[Any, Nothing, Unit] =
Runtime.addLogger(metricLogger(loggedTotalMetric, DefaultLogLevelLabel))
val logMetrics: ZLayer[Any, Nothing, Unit] =
Runtime.addLogger(metricLogger(loggedTotalMetric, logLevelMetricLabel))

def logMetricsWith(name: String, logLevelLabel: String): ZLayer[Any, Nothing, Unit] =
Runtime.addLogger(metricLogger(Metric.counter(name), logLevelLabel))
}
56 changes: 28 additions & 28 deletions docs/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ The Metrics layer
val layer = zio.logging.logMetrics
```

will add a default metric named `zio_logger_total` with the label `level` which will be
incremented for each log message with the value of `level` being the corresponding log level in lower case.
will add a default metric named `zio_log_total` with the label `level` which will be
incremented for each log message with the value of `level` being the corresponding log level label in lower case.

Metrics:

Expand All @@ -30,7 +30,7 @@ Metrics:
Custom names for the metric and label can be set via:

```scala
val layer = zio.logging.logMetrics("log_counter", "log_level")
val layer = zio.logging.logMetricsWith("log_counter", "log_level")
```

## Examples
Expand All @@ -45,11 +45,10 @@ code [here](https://github.com/zio/zio-logging/tree/master/examples/src/main/sca
```scala
package zio.logging.example

import zio.logging.{LogFormat, console, logMetrics}
import zio.{ExitCode, Runtime, Scope, ZIO, ZIOAppArgs, ZIOAppDefault, ZLayer}
import zio.metrics.connectors.prometheus.{prometheusLayer, publisherLayer, PrometheusPublisher}
import zio.logging.{ LogFormat, console, logMetrics }
import zio.metrics.connectors.MetricsConfig
import zio._
import zio.metrics.connectors.prometheus.{ PrometheusPublisher, prometheusLayer, publisherLayer }
import zio.{ ExitCode, Runtime, Scope, ZIO, ZIOAppArgs, ZIOAppDefault, ZLayer, _ }

object MetricsApp extends ZIOAppDefault {

Expand All @@ -58,35 +57,36 @@ object MetricsApp extends ZIOAppDefault {

override def run: ZIO[Scope, Any, ExitCode] =
(for {
_ <- ZIO.logInfo("Start")
_ <- ZIO.logWarning("Some warning")
_ <- ZIO.logError("Some error")
_ <- ZIO.logError("Another error")
_ <- ZIO.sleep(1.second)
_ <- ZIO.logInfo("Start")
_ <- ZIO.logWarning("Some warning")
_ <- ZIO.logError("Some error")
_ <- ZIO.logError("Another error")
_ <- ZIO.sleep(1.second)
metricValues <- ZIO.serviceWithZIO[PrometheusPublisher](_.get)
_ <- Console.printLine(metricValues)
_ <- ZIO.logInfo("Done")
_ <- Console.printLine(metricValues)
_ <- ZIO.logInfo("Done")
} yield ExitCode.success)
.provideLayer((ZLayer.succeed(MetricsConfig(200.millis)) ++ publisherLayer) >+> prometheusLayer)

}

```

Expected Console Output:

```
timestamp=2022-12-20T20:42:59.781481+01:00 level=INFO thread=zio-fiber-6 message="Start"
timestamp=2022-12-20T20:42:59.810161+01:00 level=WARN thread=zio-fiber-6 message="Some warning"
timestamp=2022-12-20T20:42:59.81197+01:00 level=ERROR thread=zio-fiber-6 message="Some error"
timestamp=2022-12-20T20:42:59.813489+01:00 level=ERROR thread=zio-fiber-6 message="Another error"
# TYPE zio_logger_warn_total counter
# HELP zio_logger_warn_total Some help
zio_logger_warn_total 1.0 1671565380643
# TYPE zio_logger_info_total counter
# HELP zio_logger_info_total Some help
zio_logger_info_total 1.0 1671565380643
# TYPE zio_logger_error_total counter
# HELP zio_logger_error_total Some help
zio_logger_error_total 2.0 1671565380643
timestamp=2022-12-20T20:43:00.835177+01:00 level=INFO thread=zio-fiber-6 message="Done"
timestamp=2022-12-28T09:43:29.226711+01:00 level=INFO thread=zio-fiber-6 message="Start"
timestamp=2022-12-28T09:43:29.255915+01:00 level=WARN thread=zio-fiber-6 message="Some warning"
timestamp=2022-12-28T09:43:29.257454+01:00 level=ERROR thread=zio-fiber-6 message="Some error"
timestamp=2022-12-28T09:43:29.258267+01:00 level=ERROR thread=zio-fiber-6 message="Another error"
# TYPE zio_log_total counter
# HELP zio_log_total Some help
zio_log_total{level="error"} 2.0 1672217010080
# TYPE zio_log_total counter
# HELP zio_log_total Some help
zio_log_total{level="warn"} 1.0 1672217010080
# TYPE zio_log_total counter
# HELP zio_log_total Some help
zio_log_total{level="info"} 1.0 1672217010080
timestamp=2022-12-28T09:43:30.281274+01:00 level=INFO thread=zio-fiber-6 message="Done"
```

0 comments on commit 5b57954

Please sign in to comment.