|
3 | 3 | [clojure.core.async :as async] |
4 | 4 | [clojure.data.json :as json] |
5 | 5 | [clj-http.client :as http-client] |
| 6 | + [com.moclojer.components.sentry :as sentry] |
6 | 7 | [taoensso.telemere :as t]) |
7 | 8 | (:import [clojure.core.async.impl.channels ManyToManyChannel])) |
8 | 9 |
|
| 10 | +(defn send-sentry-evt-from-req! [req ex] |
| 11 | + (if-let [sentry-cmp (get-in req [:components :sentry])] |
| 12 | + (sentry/send-event! sentry-cmp {:throwable ex}) |
| 13 | + (prn :error "failed to send sentry event (nil component)"))) |
| 14 | + |
9 | 15 | (defn ->str-values |
10 | 16 | "Adapts all values (including nested maps' values) from given |
11 | 17 | map `m` to a string. |
|
45 | 51 |
|
46 | 52 | (defn send-opensearch-log-req |
47 | 53 | [base-req log] |
48 | | - (http-client/request |
49 | | - (update base-req :body str \newline log \newline))) |
| 54 | + (try |
| 55 | + (http-client/request |
| 56 | + (update base-req :body str \newline log \newline)) |
| 57 | + (catch Exception e |
| 58 | + (send-sentry-evt-from-req! base-req e)))) |
50 | 59 |
|
51 | 60 | (defonce log-ch (atom nil)) |
52 | 61 |
|
|
55 | 64 | level. On `prod` env however, an async channel waits for log events, |
56 | 65 | which are then sent to OpenSearch." |
57 | 66 | [config level env & [index]] |
58 | | - (let [prod? (= env :prod) |
59 | | - log-ch' (swap! |
60 | | - log-ch |
61 | | - (fn [ch] |
62 | | - (when ch (async/close! ch)) |
63 | | - (async/chan)))] |
| 67 | + (let [prod? (= env :prod)] |
| 68 | + |
| 69 | + (when-let [ch @log-ch] |
| 70 | + (async/close! ch)) |
| 71 | + (reset! log-ch (async/chan)) |
64 | 72 |
|
65 | 73 | (t/set-min-level! level) |
66 | 74 |
|
67 | | - (when (and prod? (instance? ManyToManyChannel log-ch')) |
| 75 | + (when (and prod? (instance? ManyToManyChannel @log-ch)) |
68 | 76 | (let [os-cfg (when prod? (:opensearch config)) |
69 | 77 | os-base-req (build-opensearch-base-req os-cfg index)] |
70 | 78 |
|
|
75 | 83 | :opensearch |
76 | 84 | (fn [signal] |
77 | 85 | (async/go |
78 | | - (async/>! log-ch' (signal->opensearch-log signal))))) |
| 86 | + (async/>! @log-ch (signal->opensearch-log signal))))) |
79 | 87 |
|
80 | 88 | (async/go |
81 | 89 | (while true |
82 | | - (let [[log _] (async/alts! [log-ch'])] |
| 90 | + (let [[log _] (async/alts! [@log-ch])] |
83 | 91 | (send-opensearch-log-req os-base-req log)))))))) |
84 | 92 |
|
85 | 93 | (defn log [level msg & [:as data]] |
|
0 commit comments