|
25 | 25 | (format-body "Error data" (:data error))
|
26 | 26 | (format-body "Result" result)))
|
27 | 27 |
|
28 |
| -(defn ^:private format-trace [at direction message-type header-details body] |
29 |
| - [:debug |
30 |
| - (str (format-tag at) " " direction " " message-type " " header-details "\n" |
31 |
| - body "\n\n\n")]) |
32 |
| - |
33 | 28 | (defn ^:private latency [^java.time.Instant started ^java.time.Instant finished]
|
34 | 29 | (format "%sms" (- (.toEpochMilli finished) (.toEpochMilli started))))
|
35 | 30 |
|
36 |
| -(defn ^:private format-notification [direction notif at] |
37 |
| - (format-trace at direction "notification" (format-notification-signature notif) |
38 |
| - (format-params notif))) |
39 |
| - |
40 |
| -(defn ^:private format-request [direction req at] |
41 |
| - (format-trace at direction "request" (format-request-signature req) |
42 |
| - (format-params req))) |
43 |
| - |
44 |
| -(defn ^:private format-response [direction req {:keys [error] :as resp} started finished] |
45 |
| - (format-trace finished direction "response" |
46 |
| - (format |
47 |
| - (str "%s. Request took %s." (when error " Request failed: %s (%s).")) |
48 |
| - (format-request-signature req) |
49 |
| - (latency started finished) |
50 |
| - (:message error) (:code error)) |
51 |
| - (format-response-body resp))) |
52 |
| - |
53 |
| -(defn received-notification [notif at] (format-notification "Received" notif at)) |
54 |
| -(defn received-request [req at] (format-request "Received" req at)) |
55 |
| -(defn received-response [req resp started finished] (format-response "Received" req resp started finished)) |
56 |
| - |
57 |
| -(defn received-unmatched-response [resp at] |
58 |
| - (format-trace at "Received" "response" "for unmatched request:" |
59 |
| - (format-body "Body" resp))) |
60 |
| - |
61 |
| -(defn received-unmatched-cancellation-notification [notif at] |
62 |
| - (format-trace at "Received" "cancellation notification" (format "for unmatched request (%s):" (:id (:params notif))) |
63 |
| - (format-params notif))) |
64 |
| - |
65 |
| -(defn sending-notification [notif at] (format-notification "Sending" notif at)) |
66 |
| -(defn sending-request [req at] (format-request "Sending" req at)) |
67 |
| -(defn sending-response [req resp started finished] (format-response "Sending" req resp started finished)) |
| 31 | +(defn ^:private format-response-header-details [req {:keys [error]} started finished] |
| 32 | + (format |
| 33 | + (str "%s. Request took %s." (when error " Request failed: %s (%s).")) |
| 34 | + (format-request-signature req) |
| 35 | + (latency started finished) |
| 36 | + (:message error) (:code error))) |
| 37 | + |
| 38 | +(defn ^:private basic-trace [at direction message-type header-details] |
| 39 | + (str (format-tag at) " " direction " " message-type " " header-details)) |
| 40 | + |
| 41 | +(defn ^:private verbose-trace [header body] |
| 42 | + (str header "\n" body "\n\n\n")) |
| 43 | + |
| 44 | +(defn ^:private basic-notification [direction notif at] |
| 45 | + (basic-trace at direction "notification" (format-notification-signature notif))) |
| 46 | + |
| 47 | +(defn ^:private basic-request [direction req at] |
| 48 | + (basic-trace at direction "request" (format-request-signature req))) |
| 49 | + |
| 50 | +(defn ^:private basic-response [direction req resp started finished] |
| 51 | + (basic-trace finished direction "response" (format-response-header-details req resp started finished))) |
| 52 | + |
| 53 | +(defn ^:private basic-received-unmatched-response [at] |
| 54 | + (basic-trace at "Received" "response" "for unmatched request")) |
| 55 | + |
| 56 | +(defn ^:private basic-received-unmatched-cancellation [at notif] |
| 57 | + (basic-trace at "Received" "cancellation notification" (format "for unmatched request (%s):" (:id (:params notif))))) |
| 58 | + |
| 59 | +(defn ^:private verbose-notification [direction notif at] |
| 60 | + (verbose-trace (basic-notification direction notif at) |
| 61 | + (format-params notif))) |
| 62 | + |
| 63 | +(defn ^:private verbose-request [direction req at] |
| 64 | + (verbose-trace (basic-request direction req at) |
| 65 | + (format-params req))) |
| 66 | + |
| 67 | +(defn ^:private verbose-response [direction req resp started finished] |
| 68 | + (verbose-trace (basic-response direction req resp started finished) |
| 69 | + (format-response-body resp))) |
| 70 | + |
| 71 | +(defprotocol ITracer |
| 72 | + (received-notification [this notif at]) |
| 73 | + (received-request [this req at]) |
| 74 | + (received-response [this req resp started finished]) |
| 75 | + (received-unmatched-response [this resp at]) |
| 76 | + (received-unmatched-cancellation-notification [this notif at]) |
| 77 | + (sending-notification [this notif at]) |
| 78 | + (sending-request [this req at]) |
| 79 | + (sending-response [this req resp started finished])) |
| 80 | + |
| 81 | +(defrecord VerboseTracer [] |
| 82 | + ITracer |
| 83 | + (received-notification [_this notif at] |
| 84 | + (verbose-notification "Received" notif at)) |
| 85 | + (received-request [_this req at] |
| 86 | + (verbose-request "Received" req at)) |
| 87 | + (received-response [_this req resp started finished] |
| 88 | + (verbose-response "Received" req resp started finished)) |
| 89 | + (received-unmatched-response [_this resp at] |
| 90 | + (verbose-trace (basic-received-unmatched-response at) (format-body "Body" resp))) |
| 91 | + (received-unmatched-cancellation-notification [_this notif at] |
| 92 | + (verbose-trace (basic-received-unmatched-cancellation at notif) (format-params notif))) |
| 93 | + (sending-notification [_this notif at] |
| 94 | + (verbose-notification "Sending" notif at)) |
| 95 | + (sending-request [_this req at] |
| 96 | + (verbose-request "Sending" req at)) |
| 97 | + (sending-response [_this req resp started finished] |
| 98 | + (verbose-response "Sending" req resp started finished))) |
| 99 | + |
| 100 | +(defrecord MessagesTracer [] |
| 101 | + ITracer |
| 102 | + (received-notification [_this notif at] |
| 103 | + (basic-notification "Received" notif at)) |
| 104 | + (received-request [_this req at] |
| 105 | + (basic-request "Received" req at)) |
| 106 | + (received-response [_this req resp started finished] |
| 107 | + (basic-response "Received" req resp started finished)) |
| 108 | + (received-unmatched-response [_this _resp at] |
| 109 | + (basic-received-unmatched-response at)) |
| 110 | + (received-unmatched-cancellation-notification [_this notif at] |
| 111 | + (basic-received-unmatched-cancellation at notif)) |
| 112 | + (sending-notification [_this notif at] |
| 113 | + (basic-notification "Sending" notif at)) |
| 114 | + (sending-request [_this req at] |
| 115 | + (basic-request "Sending" req at)) |
| 116 | + (sending-response [_this req resp started finished] |
| 117 | + (basic-response "Sending" req resp started finished))) |
| 118 | + |
| 119 | +(defrecord SilentTracer [] |
| 120 | + ITracer |
| 121 | + (received-notification [_this _notif _at]) |
| 122 | + (received-request [_this _req _at]) |
| 123 | + (received-response [_this _req _resp _started _finished]) |
| 124 | + (received-unmatched-response [_this _resp _at]) |
| 125 | + (received-unmatched-cancellation-notification [_this _notif _at]) |
| 126 | + (sending-notification [_this _notif _at]) |
| 127 | + (sending-request [_this _req _at]) |
| 128 | + (sending-response [_this _req _resp _started _finished])) |
| 129 | + |
| 130 | +(defn tracer-for-level [trace-level] |
| 131 | + (case trace-level |
| 132 | + "verbose" (VerboseTracer.) |
| 133 | + "messages" (MessagesTracer.) |
| 134 | + (SilentTracer.))) |
0 commit comments