telemere/taoensso.telemere.utils.html

144 lines
21 KiB
HTML
Raw Normal View History

2024-03-28 15:32:21 +00:00
<!DOCTYPE html PUBLIC ""
"">
2024-08-28 16:36:50 +00:00
<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-beta22</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-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="
2024-05-10 13:50:11 +00:00
</pre></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.
2024-03-28 15:32:21 +00:00
Returns true iff given signal has an `:error` value, or a `:kind` or `:level`
2024-05-06 14:10:30 +00:00
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.
2024-04-05 16:27:05 +00:00
Opens the specified file and returns a stateful fn of 2 arities:
2024-05-23 08:10:16 +00:00
[content] =&gt; Writes given content to file, or noops if closed.
2024-04-05 16:27:05 +00:00
[] =&gt; Closes the writer.
2024-04-29 07:21:13 +00:00
Useful for basic handlers that write to a file, etc.
2024-04-05 16:27:05 +00:00
2024-04-29 07:21:13 +00:00
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.
2024-03-28 15:32:21 +00:00
Returns a (fn format [error]) that:
- Takes a platform error (`Throwable` or `js/Error`).
2024-05-06 14:10:30 +00:00
- 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.
2024-04-05 16:27:05 +00:00
2024-03-28 15:32:21 +00:00
Returns a (fn format [instant]) that:
- Takes a platform instant (`java.time.Instant` or `js/Date`).
2024-05-06 14:10:30 +00:00
- Returns a formatted human-readable instant string.
2024-03-28 15:32:21 +00:00
2024-04-05 16:27:05 +00:00
Options:
2024-04-10 16:41:24 +00:00
`:zone` (Clj only) `java.time.ZoneOffset` (defaults to UTC).
2024-04-05 16:27:05 +00:00
`:formatter`
`java.time.format.DateTimeFormatter` (Clj) or
`goog.i18n.DateTimeFormat` (Cljs),
2024-03-28 15:32:21 +00:00
2024-04-05 16:27:05 +00:00
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.
2024-03-28 15:32:21 +00:00
Returns a (fn format [nanosecs]) that:
- Takes a long nanoseconds (e.g. runtime).
2024-05-06 14:10:30 +00:00
- Returns a human-readable string like:
2024-05-05 11:13:03 +00:00
"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.
2024-03-28 15:32:21 +00:00
Returns a (fn format [signal]) that:
2024-05-06 14:10:30 +00:00
- Takes a Telemere signal (map).
- Returns a human-readable signal string.
Options:
`:incl-newline?` - Include terminating system newline? (default true)
2024-08-26 11:00:59 +00:00
`:preamble-fn` - (fn [signal]) =&gt; signal preamble string, see [1].
`:content-fn` - (fn [signal]) =&gt; signal content string, see [2].
2024-05-05 11:13:03 +00:00
2024-08-26 11:00:59 +00:00
[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.
2024-08-19 14:09:46 +00:00
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
2024-03-28 15:32:21 +00:00
</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`.
2024-05-10 13:50:11 +00:00
</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 incl-kvs? incl-nils? incl-newline? incl-keys], :as opts, :or {pr-fn :edn, incl-newline? true}})</code></div><div class="doc"><pre class="plaintext">Experimental, subject to change.
2024-05-06 14:10:30 +00:00
Returns a (fn pr [signal]) that:
- Takes a Telemere signal (map).
2024-08-26 11:00:59 +00:00
- Returns a machine-readable signal string.
2024-05-05 11:13:03 +00:00
2024-05-06 14:10:30 +00:00
Options:
2024-05-08 07:27:07 +00:00
`:pr-fn` - ∈ #{&lt;unary-fn&gt; :edn (default) :json (Cljs only)}
2024-08-07 09:48:04 +00:00
`:incl-kvs?` - Include signal's app-level kvs? (default false)
2024-05-10 13:50:11 +00:00
`: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
2024-08-23 12:09:04 +00:00
excluded by default: #{:location :kvs :file :host :thread}
2024-05-05 11:13:03 +00:00
Examples:
2024-05-08 07:27:07 +00:00
2024-08-26 11:00:59 +00:00
;; To print as edn:
(pr-signal-fn {:pr-fn :edn})
2024-05-05 11:13:03 +00:00
2024-08-26 11:00:59 +00:00
;; 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)})
2024-05-05 11:13:03 +00:00
2024-08-23 12:09:04 +00:00
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.
2024-08-26 11:00:59 +00:00
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.
2024-05-05 11:13:03 +00:00
Returns a (fn content [signal]) that:
2024-05-06 14:10:30 +00:00
- Takes a Telemere signal (map).
- Returns a signal content ?string (incl. data, ctx, etc.).
2024-05-05 11:13:03 +00:00
2024-05-06 14:10:30 +00:00
Options:
2024-05-10 13:50:11 +00:00
`:raw-error?` - Retain unformatted error? (default false)
`:incl-keys` - Subset of signal keys to retain from those
2024-08-23 12:09:04 +00:00
otherwise excluded by default: #{:kvs :host :thread}
2024-05-06 14:10:30 +00:00
`: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.
2024-05-05 11:13:03 +00:00
Returns a (fn preamble [signal]) that:
2024-05-06 14:10:30 +00:00
- Takes a Telemere signal (map).
2024-05-05 11:13:03 +00:00
- Returns a signal preamble ?string like:
"2024-03-26T11:14:51.806Z INFO EVENT Hostname taoensso.telemere(2,21) ::ev-id - msg"
2024-05-06 14:10:30 +00:00
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.
2024-05-05 11:13:03 +00:00
Connects to specified TCP socket and returns a stateful fn of 2 arities:
2024-05-23 08:10:16 +00:00
[content] =&gt; Writes given content to socket, or noops if closed.
2024-05-05 11:13:03 +00:00
[] =&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
2024-08-19 14:09:46 +00:00
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>