Skip to content

Commit 9c577fd

Browse files
committed
fix: handle opensearch error when sending log
1 parent 0f4aec0 commit 9c577fd

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/com/moclojer/components/logs.clj

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
[clojure.core.async :as async]
44
[clojure.data.json :as json]
55
[clj-http.client :as http-client]
6+
[com.moclojer.components.sentry :as sentry]
67
[taoensso.telemere :as t])
78
(:import [clojure.core.async.impl.channels ManyToManyChannel]))
89

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+
915
(defn ->str-values
1016
"Adapts all values (including nested maps' values) from given
1117
map `m` to a string.
@@ -45,8 +51,11 @@
4551

4652
(defn send-opensearch-log-req
4753
[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))))
5059

5160
(defonce log-ch (atom nil))
5261

@@ -55,16 +64,15 @@
5564
level. On `prod` env however, an async channel waits for log events,
5665
which are then sent to OpenSearch."
5766
[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))
6472

6573
(t/set-min-level! level)
6674

67-
(when (and prod? (instance? ManyToManyChannel log-ch'))
75+
(when (and prod? (instance? ManyToManyChannel @log-ch))
6876
(let [os-cfg (when prod? (:opensearch config))
6977
os-base-req (build-opensearch-base-req os-cfg index)]
7078

@@ -75,11 +83,11 @@
7583
:opensearch
7684
(fn [signal]
7785
(async/go
78-
(async/>! log-ch' (signal->opensearch-log signal)))))
86+
(async/>! @log-ch (signal->opensearch-log signal)))))
7987

8088
(async/go
8189
(while true
82-
(let [[log _] (async/alts! [log-ch'])]
90+
(let [[log _] (async/alts! [@log-ch])]
8391
(send-opensearch-log-req os-base-req log))))))))
8492

8593
(defn log [level msg & [:as data]]

src/com/moclojer/components/router.clj

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(ns com.moclojer.components.router
22
(:require
33
[com.moclojer.components.logs :as logs]
4-
[com.moclojer.components.sentry :as sentry]
54
[com.stuartsierra.component :as component]
65
[muuntaja.core :as m]
76
[reitit.coercion.malli :as reitit.malli]
@@ -17,17 +16,12 @@
1716
[reitit.swagger :as swagger]
1817
[reitit.swagger-ui :as swagger-ui]))
1918

20-
(defn send-sentry-evt-from-req! [req ex]
21-
(if-let [sentry-cmp (get-in req [:components :sentry])]
22-
(sentry/send-event! sentry-cmp {:throwable ex})
23-
(logs/log :error "failed to send sentry event (nil component)")))
24-
2519
(defn- coercion-error-handler [status]
2620
(fn [exception request]
2721
(logs/log :error "failed to coerce req/resp"
2822
:ctx {:ex-message (.getMessage exception)
2923
:coercion (:errors (ex-data exception))})
30-
(send-sentry-evt-from-req! request exception)
24+
(logs/send-sentry-evt-from-req! request exception)
3125
{:status status
3226
:body (if (= 400 status)
3327
(str "Invalid path or request parameters, with the following errors: "
@@ -37,7 +31,7 @@
3731
(defn- exception-info-handler [exception request]
3832
(logs/log :error "server exception"
3933
:ctx {:ex-message (.getMessage exception)})
40-
(send-sentry-evt-from-req! request exception)
34+
(logs/send-sentry-evt-from-req! request exception)
4135
{:status 500
4236
:body "Internal error."})
4337

0 commit comments

Comments
 (0)