-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
tracing.clj
81 lines (73 loc) · 2.88 KB
/
tracing.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
(ns sentry-clj.tracing
(:import
[io.sentry CustomSamplingContext EventProcessor ITransaction Scope Sentry SpanStatus TransactionContext]))
(def span-status
{:ok SpanStatus/OK
:cancel SpanStatus/CANCELLED
:internal-error SpanStatus/INTERNAL_ERROR
:unknown SpanStatus/UNKNOWN
:unknown-error SpanStatus/UNKNOWN_ERROR
:invalid-argument SpanStatus/INVALID_ARGUMENT
:deadline-exceeded SpanStatus/DEADLINE_EXCEEDED
:not-found SpanStatus/NOT_FOUND
:already-exists SpanStatus/ALREADY_EXISTS
:permisson-denied SpanStatus/PERMISSION_DENIED
:resource-exhaused SpanStatus/RESOURCE_EXHAUSTED
:fail-precondition SpanStatus/FAILED_PRECONDITION
:aborted SpanStatus/ABORTED
:out-of-range SpanStatus/OUT_OF_RANGE
:unimplemented SpanStatus/UNIMPLEMENTED
:unavailable SpanStatus/UNAVAILABLE
:data-loss SpanStatus/DATA_LOSS
:unauthenticated SpanStatus/UNAUTHENTICATED})
(defn compute-custom-sampling-context
"Compute a custom sampling context has key and info."
^CustomSamplingContext
[key info]
(let [csc (CustomSamplingContext.)]
(.set csc key info)
csc))
(defn start-transaction
"Start tracing transactions.
If a sentry-trace-header is given, connect the exsiting transaction."
[name operation custom-sampling-context sentry-trace-header]
(if sentry-trace-header
(let [contexts (TransactionContext/fromSentryTrace name operation (io.sentry.SentryTraceHeader. sentry-trace-header))]
(-> (Sentry/getCurrentHub)
(.startTransaction contexts ^CustomSamplingContext custom-sampling-context true)))
(-> (Sentry/getCurrentHub)
(.startTransaction ^String name "http.server" ^CustomSamplingContext custom-sampling-context true))))
(defn swap-scope-request!
"Set request info to the scope."
[^Scope scope req]
(.setRequest scope req))
(defn add-event-processor
"Add Event Processor to the scope.
event-processor is executed when tracing transaction finish or capture error event."
[^Scope scope ^EventProcessor event-processor]
(.addEventProcessor scope event-processor))
(defn swap-transaction-status!
"Set trace transaction status."
[^ITransaction transaction status]
(.setStatus transaction status))
(defn finish-transaction!
"Finish trace transaction and send event to Sentry."
[^ITransaction transaction]
(.finish transaction))
(defmacro with-start-child-span
"Start a child span which has the operation or description
and finish after evaluating forms."
[operation description & forms]
`(if-let [sp# (Sentry/getSpan)]
(let [inner-sp# (.startChild sp# ~operation ~description)]
(try
(let [result# (do ~@forms)]
(.setStatus inner-sp# SpanStatus/OK)
result#)
(catch Throwable e#
(.setThrowable inner-sp# e#)
(.setStatus inner-sp# SpanStatus/INTERNAL_ERROR)
(throw e#))
(finally
(.finish inner-sp#))))
(do ~@forms)))