|
1 | 1 | (ns com.moclojer.components.logs |
2 | 2 | (:require |
| 3 | + [clojure.core.async :as async] |
3 | 4 | [clojure.data.json :as json] |
4 | 5 | [clj-http.client :as http-client] |
5 | | - [taoensso.telemere :as t] |
6 | | - [taoensso.telemere.timbre :as timbre]) |
7 | | - (:import (java.util.concurrent TimeoutException TimeUnit))) |
| 6 | + [taoensso.telemere :as t]) |
| 7 | + (:import [clojure.core.async.impl.channels ManyToManyChannel])) |
8 | 8 |
|
9 | 9 | (defn build-opensearch-base-req |
10 | 10 | [config] |
11 | 11 | (let [{:keys [username password host port]} config |
12 | 12 | url (str "https://" host ":" port "/_bulk")] |
13 | 13 | {:method :post |
14 | 14 | :url url |
15 | | - :async? true |
16 | 15 | :basic-auth [username password] |
17 | 16 | :content-type :json |
18 | 17 | :body (json/write-str {:index {:_index "logs"}})})) |
|
27 | 26 | :else (pr-str v)))) |
28 | 27 | {} m)) |
29 | 28 |
|
30 | | -(defn send-opensearch-signal-req |
31 | | - [base-req signal] |
32 | | - (let [log (-> (select-keys signal [:level :ctx :data |
33 | | - :msg_ :error :thread |
34 | | - :uid :inst]) |
35 | | - (->str-values) |
36 | | - (json/write-str)) |
37 | | - future (http-client/request |
38 | | - (update |
39 | | - base-req :body |
40 | | - str \newline log \newline) |
41 | | - identity #(throw ^Exception %))] |
42 | | - (try |
43 | | - (.get future 1 TimeUnit/SECONDS) |
44 | | - (catch TimeoutException _ |
45 | | - (.cancel future true))))) |
| 29 | +(defn send-opensearch-log-req |
| 30 | + [base-req log] |
| 31 | + (http-client/request |
| 32 | + (update |
| 33 | + base-req :body |
| 34 | + str \newline (json/write-str (->str-values log)) \newline) |
| 35 | + identity #(throw ^Exception %))) |
| 36 | + |
| 37 | +(defonce log-ch (atom (async/chan))) |
46 | 38 |
|
47 | 39 | (defn setup [config level env] |
48 | | - (let [prod? (= env "prod") |
49 | | - os-cfg (when prod? (:opensearch config)) |
50 | | - os-base-req (build-opensearch-base-req os-cfg)] |
| 40 | + (let [prod? (= env :prod) |
| 41 | + log-ch' (swap! |
| 42 | + log-ch |
| 43 | + (fn [ch] |
| 44 | + (when ch (async/close! ch)) |
| 45 | + (async/chan)))] |
| 46 | + |
51 | 47 | (t/set-min-level! level) |
52 | | - (t/set-ns-filter! {:disallow "*" :allow "com.moclojer.*"}) |
53 | | - (when prod? |
54 | | - (telemere/add-signal! |
55 | | - :opensearch |
56 | | - (fn [signal]))))) |
| 48 | + |
| 49 | + (when (and prod? (instance? ManyToManyChannel log-ch')) |
| 50 | + (let [os-cfg (when prod? (:opensearch config)) |
| 51 | + os-base-req (build-opensearch-base-req os-cfg)] |
| 52 | + |
| 53 | + (t/set-ns-filter! {:disallow #{"*jetty*" "*hikari*" |
| 54 | + "*pedestal*" "*migratus*"}}) |
| 55 | + |
| 56 | + (t/add-handler! |
| 57 | + :opensearch |
| 58 | + (fn [signal] |
| 59 | + (async/go |
| 60 | + (async/>! |
| 61 | + log-ch' |
| 62 | + (select-keys signal [:level :ctx :data :msg_ :error |
| 63 | + :thread :uid :inst]))))) |
| 64 | + |
| 65 | + (async/go |
| 66 | + (while true |
| 67 | + (let [[log _] (async/alts! [log-ch'])] |
| 68 | + (send-opensearch-log-req os-base-req log)))))))) |
57 | 69 |
|
58 | 70 | (comment |
59 | 71 | (def my-signal |
|
70 | 82 | :host "foobar" |
71 | 83 | :port 25060})) |
72 | 84 |
|
73 | | - (send-opensearch-signal-req base-req my-signal) |
74 | | - |
75 | 85 | (http-client/request |
76 | 86 | (update |
77 | 87 | base-req :body |
|
82 | 92 | ;; |
83 | 93 | ) |
84 | 94 |
|
85 | | -(defmacro log [level & args] |
86 | | - `(timbre/log ~level ~@args)) |
| 95 | +(defn log [level msg & [:as data]] |
| 96 | + (t/log! {:level level |
| 97 | + :data (first data)} |
| 98 | + (str msg))) |
| 99 | + |
| 100 | +(comment |
| 101 | + ;; DEPRECATED |
| 102 | + (defmacro log [level & args] |
| 103 | + `(timbre/log ~level ~@args)) |
| 104 | + ;; |
| 105 | + ) |
87 | 106 |
|
88 | 107 | (defn gen-ctx-with-cid [] |
89 | 108 | {:cid (str "cid-" (random-uuid) "-" (System/currentTimeMillis))}) |
90 | 109 |
|
91 | 110 | (comment |
92 | | - (setup :dev) |
| 111 | + log-ch |
| 112 | + |
| 113 | + (setup {:opensearch |
| 114 | + {:username "foobar" |
| 115 | + :password "foobar" |
| 116 | + :host "foobar" |
| 117 | + :port 25060}} |
| 118 | + :info :prod) |
93 | 119 |
|
94 | | - (log :info :world) |
| 120 | + (log :info "something happened" {:user "j0suetm"}) |
95 | 121 | ;; |
96 | 122 | ) |
0 commit comments