telemere/taoensso.telemere.utils.html
2024-09-23 09:20:57 +02:00

150 lines
No EOL
21 KiB
HTML

<!DOCTYPE html PUBLIC ""
"">
<html><head><meta charset="UTF-8" /><title>taoensso.telemere.utils documentation</title><link rel="stylesheet" type="text/css" href="css/default.css" /><link rel="stylesheet" type="text/css" href="css/highlight.css" /><script type="text/javascript" src="js/highlight.min.js"></script><script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/page_effects.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body><div id="header"><h2>Generated by <a href="https://github.com/weavejester/codox">Codox</a></h2><h1><a href="index.html"><span class="project-title"><span class="project-name">Telemere</span> <span class="project-version">1.0.0-beta24</span></span></a></h1><div id="langs"><div class="lang current">clj</div><div class="lang"><a href="index.cljs.html">cljs</a></div></div></div><div class="sidebar primary"><h3 class="no-link"><span class="inner">Namespaces</span></h3><ul><li class="depth-1"><div class="no-link"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>taoensso</span></div></div></li><li class="depth-2"><a href="taoensso.telemere.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>telemere</span></div></a></li><li class="depth-3 branch"><a href="taoensso.telemere.open-telemetry.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>open-telemetry</span></div></a></li><li class="depth-3 branch"><a href="taoensso.telemere.postal.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>postal</span></div></a></li><li class="depth-3 branch"><a href="taoensso.telemere.slack.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>slack</span></div></a></li><li class="depth-3 branch"><a href="taoensso.telemere.sockets.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>sockets</span></div></a></li><li class="depth-3 branch"><a href="taoensso.telemere.streams.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>streams</span></div></a></li><li class="depth-3 branch"><a href="taoensso.telemere.timbre.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>timbre</span></div></a></li><li class="depth-3 branch"><a href="taoensso.telemere.tools-logging.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>tools-logging</span></div></a></li><li class="depth-3 current"><a href="taoensso.telemere.utils.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>utils</span></div></a></li></ul></div><div class="sidebar secondary"><h3><a href="#top"><span class="inner">Public Vars</span></a></h3><ul><li class="depth-1"><a href="taoensso.telemere.utils.html#var-clean-signal-fn"><div class="inner"><span>clean-signal-fn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-error-signal.3F"><div class="inner"><span>error-signal?</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-file-writer"><div class="inner"><span>file-writer</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-format-error-fn"><div class="inner"><span>format-error-fn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-format-inst-fn"><div class="inner"><span>format-inst-fn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-format-nsecs-fn"><div class="inner"><span>format-nsecs-fn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-format-signal-fn"><div class="inner"><span>format-signal-fn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-hex-uid-fn"><div class="inner"><span>hex-uid-fn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-host-info"><div class="inner"><span>host-info</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-host-ip"><div class="inner"><span>host-ip</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-hostname"><div class="inner"><span>hostname</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-nano-uid-fn"><div class="inner"><span>nano-uid-fn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-newline"><div class="inner"><span>newline</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-pr-edn"><div class="inner"><span>pr-edn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-pr-signal-fn"><div class="inner"><span>pr-signal-fn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-signal-content-fn"><div class="inner"><span>signal-content-fn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-signal-preamble-fn"><div class="inner"><span>signal-preamble-fn</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-tcp-socket-writer"><div class="inner"><span>tcp-socket-writer</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-thread-id"><div class="inner"><span>thread-id</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-thread-info"><div class="inner"><span>thread-info</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-thread-name"><div class="inner"><span>thread-name</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-uuid"><div class="inner"><span>uuid</span></div></a></li><li class="depth-1"><a href="taoensso.telemere.utils.html#var-uuid-str"><div class="inner"><span>uuid-str</span></div></a></li></ul></div><div class="namespace-docs" id="content"><h1 class="anchor" id="top">taoensso.telemere.utils</h1><div class="doc"><pre class="plaintext">Misc utils useful for Telemere handlers, middleware, etc.
</pre></div><div class="public anchor" id="var-clean-signal-fn"><h3>clean-signal-fn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-clean-signal-fn">cljs</a></h4><div class="usage"><code>(clean-signal-fn)</code><code>(clean-signal-fn {:keys [incl-kvs? incl-nils? incl-keys], :as opts})</code></div><div class="doc"><pre class="plaintext">Experimental, subject to change.
Returns a (fn clean [signal]) that:
- Takes a Telemere signal (map).
- Returns a minimal signal (map) ready for printing, etc.
Signals are optimized for cheap creation and easy handling, so tend to be
verbose and may contain things like nil values and duplicated content.
This util efficiently cleans signals of such noise, helping reduce
storage/transmission size, and making key info easier to see.
Options:
`:incl-nils?` - Include signal's keys with nil values? (default false)
`:incl-kvs?` - Include signal's app-level root kvs? (default false)
`:incl-keys` - Subset of signal keys to retain from those otherwise
excluded by default: #{:location :kvs :file :host :thread}</pre></div></div><div class="public anchor" id="var-error-signal.3F"><h3>error-signal?</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-error-signal.3F">cljs</a></h4><div class="usage"><code>(error-signal? signal)</code></div><div class="doc"><pre class="plaintext">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.</pre></div></div><div class="public anchor" id="var-file-writer"><h3>file-writer</h3><h4 class="lang current">clj</h4><div class="usage"><code>(file-writer {:keys [file append?], :or {append? true}})</code></div><div class="doc"><pre class="plaintext">Experimental, subject to change.
Opens the specified file and returns a stateful fn of 2 arities:
[content] =&gt; Writes given content to file, or noops if closed.
[] =&gt; 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.</pre></div></div><div class="public anchor" id="var-format-error-fn"><h3>format-error-fn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-format-error-fn">cljs</a></h4><div class="usage"><code>(format-error-fn)</code><code>(format-error-fn {:as _opts})</code></div><div class="doc"><pre class="plaintext">Experimental, subject to change.
Returns a (fn format [error]) that:
- Takes a platform error (`Throwable` or `js/Error`).
- Returns a human-readable error string.</pre></div></div><div class="public anchor" id="var-format-inst-fn"><h3>format-inst-fn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-format-inst-fn">cljs</a></h4><h4 class="added">added in Encore v3.98.0 (2024-04-08)</h4><div class="usage"><code>(format-inst-fn)</code><code>(format-inst-fn {:keys [formatter zone], :or {formatter java.time.format.DateTimeFormatter/ISO_INSTANT, zone java.time.ZoneOffset/UTC}})</code></div><div class="doc"><pre class="plaintext">Experimental, subject to change without notice.
Returns a (fn format [instant]) that:
- Takes a platform instant (`java.time.Instant` or `js/Date`).
- Returns a formatted human-readable instant string.
Options:
`:zone` (Clj only) `java.time.ZoneOffset` (defaults to UTC).
`:formatter`
`java.time.format.DateTimeFormatter` (Clj) or
`goog.i18n.DateTimeFormat` (Cljs),
Defaults to `ISO8601` formatter (`YYYY-MM-DDTHH:mm:ss.sssZ`),
e.g.: "2011-12-03T10:15:130Z".</pre></div></div><div class="public anchor" id="var-format-nsecs-fn"><h3>format-nsecs-fn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-format-nsecs-fn">cljs</a></h4><div class="usage"><code>(format-nsecs-fn)</code><code>(format-nsecs-fn {:as _opts})</code></div><div class="doc"><pre class="plaintext">Experimental, subject to change.
Returns a (fn format [nanosecs]) that:
- Takes a long nanoseconds (e.g. runtime).
- Returns a human-readable string like:
"1.00m", "4.20s", "340ms", "822μs", etc.</pre></div></div><div class="public anchor" id="var-format-signal-fn"><h3>format-signal-fn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-format-signal-fn">cljs</a></h4><div class="usage"><code>(format-signal-fn)</code><code>(format-signal-fn {:keys [incl-newline? preamble-fn content-fn], :or {incl-newline? true, preamble-fn (signal-preamble-fn), content-fn (signal-content-fn)}})</code></div><div class="doc"><pre class="plaintext">Experimental, subject to change.
Returns a (fn format [signal]) that:
- Takes a Telemere signal (map).
- Returns a human-readable signal string.
Options:
`:incl-newline?` - Include terminating system newline? (default true)
`:preamble-fn` - (fn [signal]) =&gt; signal preamble string, see [1]
`:content-fn` - (fn [signal]) =&gt; signal content string, see [2]
[1] `taoensso.telemere.utils/signal-preamble-fn`, etc.
[2] `taoensso.telemere.utils/signal-content-fn`, etc.
See also `pr-signal-fn` for an alternative to `format-signal-fn`
that produces machine-readable output (edn, JSON, etc.).</pre></div></div><div class="public anchor" id="var-hex-uid-fn"><h3>hex-uid-fn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-hex-uid-fn">cljs</a></h4><div class="usage"><code>(hex-uid-fn)</code><code>(hex-uid-fn {:keys [secure? root-len child-len], :or {root-len 32, child-len 16}})</code></div><div class="doc"><pre class="plaintext">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</pre></div></div><div class="public anchor" id="var-host-info"><h3>host-info</h3><h4 class="lang current">clj</h4><h4 class="added">added in Encore v3.115.0 (2024-08-18)</h4><div class="usage"><code>(host-info)</code><code>(host-info cache-msecs timeout-msecs fallback-val)</code><code>(host-info fallback-val)</code><code>(host-info timeout-msecs fallback-val)</code></div><div class="doc"><pre class="plaintext">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.</pre></div></div><div class="public anchor" id="var-host-ip"><h3>host-ip</h3><h4 class="lang current">clj</h4><h4 class="added">added in Encore v3.115.0 (2024-08-18)</h4><div class="usage"><code>(host-ip)</code><code>(host-ip cache-msecs timeout-msecs fallback-val)</code><code>(host-ip fallback-val)</code><code>(host-ip timeout-msecs fallback-val)</code></div><div class="doc"><pre class="plaintext">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.</pre></div></div><div class="public anchor" id="var-hostname"><h3>hostname</h3><h4 class="lang current">clj</h4><h4 class="added">added in Encore v3.115.0 (2024-08-18)</h4><div class="usage"><code>(hostname)</code><code>(hostname cache-msecs timeout-msecs fallback-val)</code><code>(hostname fallback-val)</code><code>(hostname timeout-msecs fallback-val)</code></div><div class="doc"><pre class="plaintext">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.</pre></div></div><div class="public anchor" id="var-nano-uid-fn"><h3>nano-uid-fn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-nano-uid-fn">cljs</a></h4><div class="usage"><code>(nano-uid-fn)</code><code>(nano-uid-fn {:keys [secure? root-len child-len], :or {root-len 21, child-len 10}})</code></div><div class="doc"><pre class="plaintext">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</pre></div></div><div class="public anchor" id="var-newline"><h3>newline</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-newline">cljs</a></h4><h4 class="added">added in Encore v3.68.0 (2023-09-25)</h4><div class="usage"></div><div class="doc"><pre class="plaintext">Single system newline
</pre></div></div><div class="public anchor" id="var-pr-edn"><h3>pr-edn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-pr-edn">cljs</a></h4><div class="usage"><code>(pr-edn x)</code></div><div class="doc"><pre class="plaintext">Prints given arg to an edn string readable with `read-edn`.
</pre></div></div><div class="public anchor" id="var-pr-signal-fn"><h3>pr-signal-fn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-pr-signal-fn">cljs</a></h4><div class="usage"><code>(pr-signal-fn)</code><code>(pr-signal-fn {:keys [pr-fn clean-fn incl-newline?], :as opts, :or {pr-fn :edn, clean-fn (clean-signal-fn), incl-newline? true}})</code></div><div class="doc"><pre class="plaintext">Experimental, subject to change.
Returns a (fn pr [signal]) that:
- Takes a Telemere signal (map).
- Returns a machine-readable signal string.
Options:
`:pr-fn` - ∈ #{&lt;unary-fn&gt; :edn (default) :json (Cljs only)}
`:clean-fn` - (fn [signal]) =&gt; clean signal map, see [1]
`:incl-newline?` - Include terminating system newline? (default true)
Examples:
;; To print as edn:
(pr-signal-fn {:pr-fn :edn})
;; To print as JSON:
;; Ref. &lt;<a href="https://github.com/metosin/jsonista">https://github.com/metosin/jsonista</a>&gt; (or any alt JSON lib)
#?(:clj (require '[jsonista.core :as jsonista]))
(pr-signal-fn
{:pr-fn
#?(:cljs :json ; Use js/JSON.stringify
:clj jsonista/write-value-as-string)})
[1] `taoensso.telemere.utils/clean-signal-fn`, etc.
See also `format-signal-fn` for an alternative to `pr-signal-fn`
that produces human-readable output.</pre></div></div><div class="public anchor" id="var-signal-content-fn"><h3>signal-content-fn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-signal-content-fn">cljs</a></h4><div class="usage"><code>(signal-content-fn)</code><code>(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)}})</code></div><div class="doc"><pre class="plaintext">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]) =&gt; string.
`:format-error-fn` - (fn [error]) =&gt; string.</pre></div></div><div class="public anchor" id="var-signal-preamble-fn"><h3>signal-preamble-fn</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-signal-preamble-fn">cljs</a></h4><div class="usage"><code>(signal-preamble-fn)</code><code>(signal-preamble-fn {:keys [format-inst-fn], :or {format-inst-fn (format-inst-fn)}})</code></div><div class="doc"><pre class="plaintext">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]) =&gt; string.</pre></div></div><div class="public anchor" id="var-tcp-socket-writer"><h3>tcp-socket-writer</h3><h4 class="lang current">clj</h4><div class="usage"><code>(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}})</code></div><div class="doc"><pre class="plaintext">Experimental, subject to change.
Connects to specified TCP socket and returns a stateful fn of 2 arities:
[content] =&gt; Writes given content to socket, or noops if closed.
[] =&gt; 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]) =&gt; `java.net.Socket`
`:ssl-socket-fn` - (fn [socket host port]) =&gt; `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.</pre></div></div><div class="public anchor" id="var-thread-id"><h3>thread-id</h3><h4 class="lang current">clj</h4><h4 class="added">added in Encore v3.115.0 (2024-08-18)</h4><div class="usage"><code>(thread-id)</code></div><div class="doc"><pre class="plaintext">Returns long id of current `Thread`.
</pre></div></div><div class="public anchor" id="var-thread-info"><h3>thread-info</h3><h4 class="lang current">clj</h4><h4 class="added">added in Encore v3.115.0 (2024-08-18)</h4><div class="usage"><code>(thread-info)</code></div><div class="doc"><pre class="plaintext">Returns {:keys [group name id]} for current `Thread`.
</pre></div></div><div class="public anchor" id="var-thread-name"><h3>thread-name</h3><h4 class="lang current">clj</h4><h4 class="added">added in Encore v3.115.0 (2024-08-18)</h4><div class="usage"><code>(thread-name)</code></div><div class="doc"><pre class="plaintext">Returns string name of current `Thread`.
</pre></div></div><div class="public anchor" id="var-uuid"><h3>uuid</h3><h4 class="lang current">clj</h4><h4 class="added">added in Encore v3.75.0 (2024-01-29)</h4><div class="usage"><code>(uuid)</code></div><div class="doc"><pre class="plaintext">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`.</pre></div></div><div class="public anchor" id="var-uuid-str"><h3>uuid-str</h3><h4 class="lang current">clj</h4><h4 class="lang"><a href="taoensso.telemere.utils.cljs.html#var-uuid-str">cljs</a></h4><div class="usage"><code>(uuid-str max-len)</code><code>(uuid-str)</code></div><div class="doc"><pre class="plaintext">Returns a random UUID string of given length (max 36).
Uses strong randomness when possible. See also `uuid`, `nanoid`, `rand-id-fn`.</pre></div></div></div></body></html>