Skip to content

Instantly share code, notes, and snippets.

@alvinfrancis
Created October 17, 2019 17:39
Show Gist options
  • Save alvinfrancis/4ad9a96b0d24d7f563315f08a5d9c97a to your computer and use it in GitHub Desktop.
Save alvinfrancis/4ad9a96b0d24d7f563315f08a5d9c97a to your computer and use it in GitHub Desktop.
opentracing-clj example with Jaeger

Run Jaeger's all-in-one using Docker. See: https://www.jaegertracing.io/docs/1.14/getting-started/.

docker run --rm --name jaeger -p6831:6831/udp -p16686:16686 jaegertracing/all-in-one:latest

Run the example opentracing-clj project using the Clojure CLI. Note the environment variables set for usage with testing Jaeger tracing.

JAEGER_SAMPLER_TYPE=const JAEGER_SAMPLER_PARAM=1 JAEGER_SERVICE_NAME=example-service clj -m example

Navigate to the Jaeger UI at http://localhost:16686 to inspect the sample trace. Multiple runs of the example clojure script will create additional traces.

{:deps {opentracing-clj {:mvn/version "0.1.5"}
;; https://github.com/jaegertracing/jaeger-client-java
io.jaegertracing/jaeger-client {:mvn/version "0.32.0"}}
:paths ["."]}
(ns example
(:require [opentracing-clj.core :as tracing])
(:import
(io.opentracing.contrib.tracerresolver TracerResolver)
(io.opentracing.util GlobalTracer)))
(defn sleep
[secs]
(Thread/sleep secs))
(defn init-tracer!
[]
(when-not (GlobalTracer/isRegistered)
;; Adding the Jaeger Tracing library configures OpenTracing's TracerResolver to use the Jaeger Java Client.
(GlobalTracer/register (TracerResolver/resolveTracer))))
(defn -main []
(init-tracer!)
(tracing/with-span [outer {:name "outer"}]
(println "Sleeping for 1 second ...")
(Thread/sleep 1000)
(tracing/with-span [inner {:name "inner"}]
(println "Sleeping for 2 second ...")
(Thread/sleep 2000)
(println "Inner end."))
(println "Sleeping for 3 second ...")
(Thread/sleep 3000)
(println "Outer end.")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment