zio-telemetry


License

License

GroupId

GroupId

dev.zio
ArtifactId

ArtifactId

zio-telemetry_2.12
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

zio-telemetry
zio-telemetry
Project URL

Project URL

https://github.com/zio/zio-telemetry/
Project Organization

Project Organization

dev.zio
Source Code Management

Source Code Management

https://github.com/zio/zio-telemetry/

Download zio-telemetry_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/dev.zio/zio-telemetry_2.12/ -->
<dependency>
    <groupId>dev.zio</groupId>
    <artifactId>zio-telemetry_2.12</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/dev.zio/zio-telemetry_2.12/
implementation 'dev.zio:zio-telemetry_2.12:0.1.0'
// https://jarcasting.com/artifacts/dev.zio/zio-telemetry_2.12/
implementation ("dev.zio:zio-telemetry_2.12:0.1.0")
'dev.zio:zio-telemetry_2.12:jar:0.1.0'
<dependency org="dev.zio" name="zio-telemetry_2.12" rev="0.1.0">
  <artifact name="zio-telemetry_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='dev.zio', module='zio-telemetry_2.12', version='0.1.0')
)
libraryDependencies += "dev.zio" % "zio-telemetry_2.12" % "0.1.0"
[dev.zio/zio-telemetry_2.12 "0.1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
dev.zio : zio_2.12 jar 1.0.0-RC18-2
io.opentracing : opentracing-api jar 0.33.0
org.scala-lang.modules : scala-collection-compat_2.12 jar 2.1.4

test (3)

Group / Artifact Type Version
dev.zio : zio-test_2.12 jar 1.0.0-RC18-2
dev.zio : zio-test-sbt_2.12 jar 1.0.0-RC18-2
io.opentracing : opentracing-mock jar 0.33.0

Project Modules

There are no modules declared in this project.

ZIO telemetry

CircleCI Releases Snapshots Discord

ZIO telemetry is purely-functional and type-safe. It provides clients for OpenTracing and OpenTelemetry

OpenTracing

OpenTracing is a standard and API for distributed tracing, i.e. collecting timings, and logs across process boundaries. Well known implementations are Jaeger and Zipkin.

First, add the following dependency to your build.sbt:

"dev.zio" %% "zio-opentracing" % <version>

To use ZIO telemetry, you will need a Clock and a OpenTelemetry service in your environment:

import io.opentracing.mock.MockTracer
import io.opentracing.propagation._
import zio._
import zio.clock.Clock
import zio.opentracing._

val tracer = new MockTracer

val managedEnvironment = 
  for {
    clock_ <- ZIO.environment[Clock].toManaged_
    ot     <- managed(tracer)
  } yield new Clock with Telemetry {
    override val clock: Clock.Service[Any]    = clock_.clock
    override def telemetry: Telemetry.Service = ot.telemetry
  }

After importing import zio.opentracing._, additional combinators on ZIOs are available to support starting child spans, tagging, logging and managing baggage.

// start a new root span and set some baggage item
val zio = UIO.unit
             .setBaggage("foo", "bar")
             .root("root span")
          
// start a child of the current span, set a tag and log a message
val zio = UIO.unit
             .tag("http.status_code", 200)
             .log("doing some serious work here!")
             .span("child span")

To propagate contexts across process boundaries, extraction and injection can be used. The current span context is injected into a carrier, which is passed through some side channel to the next process. There it is injected back and a child span of it is started. For the example we use the standardized TextMap carrier. For details about extraction and injection, please refer to OpenTracing Documentation.

Due to the use of the (mutable) OpenTracing carrier APIs, injection and extraction are not referentially transparent.

val buffer = new TextMapAdapter(mutable.Map.empty.asJava)
for {
  _ <- zio.inject(Format.Builtin.TEXT_MAP, buffer)
  _ <- zio.spanFrom(Format.Builtin.TEXT_MAP, buffer, "child of remote span")
} yield buffer

Example usage

Firstly, start Jaeger by running following command:

docker run -d --name jaeger \
  -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
  -p 5775:5775/udp \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 14268:14268 \
  -p 9411:9411 \
  jaegertracing/all-in-one:1.6

To check if it's running properly visit Jaeger UI. More info can be found here.

Our application contains two services:

  1. proxy service
  2. backend service

Proxy service

Represents entry point to example distributed system. It exposes /statuses endpoint which returns list of system's services statuses.

In order to start service run:

sbt "opentracingExample/runMain zio.telemetry.opentracing.example.ProxyServer"

If it's start properly it should be available on http://0.0.0.0:8080/statuses.

Backend service

Represents "internal" service of the system. It exposes /status endpoint which returns service status.

In order to start service run:

sbt "opentracingExample/runMain zio.telemetry.opentracing.example.BackendServer"

If it's start properly it should be available on http://0.0.0.0:9000/status.

Configuration is given in application.conf.

After both services are properly started, running following command

curl -X GET http://0.0.0.0:8080/statuses

should return following response:

{
  "data": [
    {
      "name": "backend",
      "version": "1.0.0",
      "status": "up"
    },
    {
      "name": "proxy",
      "version": "1.0.0",
      "status": "up"
    }
  ]
}

Simultaneously, it will create trace that will be stored in Jaeger backend.

OpenTelemetry

Use this dependency instead:

"dev.zio" %% "zio-opentelemetry" % <version>

First, start Jaeger by running

docker run --rm -it \
  -p 16686:16686 \
  -p 14250:14250 \
  jaegertracing/all-in-one:1.16

Then start the proxy server

sbt "opentelemetryExample/runMain zio.telemetry.opentelemetry.example.ProxyServer"

and the backend server

sbt "opentelemetryExample/runMain zio.telemetry.opentelemetry.example.BackendServer"

Now perform the following request:

curl -X GET http://0.0.0.0:8080/statuses

and head over to http://localhost:16686/ to see the result.

dev.zio

ZIO

ZIO — Real World Functional Programming

Versions

Version
0.1.0
0.0.3
0.0.2
0.0.1