[new] Add host info to signal content

This commit is contained in:
Peter Taoussanis 2024-08-13 12:22:06 +02:00
parent d635318f73
commit 1cef195715
4 changed files with 46 additions and 69 deletions

View file

@ -22,12 +22,16 @@ Default signal keys:
`:ctx` --------- ?val of `*ctx*` (arb app-level state) when signal was created
`:parent` ------ ?{:keys [id uid]} of parent signal, present in nested signals when tracing
`:root` -------- ?{:keys [id uid]} of root signal, present in nested signals when tracing
`:location` ---- ?{:keys [ns file line column]} signal creator callsite
`:ns` ---------- ?str namespace of signal creator callsite, same as (:ns location)
`:line` -------- ?int line of signal creator callsite, same as (:line location)
`:column` ------ ?int column of signal creator callsite, same as (:column location)
`:file` -------- ?str filename of signal creator callsite, same as (:file location)
`:thread` ------ (Clj only) {:keys [group name id]} thread info for thread that called signal creator
`:host` -------- (Clj only) {:keys [name ip]} info for network host
`:thread` ------ (Clj only) {:keys [name id group]} info for thread that created signal
`:sample-rate` - ?rate ∈ℝ[0,1] for combined signal AND handler sampling (0.75 => allow 75% of signals, nil => allow all)
<kvs> ---------- Other arb app-level ?kvs given to signal creator. Typically NOT included

View file

