Giter Club home page Giter Club logo

zio-telemetry's Introduction

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.

zio-telemetry's People

Contributors

scala-steward avatar mijicd avatar runtologist avatar arjun-1 avatar drmarjanovic avatar ghostdogpr avatar bojanbla avatar 3link avatar adamgfraser avatar andrzejressel avatar easel avatar frekw avatar fristi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.