Skip to content

Commit

Permalink
Upgrade to 1.0.0-RC18 (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
runtologist authored Mar 14, 2020
1 parent 9c9fa84 commit 85aff0e
Show file tree
Hide file tree
Showing 15 changed files with 328 additions and 364 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
**/.DS_Store
project/metals.sbt
target
.bloop
.metals
12 changes: 7 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ inThisBuild(
)
)

Global / onChangedBuildSource := ReloadOnSourceChanges

addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")

Expand All @@ -41,11 +43,11 @@ lazy val root =
.aggregate(core, example)

val http4sVersion = "0.21.1"
val jaegerVersion = "1.1.0"
val sttpVersion = "2.0.1"
val jaegerVersion = "1.2.0"
val sttpVersion = "2.0.5"
val opentracingVersion = "0.33.0"
val zipkinVersion = "2.12.1"
val zioVersion = "1.0.0-RC17"
val zioVersion = "1.0.0-RC18-2"

lazy val core =
project
Expand All @@ -62,7 +64,7 @@ lazy val core =
)
)

testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework"))
Global / testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework"))

lazy val example =
project
Expand All @@ -83,7 +85,7 @@ lazy val example =
"com.github.pureconfig" %% "pureconfig" % "0.12.3",
"com.softwaremill.sttp.client" %% "async-http-client-backend-zio" % sttpVersion,
"com.softwaremill.sttp.client" %% "circe" % sttpVersion,
"dev.zio" %% "zio-interop-cats" % "2.0.0.0-RC10",
"dev.zio" %% "zio-interop-cats" % "2.0.0.0-RC12",
"io.zipkin.reporter2" % "zipkin-reporter" % zipkinVersion,
"io.zipkin.reporter2" % "zipkin-sender-okhttp3" % zipkinVersion
)
Expand Down
25 changes: 0 additions & 25 deletions modules/core/src/main/scala/zio/telemetry/Telemetry.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,65 @@ import io.opentracing.propagation.Format
import io.opentracing.Span
import io.opentracing.SpanContext
import io.opentracing.Tracer
import zio.URIO
import zio.ZIO
import zio.clock.Clock
import zio.telemetry._
import zio.Task
import java.util.concurrent.TimeUnit
import zio.UIO
import scala.jdk.CollectionConverters._

trait OpenTracing extends Telemetry {
override def telemetry: OpenTracing.Service
}
import zio._
import zio.clock.Clock