@ -196,7 +196,7 @@
(defrecord Signal
;; Telemere's main public data type, we avoid nesting and duplication
[^long schema inst uid,
location ns line column file thread,
location ns line column file, #?@(:clj [host thread]),
sample-rate, kind id level, ctx parent root, data kvs msg_,
error run-form run-val end-inst run-nsecs]
@ -471,17 +471,6 @@
error))
value)))
#?(:clj
(defn thread-info
"Returns {:keys [group name id]} for current thread."
[]
(when-let [t (Thread/currentThread)]
{:group (when-let [g (.getThreadGroup t)] (.getName g))
:name (.getName t)
:id (.getId t)})))
(comment (enc/qb 1e6 (thread-info))) ; 44.49
(defn inst+nsecs
"Returns given platform instant plus given number of nanosecs."
[inst run-nsecs]
@ -540,7 +529,7 @@
uid-form (get opts :uid (when trace? :auto/uuid))
uid-form (parse-uid-form uid-form)
thread-form (if clj? `(thread-info) nil)
thread-form (if clj? `(enc/thread-info) nil)
signal-delay-form
(let [{do-form :do
@ -579,36 +568,35 @@
(ex-info "Signals cannot have `:msg_` opt (did you mean `:msg`?))"
{:msg_ (enc/typed-val (val e))}))))
record-form
(if run-form
`(let [^RunResult run-result# ~'__run-result
run-nsecs# (.-run-nsecs run-result#)
run-val# (.-value run-result#)
run-err# (.-error run-result#)
end-inst# (inst+nsecs ~'__inst run-nsecs#)
msg_#
(let [mf# ~msg-form]
(if (fn? mf#) ; Undocumented, handy for `trace!`/`spy!`, etc.
(delay (mf# '~run-form run-val# run-err# run-nsecs#))
mf#))]
(Signal. 1 ~'__inst ~'__uid,
~location ~'__ns ~line-form ~column-form ~file-form ~'__thread,
~sample-rate-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root,
~data-form ~kvs-form msg_#,
run-err# '~run-form run-val# end-inst# run-nsecs#))
`(Signal. 1 ~'__inst ~'__uid,
~location ~'__ns ~line-form ~column-form ~file-form ~'__thread,
~sample-rate-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root,
~data-form ~kvs-form ~msg-form,
~error-form nil nil nil nil))
signal-form
(if-not kvs-form
record-form
`(let [signal# ~record-form]
(reduce-kv assoc signal# (.-kvs signal#))))]
(let [record-form
(let [clause [(if run-form :run :no-run) (if clj? :clj :cljs)]]
(case clause
[:run :clj ] `(Signal. 1 ~'__inst ~'__uid, ~location ~'__ns ~line-form ~column-form ~file-form, (enc/host-info) ~'__thread, ~sample-rate-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root, ~data-form ~kvs-form ~'_msg_, ~'_run-err '~run-form ~'_run-val ~'_end-inst ~'_run-nsecs)
[:run :cljs] `(Signal. 1 ~'__inst ~'__uid, ~location ~'__ns ~line-form ~column-form ~file-form, ~sample-rate-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root, ~data-form ~kvs-form ~'_msg_, ~'_run-err '~run-form ~'_run-val ~'_end-inst ~'_run-nsecs)
[:no-run :clj ] `(Signal. 1 ~'__inst ~'__uid, ~location ~'__ns ~line-form ~column-form ~file-form, (enc/host-info) ~'__thread, ~sample-rate-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root, ~data-form ~kvs-form ~msg-form, ~error-form nil nil nil nil)
[:no-run :cljs] `(Signal. 1 ~'__inst ~'__uid, ~location ~'__ns ~line-form ~column-form ~file-form, ~sample-rate-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root, ~data-form ~kvs-form ~msg-form, ~error-form nil nil nil nil)
(enc/unexpected-arg! clause {:context :signal-constructor-args})))
record-form
(if-not run-form
record-form
`(let [~(with-meta '_run-result {:tag `RunResult}) ~'__run-result
~'_run-nsecs (.-run-nsecs ~'_run-result)
~'_run-val (.-value ~'_run-result)
~'_run-err (.-error ~'_run-result)
~'_end-inst (inst+nsecs ~'__inst ~'_run-nsecs)
~'_msg_
(let [mf# ~msg-form]
(if (fn? mf#) ; Undocumented, handy for `trace!`/`spy!`, etc.
(delay (mf# '~run-form ~'_run-val ~'_run-err ~'_run-nsecs))
mf#))]
~record-form))]
(if-not kvs-form
record-form
`(let [signal# ~record-form]
(reduce-kv assoc signal# (.-kvs signal#)))))]
`(delay
;; Delay (cache) shared by all handlers. Covers signal `:let` eval, signal construction,

View file

@ -53,26 +53,10 @@
;;;; Public misc
(enc/defaliases enc/newline enc/pr-edn #?(:cljs enc/pr-json) #?(:clj impl/thread-info))
#?(:clj (defn thread-name "Returns string name of current thread." ^String [] (.getName (Thread/currentThread))))
#?(:clj (defn thread-id "Returns long id of current thread." ^long [] (.getId (Thread/currentThread))))
(comment [(thread-name) (thread-id)])
#?(:clj
(defn host-ip
"Returns cached local host IP address string, or `timeout-val` (default \"UnknownHost\")."
( [timeout-msecs timeout-val] (enc/get-host-ip (enc/msecs :mins 1) timeout-msecs timeout-val))
(^String [ ] (enc/get-host-ip (enc/msecs :mins 1) 5000 "UnknownHost"))))
#?(:clj
(defn hostname
"Returns cached local hostname string, or `timeout-val` (default \"UnknownHost\")."
( [timeout-msecs timeout-val] (enc/get-hostname (enc/msecs :mins 1) timeout-msecs timeout-val))
(^String [ ] (enc/get-hostname (enc/msecs :mins 1) 3500 (delay (host-ip 1500 "UnknownHost"))))))
(comment (enc/qb 1e6 (hostname))) ; 56.88
(enc/defaliases
enc/newline enc/pr-edn #?(:cljs enc/pr-json)
#?@(:clj [enc/thread-info enc/thread-id enc/thread-name
enc/host-info enc/host-ip enc/hostname]))
#?(:cljs
(defn js-console-logger
@ -520,12 +504,13 @@
(let [af append-fn
vf val-fn]
(let [{:keys [uid parent root data kvs ctx #?(:clj thread) sample-rate]} signal]
(let [{:keys [uid parent root data kvs ctx #?@(:clj [host thread]) sample-rate]} signal]
(when sample-rate (af " sample: " (vf sample-rate)))
(when uid (af " uid: " (vf uid)))
(when parent (af " parent: " (vf (dissoc parent :inst))))
(when (and parent root) (af " root: " (vf (dissoc root :inst))))
#?(:clj (when (and thread incl-thread?) (af " thread: " (vf thread))))
(when parent (af " parent: " (vf (dissoc parent :inst)))) ; {:keys [id uid]}
(when (and parent root) (af " root: " (vf (dissoc root :inst)))) ; {:keys [id uid]}
#?(:clj (when host (af " host: " (vf host)))) ; {:keys [ name ip]}
#?(:clj (when (and thread incl-thread?) (af " thread: " (vf thread)))) ; {:keys [group name id]}
(when data (af " data: " (vf data)))
(when (and kvs incl-kvs?) (af " kvs: " (vf kvs)))
(when ctx (af " ctx: " (vf ctx))))

View file

@ -828,8 +828,8 @@
(testing "format-signal-fn"
(let [sig (with-sig :raw :trap (tel/event! ::ev-id {:inst t1, :msg ["a" "b"]}))]
(is (enc/str-starts-with? ((tel/format-signal-fn) sig) "2024-01-01T01:01:01.110Z INFO EVENT"))
(is (enc/str-ends-with? ((tel/format-signal-fn) sig) "::ev-id - a b\n"))))])])
[(is (enc/str-starts-with? ((tel/format-signal-fn) sig ) "2024-01-01T01:01:01.110Z INFO EVENT"))
(is (enc/str-ends-with? ((tel/format-signal-fn) (dissoc sig :host)) "::ev-id - a b\n"))]))])])
;;;; File handler