/
core.clj
164 lines (137 loc) · 4.46 KB
/
core.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(ns clojure-elastic-apm.core
(:import [co.elastic.apm.api ElasticApm Transaction Span HeaderExtractor Outcome]))
(defn current-apm-transaction []
(ElasticApm/currentTransaction))
(defn current-apm-span []
(ElasticApm/currentSpan))
(defn ^:deprecated add-tag [^Span span-or-tx k v]
(.setLabel span-or-tx (name k) (str v)))
(defn set-label [^Span span-or-tx k v]
(cond
(instance? Number v) (.setLabel span-or-tx (name k) ^Number v)
(instance? Boolean v) (.setLabel span-or-tx (name k) ^boolean v)
:else (.setLabel span-or-tx (name k) (str v))))
(defn set-name [^Span span-or-tx name]
(.setName span-or-tx name))
(defn set-result [^Transaction tx ^String result]
(.setResult tx result))
(def outcome-success Outcome/SUCCESS)
(def outcome-failure Outcome/FAILURE)
(def outcome-unknown Outcome/UNKNOWN)
(defn set-outcome [^Span span-or-tx ^Outcome outcome]
(.setOutcome span-or-tx outcome))
(defn set-outcome-success [^Span span-or-tx]
(set-outcome span-or-tx outcome-success))
(defn set-outcome-failure [^Span span-or-tx]
(set-outcome span-or-tx outcome-failure))
(defn set-outcome-unknown [^Span span-or-tx]
(set-outcome span-or-tx outcome-unknown))
(defn ^HeaderExtractor trace-extractor [traceparent]
(reify HeaderExtractor
(getFirstHeader [_ _] traceparent)))
(def type-request Transaction/TYPE_REQUEST)
(defn start-transaction
([]
(start-transaction {}))
([{tx-name :name
tx-type :type
tags :tags
labels :labels
traceparent :traceparent}]
(let [tx (if traceparent
(ElasticApm/startTransactionWithRemoteParent (trace-extractor traceparent))
(ElasticApm/startTransaction))]
(when tx-name
(set-name tx tx-name))
(when tx-type
(.setType tx tx-type))
(doseq [[k v] labels]
(set-label tx k v))
(doseq [[k v] tags]
(set-label tx k v))
tx)))
(defn create-span
([]
(create-span {}))
([{parent :parent
span-name :name
labels :labels
tags :tags}]
(let [span (.startSpan (or parent (current-apm-span)))]
(when span-name
(set-name span span-name))
(doseq [[k v] labels]
(set-label span k v))
(doseq [[k v] tags]
(set-label span k v))
span)))
(defn create-exit-span [{parent :parent
span-name :name
labels :labels
type :type
subtype :subtype
action :action}]
(let [span (.startExitSpan (or parent (current-apm-span))
(or type "ext")
(or subtype "undefined subtype")
action)]
(when span-name
(set-name span span-name))
(doseq [[k v] labels]
(set-label span k v))
span))
(defn end [^Span span-or-tx]
(.end span-or-tx))
(defn activate [^Span span-or-tx]
(.activate span-or-tx))
(defn capture-exception [^Span span-or-tx ^Exception e]
(.captureException span-or-tx e))
(defn apm-transaction*
([func]
(apm-transaction* func {}))
([func opts]
(let [activate? (or (nil? (:activate? opts)) (:activate? opts))
tx (start-transaction opts)]
(try
(if activate?
(with-open [_ (activate tx)]
(func tx))
(func tx))
(catch Exception e
(capture-exception tx e)
(throw e))
(finally
(end tx))))))
(defn apm-span*
([create-span-fn func]
(apm-span* create-span-fn func {}))
([create-span-fn func opts]
(let [activate? (or (nil? (:activate? opts)) (:activate? opts))
span (create-span-fn opts)]
(try
(if activate?
(with-open [_ (activate span)]
(func span))
(func span))
(catch Exception e
(capture-exception span e)
(throw e))
(finally
(end span))))))
(defmacro with-apm-transaction [binding & body]
`(apm-transaction* (^{:once true} fn* [~(first binding)] ~@body)
~(second binding)))
(defmacro with-apm-span [binding & body]
`(apm-span* create-span
(^{:once true} fn* [~(first binding)] ~@body)
~(second binding)))
(defmacro with-apm-exit-span [binding & body]
`(apm-span* create-exit-span
(^{:once true} fn* [~(first binding)] ~@body)
~(second binding)))
(defn catch-error-to-apm [thunk]
(with-apm-transaction [tx (current-apm-transaction)]
(try
(thunk)
(catch Exception e
(capture-exception tx e)))))