taoensso.telemere.utils
Misc utils useful for Telemere handlers, middleware, etc.
error-signal?
clj
(error-signal? signal)
Experimental, subject to change.
Returns true iff given signal has an `:error` value, or a `:kind` or `:level`
that indicates that it's an error.
file-writer
clj
(file-writer {:keys [file append?], :or {append? true}})
Experimental, subject to change.
Opens the specified file and returns a stateful fn of 2 arities:
[content] => Writes given content to file, or noops if closed.
[] => Closes the writer.
Useful for basic handlers that write to a file, etc.
Notes:
- Automatically creates file and parent dirs as necessary.
- Writer should be manually closed after use (with zero-arity call).
- Flushes after every write.
- Thread safe, locks on single file stream.
hex-uid-fn
clj
(hex-uid-fn)(hex-uid-fn {:keys [secure? root-len child-len], :or {root-len 32, child-len 16}})
Experimental, subject to change.
Returns a (fn hex-uid [root?]) that returns a random hex-style uid string like:
"05039666eb9dc3206475f44ab9f3d843" - 128 bit (32 char) root uid
"721fcef639a51513" - 64 bit (16 char) non-root (child) uid
host-info
clj
added in Encore v3.115.0 (2024-08-18)
(host-info)(host-info cache-msecs timeout-msecs fallback-val)(host-info fallback-val)(host-info timeout-msecs fallback-val)
Returns ?{:keys [ip name]} with string vals or `fallback-val` (default nil).
Arities 0 and 3 are cached, prefer these!
Arities 1 and 2 are uncached and intended for advanced users only.host-ip
clj
added in Encore v3.115.0 (2024-08-18)
(host-ip)(host-ip cache-msecs timeout-msecs fallback-val)(host-ip fallback-val)(host-ip timeout-msecs fallback-val)
Returns local host IP string or `fallback-val` (default nil).
Arities 0 and 3 are cached, prefer these!
Arities 1 and 2 are uncached and intended for advanced users only.
hostname
clj
added in Encore v3.115.0 (2024-08-18)
(hostname)(hostname cache-msecs timeout-msecs fallback-val)(hostname fallback-val)(hostname timeout-msecs fallback-val)
Returns local hostname string or `fallback-val` (default nil).
Arities 0 and 3 are cached, prefer these!
Arities 1 and 2 are uncached and intended for advanced users only.
nano-uid-fn
clj
(nano-uid-fn)(nano-uid-fn {:keys [secure? root-len child-len], :or {root-len 21, child-len 10}})
Experimental, subject to change.
Returns a (fn nano-uid [root?]) that returns a random nano-style uid string like:
"r76-B8LoIPs5lBG1_Uhdy" - 126 bit (21 char) root uid
"tMEYoZH0K-" - 60 bit (10 char) non-root (child) uid
newline
clj
added in Encore v3.68.0 (2023-09-25)
pr-edn
clj
(pr-edn x)
Prints given arg to an edn string readable with `read-edn`.
pr-signal-fn
clj
(pr-signal-fn)(pr-signal-fn {:keys [pr-fn incl-kvs? incl-nils? incl-newline? incl-keys], :as opts, :or {pr-fn :edn, incl-newline? true}})
Experimental, subject to change.
Returns a (fn pr [signal]) that:
- Takes a Telemere signal (map).
- Returns a machine-readable signal ?string.
Options:
`:pr-fn` - ∈ #{<unary-fn> :edn (default) :json (Cljs only)}
`:incl-kvs?` - Include signal's app-level kvs? (default false)
`:incl-nils?` - Include signal's keys with nil values? (default false)
`:incl-newline?` - Include terminating system newline? (default true)
`:incl-keys` - Subset of signal keys to retain from those otherwise
excluded by default: #{:location :kvs :file :host :thread}
Examples:
(pr-signal-fn {:pr-fn :edn ...}) ; Outputs edn
(pr-signal-fn {:pr-fn :json ...}) ; Outputs JSON (Cljs only)
To output JSON for Clj, you must provide an appropriate `:pr-fn`.
`jsonista` offers one good option, Ref. <https://github.com/metosin/jsonista>:
(require '[jsonista.core :as jsonista])
(pr-signal-fn {:pr-fn jsonista/write-value-as-string ...})
Motivation:
Why use this util instead of just directly using the print function
given to `:pr-fn`? Signals are optimized for cheap creation and easy handling,
so may contain things like nil values and duplicated content.
This util efficiently clean signals of such noise, helping reduce
storage/transmission size, and making key info easier to see.
See also `format-signal-fn` for human-readable output.signal-content-fn
clj
(signal-content-fn)(signal-content-fn {:keys [raw-error? incl-keys format-nsecs-fn format-error-fn], :or {format-nsecs-fn (format-nsecs-fn), format-error-fn (format-error-fn)}})
Experimental, subject to change.
Returns a (fn content [signal]) that:
- Takes a Telemere signal (map).
- Returns a signal content ?string (incl. data, ctx, etc.).
Options:
`:raw-error?` - Retain unformatted error? (default false)
`:incl-keys` - Subset of signal keys to retain from those
otherwise excluded by default: #{:kvs :host :thread}
`:format-nsecs-fn` - (fn [nanosecs]) => string.
`:format-error-fn` - (fn [error]) => string.signal-preamble-fn
clj
(signal-preamble-fn)(signal-preamble-fn {:keys [format-inst-fn], :or {format-inst-fn (format-inst-fn)}})
Experimental, subject to change.
Returns a (fn preamble [signal]) that:
- Takes a Telemere signal (map).
- Returns a signal preamble ?string like:
"2024-03-26T11:14:51.806Z INFO EVENT Hostname taoensso.telemere(2,21) ::ev-id - msg"
Options:
`:format-inst-fn` - (fn format [instant]) => string.tcp-socket-writer
clj
(tcp-socket-writer {:keys [host port ssl? connect-timeout-msecs socket-fn ssl-socket-fn], :as opts, :or {connect-timeout-msecs 3000, socket-fn default-socket-fn, ssl-socket-fn default-ssl-socket-fn}})
Experimental, subject to change.
Connects to specified TCP socket and returns a stateful fn of 2 arities:
[content] => Writes given content to socket, or noops if closed.
[] => Closes the writer.
Useful for basic handlers that write to a TCP socket, etc.
Options:
`:ssl?` - Use SSL/TLS?
`:connect-timeout-msecs` - Connection timeout (default 3000 msecs)
`:socket-fn` - (fn [host port timeout]) => `java.net.Socket`
`:ssl-socket-fn` - (fn [socket host port]) => `java.net.Socket`
Notes:
- Writer should be manually closed after use (with zero-arity call).
- Flushes after every write.
- Will retry failed writes once, then drop.
- Thread safe, locks on single socket stream.
- Advanced users may want a custom implementation using a connection
pool and/or more sophisticated retry semantics, etc.thread-id
clj
added in Encore v3.115.0 (2024-08-18)
(thread-id)
Returns long id of current `Thread`.
thread-info
clj
added in Encore v3.115.0 (2024-08-18)
(thread-info)
Returns {:keys [group name id]} for current `Thread`.
thread-name
clj
added in Encore v3.115.0 (2024-08-18)
(thread-name)
Returns string name of current `Thread`.
uuid
clj
added in Encore v3.75.0 (2024-01-29)
(uuid)
For Clj: returns a random `java.util.UUID`.
For Cljs: returns a random UUID string.
Uses strong randomness when possible.
See also `uuid-str`, `nanoid`, `rand-id-fn`.
uuid-str
clj
(uuid-str max-len)(uuid-str)
Returns a random UUID string of given length (max 36).
Uses strong randomness when possible. See also `uuid`, `nanoid`, `rand-id-fn`.