-
Notifications
You must be signed in to change notification settings - Fork 76
/
JaegerTracer.scala
58 lines (48 loc) · 1.89 KB
/
JaegerTracer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2019 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package natchez
package jaeger
import cats.effect._
import cats.implicits._
import io.jaegertracing.Configuration
import io.jaegertracing.internal.exceptions.UnsupportedFormatException
import io.jaegertracing.internal.{ JaegerTracer => NativeJaegerTracer }
import io.opentracing.propagation.Format
import io.opentracing.propagation.TextMapAdapter
import scala.jdk.CollectionConverters._
object Jaeger {
def entryPoint[F[_]: Sync](system: String)(
configure: Configuration => F[NativeJaegerTracer]
): Resource[F, EntryPoint[F]] =
Resource.make(
Sync[F].delay(
new Configuration(system)).flatMap(configure))(
c => Sync[F].delay(c.close)
).map { t =>
new EntryPoint[F] {
def continue(name: String, kernel: Kernel): Resource[F,Span[F]] =
Resource.make(
Sync[F].delay {
val p = t.extract(
Format.Builtin.HTTP_HEADERS,
new TextMapAdapter(kernel.toHeaders.asJava)
)
t.buildSpan(name).asChildOf(p).start()
}
)(s => Sync[F].delay(s.finish)).map(JaegerSpan(t, _))
def root(name: String): Resource[F,Span[F]] =
Resource.make(
Sync[F].delay(t.buildSpan(name).start()))(
s => Sync[F].delay(s.finish)
).map(JaegerSpan(t, _))
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 a => a.pure[Resource[F, ?]]
} recoverWith {
case _: UnsupportedFormatException => root(name)
}
}
}
}