Example Ktor application instrumented with OpenTracing using ktor-opentracing.
Retrieves data about earthquakes that happened today using an API from the U.S. Geological Survey.
-
Start an all-in-one Jaeger backend with in-memory storage.
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 14250:14250 \ -p 9411:9411 \ jaegertracing/all-in-one:1.20
-
Start the application.
./gradlew run
-
Send some requests. Other routes available are
/earthquake/biggest
and/earthquake/biggerthan/5
(where 5 is a parameter).curl localhost:8080/earthquake/latest { "location" : "21 km SSE of Karluk, Alaska", "magnitude" : 1.9, "timeGMT" : "2020-11-02 09:46:39" }
-
See traces in Jaeger.
-
Stop the Jaeger docker container.
docker ps docker stop <containerId>
-
Import ktor-opentracing and the Java Jaeger client (commit).
implementation "io.jaegertracing:jaeger-client:1.3.2" implementation "com.zopa:ktor-opentracing:0.3.5"
-
Instantiate a tracer and register it in GlobalTracer (commit).
val tracer = Configuration("tracing-example") .withSampler(Configuration.SamplerConfiguration.fromEnv() .withType(ConstSampler.TYPE) .withParam(1)) .withReporter(Configuration.ReporterConfiguration.fromEnv() .withLogSpans(true) .withSender( Configuration.SenderConfiguration() .withAgentHost("localhost") .withAgentPort(6831))).tracerBuilder .withScopeManager(ThreadContextElementScopeManager()) .build() GlobalTracer.registerIfAbsent(tracer)
-
Install the
OpenTracingServer
feature into the application call pipeline (commit).install(OpenTracingServer)
-
Install the
OpenTracingClient
feature onto the http client (commit).install(OpenTracingClient)
-
Instrument method calls using the
span
helper function (commit).= span("EarthquakeClient.getBiggest()") {
From the lamdba expression passed to
span
, you can add tags or logs to the span by callingsetTag()
orlog()
.