|
6 | 6 | [taoensso.telemere :as t]) |
7 | 7 | (:import [clojure.core.async.impl.channels ManyToManyChannel])) |
8 | 8 |
|
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 | | - |
19 | 9 | (defn ->str-values |
20 | 10 | [m] |
21 | 11 | (reduce-kv |
|
26 | 16 | :else (pr-str v)))) |
27 | 17 | {} m)) |
28 | 18 |
|
| 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 | + |
29 | 41 | (defn send-opensearch-log-req |
30 | 42 | [base-req log] |
31 | 43 | (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))) |
36 | 45 |
|
37 | | -(defonce log-ch (atom (async/chan))) |
| 46 | +(defonce log-ch (atom nil)) |
38 | 47 |
|
39 | | -(defn setup [config level env] |
| 48 | +(defn setup [config level env & [index]] |
40 | 49 | (let [prod? (= env :prod) |
41 | 50 | log-ch' (swap! |
42 | 51 | log-ch |
|
48 | 57 |
|
49 | 58 | (when (and prod? (instance? ManyToManyChannel log-ch')) |
50 | 59 | (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)] |
52 | 61 |
|
53 | 62 | (t/set-ns-filter! {:disallow #{"*jetty*" "*hikari*" |
54 | 63 | "*pedestal*" "*migratus*"}}) |
|
57 | 66 | :opensearch |
58 | 67 | (fn [signal] |
59 | 68 | (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))))) |
64 | 70 |
|
65 | 71 | (async/go |
66 | 72 | (while true |
67 | 73 | (let [[log _] (async/alts! [log-ch'])] |
68 | 74 | (send-opensearch-log-req os-base-req log)))))))) |
69 | 75 |
|
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 | | - |
95 | 76 | (defn log [level msg & [:as data]] |
96 | 77 | (t/log! {:level level |
97 | 78 | :data (first data)} |
98 | 79 | (str msg))) |
99 | 80 |
|
100 | | -(comment |
101 | | - ;; DEPRECATED |
102 | | - (defmacro log [level & args] |
103 | | - `(timbre/log ~level ~@args)) |
104 | | - ;; |
105 | | - ) |
106 | | - |
107 | 81 | (defn gen-ctx-with-cid [] |
108 | 82 | {:cid (str "cid-" (random-uuid) "-" (System/currentTimeMillis))}) |
109 | 83 |
|
110 | 84 | (comment |
111 | | - log-ch |
112 | | - |
| 85 | + @log-ch |
| 86 | + |
113 | 87 | (setup {:opensearch |
114 | 88 | {:username "foobar" |
115 | 89 | :password "foobar" |
116 | 90 | :host "foobar" |
117 | 91 | :port 25060}} |
118 | | - :info :prod) |
| 92 | + :info :prod "moclojer-api-logs") |
119 | 93 |
|
120 | | - (log :info "something happened" {:user "j0suetm"}) |
| 94 | + (log :error "something happened" {:user "j0suetm"}) |
121 | 95 | ;; |
122 | 96 | ) |
0 commit comments