object OpenTracing {

trait Service extends Telemetry.Service {
override type A = Span
trait Service {
private[opentracing] val tracer: Tracer
def currentSpan: FiberRef[Span]
def root(opName: String): UIO[Span]
def span(span: Span, opName: String): UIO[Span]
def finish(span: Span): UIO[Unit]
def error(span: Span, cause: Cause[_], tagError: Boolean, logError: Boolean): UIO[Unit]
}

def spanFrom[R, R1 <: R with Clock with OpenTracing, E, A, C <: Object](
def live(tracer: Tracer, rootOpName: String = "ROOT"): ZLayer[Clock, Nothing, OpenTracing] =
ZLayer.fromManaged(managed(tracer, rootOpName))

private[opentracing] def managed(tracer: Tracer, rootOpName: String): ZManaged[Clock, Nothing, OpenTracing.Service] =
ZManaged.make(
for {
span <- UIO(tracer.buildSpan(rootOpName).start())
ref <- FiberRef.make(span)
env <- ZIO.environment[Clock]
clock = env.get[Clock.Service]
tracer_ = tracer
} yield new OpenTracing.Service {
override val tracer: Tracer = tracer_
override val currentSpan: FiberRef[Span] = ref

override def root(opName: String): UIO[Span] =
UIO(tracer.buildSpan(opName).start())

override def span(span: Span, opName: String): UIO[Span] =
for {
old <- currentSpan.get
child <- UIO(tracer.buildSpan(opName).asChildOf(old).start())
} yield child

override def finish(span: Span): UIO[Unit] =
clock.currentTime(TimeUnit.MICROSECONDS).map(span.finish)

override def error(span: Span, cause: Cause[_], tagError: Boolean, logError: Boolean): UIO[Unit] =
UIO(span.setTag("error", true)).when(tagError) *>
UIO(span.log(Map("error.object" -> cause, "stack" -> cause.prettyPrint).asJava)).when(logError)
}
)(_.currentSpan.get.flatMap(span => UIO(span.finish())))

def spanFrom[R, R1 <: R with Clock with OpenTracing, E, Span, C <: Object](
format: Format[C],
carrier: C,
zio: ZIO[R, E, A],
zio: ZIO[R, E, Span],
opName: String,
tagError: Boolean = true,
logError: Boolean = true
): ZIO[R1, E, A] =
): ZIO[R1, E, Span] =
ZIO.accessM { env =>
val telemetry = env.telemetry
val telemetry = env.get[OpenTracing.Service]
Task(telemetry.tracer.extract(format, carrier))
.fold(_ => None, Option.apply)
.flatMap {
Expand All @@ -49,14 +78,14 @@ object OpenTracing {

def inject[C <: Object](format: Format[C], carrier: C): ZIO[OpenTracing, Nothing, Unit] =
ZIO.accessM { env =>
val telemetry = env.telemetry
val telemetry = env.get[OpenTracing.Service]
telemetry.currentSpan.get.flatMap { span =>
ZIO.effectTotal(telemetry.tracer.inject(span.context(), format, carrier)).unit
}
}

def context: ZIO[OpenTracing, Nothing, SpanContext] =
ZIO.accessM { _.telemetry.currentSpan.get.map(_.context) }
ZIO.accessM(_.get.currentSpan.get.map(_.context))

def getBaggageItem(key: String): URIO[OpenTracing, Option[String]] =
getSpan.map(_.getBaggageItem(key)).map(Option(_))
Expand Down Expand Up @@ -88,8 +117,8 @@ object OpenTracing {
} yield ()

private def getSpan: URIO[OpenTracing, Span] =
ZIO.accessM { _.telemetry.currentSpan.get }
ZIO.accessM(_.get.currentSpan.get)

private def getCurrentTimeMicros: ZIO[Clock, Nothing, Long] =
ZIO.accessM(_.clock.currentTime(TimeUnit.MICROSECONDS))
ZIO.accessM(_.get.currentTime(TimeUnit.MICROSECONDS))
}
94 changes: 50 additions & 44 deletions modules/core/src/main/scala/zio/telemetry/opentracing/package.scala
Original file line number Diff line number Diff line change
@@ -1,57 +1,63 @@
package zio.telemetry

import java.util.concurrent.TimeUnit

import io.opentracing.Span
import io.opentracing.Tracer
import io.opentracing.propagation.Format
import zio.Cause
import zio.FiberRef
import zio.UIO
import zio.URIO
import zio.ZIO
import zio.ZManaged
import zio._
import zio.clock.Clock

import scala.jdk.CollectionConverters._

package object opentracing {
type OpenTracing = Has[OpenTracing.Service]

def managed(tracer: Tracer, rootOpName: String = "ROOT"): ZManaged[Clock, Nothing, OpenTracing] =
ZManaged.make(
for {
span <- UIO(tracer.buildSpan(rootOpName).start())
ref <- FiberRef.make(span)
tracer_ = tracer
} yield new OpenTracing {

override val telemetry: OpenTracing.Service = new OpenTracing.Service {
override type A = Span

override val tracer: Tracer = tracer_
override val currentSpan: FiberRef[Span] = ref

override def root(opName: String): URIO[Clock, Span] =
UIO(tracer.buildSpan(opName).start())

override def span(span: Span, opName: String): URIO[Clock, Span] =
for {
old <- currentSpan.get
child <- UIO(tracer.buildSpan(opName).asChildOf(old).start())
} yield child

override def finish(span: Span): URIO[Clock, Unit] =
URIO.accessM(_.clock.currentTime(TimeUnit.MICROSECONDS).map(span.finish))

override def error(span: Span, cause: Cause[_], tagError: Boolean, logError: Boolean): UIO[Unit] =
UIO(span.setTag("error", true)).when(tagError) *>
UIO(span.log(Map("error.object" -> cause, "stack" -> cause.prettyPrint).asJava)).when(logError)
implicit final class OpenTracingZioOps[R, E, A](val zio: ZIO[R, E, A]) extends AnyVal {

}
}
)(_.telemetry.currentSpan.get.flatMap(span => UIO(span.finish())))
def root[R1 <: R with Clock with OpenTracing](
opName: String,
tagError: Boolean = true,
logError: Boolean = true
): ZIO[R1, E, A] =
for {
telemetry <- getTelemetry
root <- telemetry.root(opName)
r <- span(telemetry)(root, tagError, logError)
} yield r

implicit final class OpenTracingZioOps[R, E, A](val zio: ZIO[R, E, A]) extends AnyVal {
def span[R1 <: R with Clock with OpenTracing](
opName: String,
tagError: Boolean = true,
logError: Boolean = true
): ZIO[R1, E, A] =
for {
telemetry <- getTelemetry
old <- getSpan(telemetry)
child <- telemetry.span(old, opName)
r <- span(telemetry)(child, tagError, logError)
} yield r

def span[R1 <: R with Clock](telemetry: OpenTracing.Service)(
span: Span,
tagError: Boolean,
logError: Boolean
): ZIO[R1, E, A] =
for {
old <- getSpan(telemetry)
r <- (setSpan(telemetry)(span) *>
zio.catchAllCause { cause =>
telemetry.error(span, cause, tagError, logError) *>
IO.done(Exit.Failure(cause))
}).ensuring(
telemetry.finish(span) *>
setSpan(telemetry)(old)
)
} yield r

private def setSpan(telemetry: OpenTracing.Service)(span: Span): UIO[Unit] =
telemetry.currentSpan.set(span)

private def getSpan(telemetry: OpenTracing.Service): UIO[Span] =
telemetry.currentSpan.get

private def getTelemetry: ZIO[OpenTracing, Nothing, OpenTracing.Service] =
ZIO.environment[OpenTracing].map(_.get[OpenTracing.Service])

def spanFrom[R1 <: R with Clock with OpenTracing, C <: Object](
format: Format[C],
Expand Down
58 changes: 0 additions & 58 deletions modules/core/src/main/scala/zio/telemetry/package.scala

This file was deleted.

Loading

0 comments on commit 85aff0e

Please sign in to comment.