-
Notifications
You must be signed in to change notification settings - Fork 0
/
b3.clj
77 lines (64 loc) · 2.13 KB
/
b3.clj
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(ns zipkin-clj.b3
"See https://github.com/openzipkin/b3-propagation"
(:require [zipkin-clj.core :as core]))
;; ====================================================================
;; Internal
;; ====================================================================
(defn- parse-flags [^String s]
(case s
("1" "true")
{:sample? true}
("0" "false")
{:sample? false}
"d"
{:debug? true}
nil))
;; ====================================================================
;; API
;; ====================================================================
(defn encode
"Encodes `span` for propagation in a single \"b3\" header.
`span` must be a valid span, e.g., produced by `zipkin-clj.core/span`.
Returns a string to be used a \"b3\" header value. "
[span]
(let [flags (if (-> span :debug)
"d"
(when-some [sample? (::core/sample? span)]
(if sample? "1" "0")))]
(str
(:traceId span) "-" (:id span)
(when flags
(str
"-" flags
(when-let [parent-id (:parentId span)]
parent-id))))))
(defn decode
"Converts a \"b3\" header value into a span.
Returns a span to use, e.g., with `zipkin-clj.core/trace-context!` or
as a parent span. The span doesn't have start time and therefore is not
supposed to be used with `zipkin-clj.core/finish-span`."
[^String s]
(if-some [flags (parse-flags s)]
(core/span flags)
(let [t-end (case (.charAt s 16) \- 16 32)
trace-id (subs s 0 t-end)
s-start (inc t-end)
s-end (+ s-start 16)
span-id (subs s s-start s-end)
f-start (inc s-end)
f-end (inc f-start)
flags-str (when (> (count s) f-start) (subs s f-start f-end))
flags (parse-flags flags-str)
p-start (inc f-end)
parent-id (when (> (count s) f-end) (subs s p-start))]
(core/span
(merge
{:trace-id trace-id
:id span-id}
flags
(when parent-id
{:parent
(merge
{:traceId trace-id
:id parent-id}
flags)}))))))