-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from tpolecat/merge-with-master
merge with master
- Loading branch information
Showing
18 changed files
with
432 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2019-2020 by Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package natchez | ||
package datadog | ||
|
||
import cats.effect._ | ||
import cats.syntax.all._ | ||
import io.opentracing.propagation.{Format, TextMapAdapter} | ||
import io.{opentracing => ot} | ||
|
||
import java.net.URI | ||
import scala.jdk.CollectionConverters._ | ||
|
||
final class DDEntryPoint[F[_]: Sync](tracer: ot.Tracer, uriPrefix: Option[URI]) extends EntryPoint[F] { | ||
override def root(name: String): Resource[F, Span[F]] = | ||
Resource.make( | ||
Sync[F].delay(tracer.buildSpan(name).start()))( | ||
s => Sync[F].delay(s.finish())) | ||
.map(DDSpan(tracer, _, uriPrefix)) | ||
|
||
override def continue(name: String, kernel: Kernel): Resource[F, Span[F]] = | ||
Resource.make( | ||
Sync[F].delay { | ||
val spanContext = tracer.extract( | ||
Format.Builtin.HTTP_HEADERS, | ||
new TextMapAdapter(kernel.toHeaders.asJava) | ||
) | ||
tracer.buildSpan(name).asChildOf(spanContext).start() | ||
} | ||
)(s => Sync[F].delay(s.finish())).map(DDSpan(tracer, _, uriPrefix)) | ||
|
||
override def continueOrElseRoot(name: String, kernel: Kernel): Resource[F, Span[F]] = | ||
continue(name, kernel) flatMap { | ||
case null => root(name) // hurr, means headers are incomplete or invalid | ||
case span => span.pure[Resource[F, *]] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
modules/examples/src/main/scala-2/GlobalTracerExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) 2019-2020 by Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package example | ||
|
||
import cats._ | ||
import cats.data.Kleisli | ||
import cats.effect._ | ||
import cats.syntax.all._ | ||
import natchez._ | ||
import scala.util.Random | ||
import scala.concurrent.duration._ | ||
import java.net.URI | ||
|
||
|
||
object GlobalTracerMain extends IOApp { | ||
|
||
def runF[F[_]: Sync: Trace: Parallel: Timer]: F[Unit] = | ||
Trace[F].span("Sort some stuff!") { | ||
for { | ||
as <- Sync[F].delay(List.fill(10)(Random.nextInt(1000))) | ||
_ <- Sort.qsort[F, Int](as) | ||
u <- Trace[F].traceUri | ||
_ <- u.traverse(uri => Sync[F].delay(println(s"View this trace at $uri"))) | ||
_ <- Sync[F].delay(println("Done.")) | ||
} yield () | ||
} | ||
|
||
def globalTracerEntryPoint[F[_]: Sync]: F[Option[EntryPoint[F]]] = { | ||
// Datadog | ||
// import natchez.datadog.DDTracer | ||
// val prefix = Some(new URI("https://app.datadoghq.com")) // https://app.datadoghq.eu for Europe | ||
// DDTracer.globalTracerEntryPoint[F](prefix) | ||
|
||
// Jaeger | ||
import natchez.jaeger.Jaeger | ||
val prefix = Some(new URI("http://localhost:16686")) | ||
Jaeger.globalTracerEntryPoint[F](prefix) | ||
|
||
// Lightstep | ||
// import natchez.lightstep.Lightstep | ||
// Lightstep.globalTracerEntryPoint[F] | ||
|
||
} | ||
|
||
def run(args: List[String]): IO[ExitCode] = { | ||
globalTracerEntryPoint[IO].flatMap { | ||
case None => IO.delay { | ||
println("No tracer registered to the global tracer. Is your agent attached with tracing enabled?") | ||
} as ExitCode.Error | ||
case Some(ep) => | ||
ep.root("this is the root span") | ||
.use(span => runF[Kleisli[IO, Span[IO], *]].run(span)) *> | ||
IO.sleep(1.second) as ExitCode.Success // Turns out Tracer.close() in Jaeger doesn't block. Annoying. Maybe fix in there? | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2019-2020 by Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package example | ||
|
||
import cats._ | ||
import cats.effect.Timer | ||
import cats.syntax.all._ | ||
import natchez.Trace | ||
import scala.concurrent.duration._ | ||
|
||
object Sort { | ||
|
||
// Intentionally slow parallel quicksort, to demonstrate branching. If we run too quickly it seems | ||
// to break Jaeger with "skipping clock skew adjustment" so let's pause a bit each time. | ||
def qsort[F[_]: Monad: Parallel: Trace: Timer, A: Order](as: List[A]): F[List[A]] = | ||
Trace[F].span(as.mkString(",")) { | ||
Timer[F].sleep(10.milli) *> { | ||
as match { | ||
case Nil => Monad[F].pure(Nil) | ||
case h :: t => | ||
val (a, b) = t.partition(_ <= h) | ||
(qsort[F, A](a), qsort[F, A](b)).parMapN(_ ++ List(h) ++ _) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.