Skip to content

Commit a1941b3

Browse files
committed
feat: index selec. on setup and signal pre-processing before req.
1 parent 4071352 commit a1941b3

File tree

1 file changed

+31
-57
lines changed

1 file changed

+31
-57
lines changed

src/com/moclojer/components/logs.clj

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66
[taoensso.telemere :as t])
77
(:import [clojure.core.async.impl.channels ManyToManyChannel]))
88

9-
(defn build-opensearch-base-req
10-
[config]
11-
(let [{:keys [username password host port]} config
12-
url (str "https://" host ":" port "/_bulk")]
13-
{:method :post
14-
:url url
15-
:basic-auth [username password]
16-
:content-type :json
17-
:body (json/write-str {:index {:_index "logs"}})}))
18-
199
(defn ->str-values
2010
[m]
2111
(reduce-kv
@@ -26,17 +16,36 @@
2616
:else (pr-str v))))
2717
{} m))
2818

19+
(defn signal->opensearch-log
20+
[{:keys [thread location] :as signal}]
21+
(-> (select-keys signal [:level :ctx :data :msg_ :error :uid :inst])
22+
(merge {"thread/group" (:group thread)
23+
"thread/name" (:name thread)
24+
"thread/id" (:id thread)
25+
:location (str (:ns location) ":"
26+
(:line location) "x"
27+
(:column location))})
28+
(->str-values)
29+
(json/write-str)))
30+
31+
(defn build-opensearch-base-req
32+
[config index]
33+
(let [{:keys [username password host port]} config
34+
url (str "https://" host ":" port "/_bulk")]
35+
{:method :post
36+
:url url
37+
:basic-auth [username password]
38+
:content-type :json
39+
:body (json/write-str {:index {:_index index}})}))
40+
2941
(defn send-opensearch-log-req
3042
[base-req log]
3143
(http-client/request
32-
(update
33-
base-req :body
34-
str \newline (json/write-str (->str-values log)) \newline)
35-
identity #(throw ^Exception %)))
44+
(update base-req :body str \newline log \newline)))
3645

37-
(defonce log-ch (atom (async/chan)))
46+
(defonce log-ch (atom nil))
3847

39-
(defn setup [config level env]
48+
(defn setup [config level env & [index]]
4049
(let [prod? (= env :prod)
4150
log-ch' (swap!
4251
log-ch
@@ -48,7 +57,7 @@
4857

4958
(when (and prod? (instance? ManyToManyChannel log-ch'))
5059
(let [os-cfg (when prod? (:opensearch config))
51-
os-base-req (build-opensearch-base-req os-cfg)]
60+
os-base-req (build-opensearch-base-req os-cfg index)]
5261

5362
(t/set-ns-filter! {:disallow #{"*jetty*" "*hikari*"
5463
"*pedestal*" "*migratus*"}})
@@ -57,66 +66,31 @@
5766
:opensearch
5867
(fn [signal]
5968
(async/go
60-
(async/>!
61-
log-ch'
62-
(select-keys signal [:level :ctx :data :msg_ :error
63-
:thread :uid :inst])))))
69+
(async/>! log-ch' (signal->opensearch-log signal)))))
6470

6571
(async/go
6672
(while true
6773
(let [[log _] (async/alts! [log-ch'])]
6874
(send-opensearch-log-req os-base-req log))))))))
6975

70-
(comment
71-
(def my-signal
72-
(t/with-signal
73-
(t/log! {:level :info
74-
:data {:hello true
75-
:time "hello world"}}
76-
"hello")))
77-
78-
(def base-req
79-
(build-opensearch-base-req
80-
{:username "foobar"
81-
:password "foobar"
82-
:host "foobar"
83-
:port 25060}))
84-
85-
(http-client/request
86-
(update
87-
base-req :body
88-
str
89-
\newline
90-
(json/write-str my-signal)
91-
\newline))
92-
;;
93-
)
94-
9576
(defn log [level msg & [:as data]]
9677
(t/log! {:level level
9778
:data (first data)}
9879
(str msg)))
9980

100-
(comment
101-
;; DEPRECATED
102-
(defmacro log [level & args]
103-
`(timbre/log ~level ~@args))
104-
;;
105-
)
106-
10781
(defn gen-ctx-with-cid []
10882
{:cid (str "cid-" (random-uuid) "-" (System/currentTimeMillis))})
10983

11084
(comment
111-
log-ch
112-
85+
@log-ch
86+
11387
(setup {:opensearch
11488
{:username "foobar"
11589
:password "foobar"
11690
:host "foobar"
11791
:port 25060}}
118-
:info :prod)
92+
:info :prod "moclojer-api-logs")
11993

120-
(log :info "something happened" {:user "j0suetm"})
94+
(log :error "something happened" {:user "j0suetm"})
12195
;;
12296
)

0 commit comments

Comments
 (0)