Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
Zio2/cleanup (#414)
Browse files Browse the repository at this point in the history
* Apply formatting

* Start to update site to ZIO 2 RC6

* Bump dotty version for CI

* Remove compile flag on docs for RC release

* Bump ZIO HTTP version

* Fix compile issues after version bumps

* Just maintenance

* Apply formatting

* Update CI workflow
  • Loading branch information
atooni authored Jun 3, 2022
1 parent 23aa33a commit ae65631
Show file tree
Hide file tree
Showing 30 changed files with 177 additions and 357 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: false
matrix:
java: ['adopt@1.8', 'adopt@1.11']
scala: ['2.12.15', '2.13.8', '3.1.0']
scala: ['2.12.15', '2.13.8', '3.1.2']
steps:
- uses: actions/checkout@v2.3.4
- uses: olafurpg/setup-scala@v10
Expand All @@ -25,15 +25,10 @@ jobs:
uses: coursier/cache-action@v5
- name: Lint code
run: sbt check
- name: compile without examples and JS client
run: sbt ++${{ matrix.scala }}! coreJVM/compile coreJS/compile
- name: compile all
run: sbt ++${{ matrix.scala }}! compile
if: matrix.scala == '2.13.8'
- name: Run tests
# TODO: Run the test on all Scala versions before we release something
run: sbt ++${{ matrix.scala }} test
if: matrix.scala == '2.13.8'

publish:
runs-on: ubuntu-20.04
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ lazy val root =
(project in file("."))
.settings(
run / fork := true,
Test/run/javaOptions += "-Djava.net.preferIPv4Stack=true",
Test / run / javaOptions += "-Djava.net.preferIPv4Stack=true",
cancelable := true,
stdSettings("zio.metrics.connectors"),
libraryDependencies ++= Seq(
Expand Down
37 changes: 17 additions & 20 deletions docs/metrics/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ title: Instrumentation Examples

```scala mdoc:invisible
import zio._
import zio.random._
import zio.duration._
import zio.zmx.metrics._
import zio.zmx.state.DoubleHistogramBuckets
import zio.metrics._
```

The trait below is used in the ZMX sample application just to show how the individual aspects
Expand All @@ -20,49 +17,49 @@ metrics can be visualized in the supported back ends.
```scala mdoc
trait InstrumentedSample {

// Create a gauge that can be set to absolute values, it can be applied to effects yielding a Double
val aspGaugeAbs = MetricAspect.setGauge("setGauge")
// Create a gauge that can be set relative to it's current value, it can be applied to effects yielding a Double
val aspGaugeRel = MetricAspect.adjustGauge("adjustGauge")
// Create a gauge, it can be applied to effects yielding a Double
val aspGauge1 = Metric.gauge("gauge1")

val aspGauge2 = Metric.gauge("gauge2")

// Create a histogram with 12 buckets: 0..100 in steps of 10, Infinite
// It also can be applied to effects yielding a Double
val aspHistogram =
MetricAspect.observeHistogram("zmxHistogram", DoubleHistogramBuckets.linear(0.0d, 10.0d, 11).boundaries)
Metric.histogram("zmxHistogram", MetricKeyType.Histogram.Boundaries.linear(0.0d, 10.0d, 11))

// Create a summary that can hold 100 samples, the max age of the samples is 1 day.
// The summary should report th 10%, 50% and 90% Quantile
// It can be applied to effects yielding an Int
val aspSummary =
MetricAspect.observeSummaryWith[Int]("mySummary", 1.day, 100, 0.03d, Chunk(0.1, 0.5, 0.9))(_.toDouble)
Metric.summary("mySummary", 1.day, 100, 0.03d, Chunk(0.1, 0.5, 0.9)).contramap[Int](_.toDouble)

// Create a Set to observe the occurrences of unique Strings
// It can be applied to effects yielding a String
val aspSet = MetricAspect.occurrences("mySet", "token")
val aspSet = Metric.frequency("mySet")

// Create a counter applicable to any effect
val aspCountAll = MetricAspect.count("countAll")
val aspCountAll = Metric.counter("countAll").contramap[Any](_ => 1L)

private lazy val gaugeSomething = for {
_ <- nextDoubleBetween(0.0d, 100.0d) @@ aspGaugeAbs @@ aspCountAll
_ <- nextDoubleBetween(-50d, 50d) @@ aspGaugeRel @@ aspCountAll
_ <- Random.nextDoubleBetween(0.0d, 100.0d) @@ aspGauge1 @@ aspCountAll
_ <- Random.nextDoubleBetween(-50d, 50d) @@ aspGauge2 @@ aspCountAll
} yield ()

// Just record something into a histogram
private lazy val observeNumbers = for {
_ <- nextDoubleBetween(0.0d, 120.0d) @@ aspHistogram @@ aspCountAll
_ <- nextIntBetween(100, 500) @@ aspSummary @@ aspCountAll
_ <- Random.nextDoubleBetween(0.0d, 120.0d) @@ aspHistogram @@ aspCountAll
_ <- Random.nextIntBetween(100, 500) @@ aspSummary @@ aspCountAll
} yield ()

// Observe Strings in order to capture unique values
private lazy val observeKey = for {
_ <- nextIntBetween(10, 20).map(v => s"myKey-$v") @@ aspSet @@ aspCountAll
_ <- Random.nextIntBetween(10, 20).map(v => s"myKey-$v") @@ aspSet @@ aspCountAll
} yield ()

def program: ZIO[ZEnv, Nothing, ExitCode] = for {
def program: ZIO[Any, Nothing, Unit] = for {
_ <- gaugeSomething.schedule(Schedule.spaced(200.millis)).forkDaemon
_ <- observeNumbers.schedule(Schedule.spaced(150.millis)).forkDaemon
_ <- observeKey.schedule(Schedule.spaced(300.millis)).forkDaemon
} yield ExitCode.success
} yield ()
}
```
```
31 changes: 14 additions & 17 deletions docs/metrics/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ title: "ZMX Metric Reference"

```scala mdoc:invisible
import zio._
import zio.random._
import zio.duration._
import zio.zmx.metrics._
import zio.zmx.state.DoubleHistogramBuckets
import zio.metrics._
```

All metrics in ZMX are defined in the form of aspects that can be applied to effects without changing
Expand Down Expand Up @@ -70,15 +67,15 @@ def countValueWith[A](name: String, tags: Label*)(f: A => Double): MetricAspect[

Create a counter named `countAll` which is incremented by `1` every time it is invoked.

```scala mdoc:silent
```scala
val aspCountAll = MetricAspect.count("countAll")
```

Now the counter can be applied to any effect. Note, that the same aspect can be applied
to more than one effect. In the example we would count the sum of executions of both effects
in the for comprehension.

```scala mdoc:silent
```scala
val countAll = for {
_ <- ZIO.unit @@ aspCountAll
_ <- ZIO.unit @@ aspCountAll
Expand All @@ -87,14 +84,14 @@ val countAll = for {

Create a counter named `countBytes` that can be applied to effects having the output type `Double`.

```scala mdoc:silent
```scala
val aspCountBytes = MetricAspect.countValue("countBytes")
```

Now we can apply it to effects producing `Double` (in a real application the value might be
the number of bytes read from a stream or something similar):

```scala mdoc:silent
```scala
val countBytes = nextDoubleBetween(0.0d, 100.0d) @@ aspCountBytes
```

Expand Down Expand Up @@ -135,21 +132,21 @@ def adjustGaugeWith[A](name: String, tags: Label*)(f: A => Double): MetricAspect

Create a gauge that can be set to absolute values, it can be applied to effects yielding a Double

```scala mdoc:silent
```scala
val aspGaugeAbs = MetricAspect.setGauge("setGauge")
```

Create a gauge that can be set relative to it's current value, it can be applied to effects
yielding a Double

```scala mdoc:silent
```scala
val aspGaugeRel = MetricAspect.adjustGauge("adjustGauge")
```

Now we can apply these effects to effects having an output type `Double`. Note that we can instrument
an effect with any number of aspects if the type constraints are satisfied.

```scala mdoc:silent
```scala
val gaugeSomething = for {
_ <- nextDoubleBetween(0.0d, 100.0d) @@ aspGaugeAbs @@ aspCountAll
_ <- nextDoubleBetween(-50d, 50d) @@ aspGaugeRel @@ aspCountAll
Expand Down Expand Up @@ -193,14 +190,14 @@ def observeHistogramWith[A](name: String, boundaries: Chunk[Double], tags: Label

Create a histogram with 12 buckets: `0..100` in steps of `10` and `Double.MaxValue`. It can be applied to effects yielding a `Double`.

```scala mdoc:silent
```scala
val aspHistogram =
MetricAspect.observeHistogram("histogram", DoubleHistogramBuckets.linear(0.0d, 10.0d, 11).boundaries)
```

Now we can apply the histogram to effects producing `Double`:

```scala mdoc:silent
```scala
val histogram = nextDoubleBetween(0.0d, 120.0d) @@ aspHistogram
```

Expand Down Expand Up @@ -260,13 +257,13 @@ Create a summary that can hold 100 samples, the max age of the samples is `1 day
error margin is `3%`. The summary should report the `10%`, `50%` and `90%` Quantile.
It can be applied to effects yielding an `Int`.

```scala mdoc:silent
```scala
val aspSummary =
MetricAspect.observeSummaryWith[Int]("mySummary", 1.day, 100, 0.03d, Chunk(0.1, 0.5, 0.9))(_.toDouble)
```

Now we can apply this aspect to an effect producing an `Int`:
```scala mdoc:silent
```scala
val summary = nextIntBetween(100, 500) @@ aspSummary
```

Expand Down Expand Up @@ -306,13 +303,13 @@ def occurrencesWith[A](name: String, setTag: String, tags: Label*)(
Create a Set to observe the occurrences of unique Strings.
It can be applied to effects yielding a String.

```scala mdoc:silent
```scala
val aspSet = MetricAspect.occurrences("mySet", "token")
```

Now we can generate some keys within an effect and start counting the occurrences
for each value.

```scala mdoc:silent
```scala
val set = nextIntBetween(10, 20).map(v => s"myKey-$v") @@ aspSet
```
10 changes: 5 additions & 5 deletions docs/metrics/prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mySet{token="myKey-12"} 10.0 1623589839194

## Serving Prometheus metrics

```scala mdoc:invisible
```scala
import zhttp.http._
import zhttp.service.Server

Expand All @@ -115,14 +115,14 @@ upon request. The state is encoded in the `Prometheus` case class and the single
type `String` holds the prometheus encoded metric state.

So, to retrieve the prometheus encoded state, the application can simply use
```scala mdoc:silent
```scala
val encoded = PrometheusClient.snapshot
val content = encoded.map(_.value)
```

In our example application we use [zio-http](https://github.com/dream11/zio-http) to serve the metrics. Other application might choose another HTTP server framework if required.

```scala mdoc:silent
```scala
private lazy val indexPage = HttpData.CompleteData(
Chunk
.fromArray("""<html>
Expand All @@ -146,7 +146,7 @@ private lazy val httpEffect = Http.collectM[Request] {
Now, using the HTTP server and the [instrumentation examples](example.md) we can create an effect
that simply runs the sample effects with their instrumentation until the user presses any key.

```scala mdoc:silent
```scala
private lazy val execute =
(for {
s <- ((Server.port(8080) ++ Server.app(static +++ httpEffect)).start *> ZIO.never).forkDaemon
Expand All @@ -160,7 +160,7 @@ private lazy val execute =
Finally, within a `ZIO.App` we can override the run method, which is now simply the execute
method with a Prometheus client provided in it´s environment:

```scala mdoc:silent
```scala
def run(args: List[String]): URIO[ZEnv, ExitCode] =
execute.provideCustomLayer(
PrometheusClient.live ++ ServerChannelFactory.auto ++ EventLoopGroup.auto(5)
Expand Down
15 changes: 4 additions & 11 deletions docs/metrics/statsd.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ title: "StatsD Client"

```scala
import zio._
import zio.console._
import zio.duration._
import zio.zmx.metrics._

import zio.zmx.statsd._
import zio.zmx.InstrumentedSample
import zio.metrics._
```

In a normal StatsD setup we will find a StatsD agent with an open UDP port where applications send their
Expand Down Expand Up @@ -96,7 +91,7 @@ mySet:1|g|#token:myKey-12

## The ZMX StatsD example

```scala mdoc:invisible
```scala
import java.net.InetSocketAddress

import zio._
Expand All @@ -114,7 +109,7 @@ to a specified UDP destination.

Again we need an effect that runs our instrumented code until the user presses any key:

```scala mdoc:silent
```scala
val execute =
for {
fiber <- instrumentedSample.program.fork
Expand All @@ -125,7 +120,7 @@ val execute =

Now, we can override the `run` method of our ZIO `App` and simply provide a `StatsDListener`.

```scala mdoc:silent
```scala
def run(args: List[String]): URIO[ZEnv, ExitCode] =
execute.provideCustomLayer(StatsdClient.default).orDie
```
Expand Down Expand Up @@ -202,5 +197,3 @@ sbt examples/run

![A simple Datadog Dashboard](/zio-zmx/img/ZIOZmx-Datadog.png)



Loading

0 comments on commit ae65631

Please sign in to comment.