Compare commits

..

4 commits

Author SHA1 Message Date
Peter Taoussanis
a6fc4adf6a [new] OpenTelemetry handler: support spans created outside Telemere
Some checks failed
Clj tests / tests (17, ubuntu-latest) (push) Has been cancelled
Clj tests / tests (19, ubuntu-latest) (push) Has been cancelled
Clj tests / tests (21, ubuntu-latest) (push) Has been cancelled
Cljs tests / tests (21, ubuntu-latest) (push) Has been cancelled
Graal tests / tests (17, macOS-latest) (push) Has been cancelled
Graal tests / tests (17, ubuntu-latest) (push) Has been cancelled
Graal tests / tests (17, windows-latest) (push) Has been cancelled
BEFORE this commit:

  Telemere captured OpenTelemetry context only when generating
  tracing signals (`trace!`, `spy!`, etc.).

AFTER this commit:

  Telemere always captures OpenTelemetry context when present.

Motivation for the change:

  Telemere users may have spans automatically created by the
  OpenTelemetry Java Agent, or manually created by other libs
  like clj-otel.

  By always capturing the OpenTelemetry context when present,
  this lets even non-tracing Telemere signals (like `log!`)
  include the relevant trace and span IDs for external tooling.

Thanks to @devurandom for suggesting this, and for helping
debug/identify the necessary changes 🙏
2025-12-05 08:03:41 +01:00
Peter Taoussanis
6155713fde [fix] OpenTelemetry handler: add missing line info to output
Previous code was a vestigial leftover from when signals
had {:line <num> :column <num> ...} rather than
{:coords [<line-num> <column-num>] ...}
2025-12-04 21:13:41 +01:00
Peter Taoussanis
a883df3c41 [new] [#68] Add config to skip host and/or thread info 2025-12-04 21:13:41 +01:00
Peter Taoussanis
e6ce33dd4e [mod] SLF4J->Telemere backend: move noisy stuff out of signal data
This is a BREAKING change for the small minority of users that:

  1. Are using the `taoensso.telemere.slf4j` backend, AND
  2. Are using the low-level `:slf4j/args` or `:slf4j/marker-names`
     values in signal `:data`

BEFORE this commit:

  SLF4J signals contain:
  {:data {:slf4j/kvs {...},
          :slf4j/args [...],
          :slf4j/marker-names #{...}},
   ...}.

AFTER this commit:

  SLF4J signals contain:
  {:data {:slf4j/kvs {...}},
   :kvs  {:slf4j/args <Object[]>,
          :slf4j/markers #{...}},
   ...}

  So:
    - [:data :slf4j/marker-names] has moved to [:kvs :slf4j/markers].
    - [:data :slf4j/args]         has moved to [:kvs :slf4j/args],
      and is now an Object[] rather than vector.

Motivation for the change:

  The new behaviour is a more sensible default.

  Basically: anything in `:data` is included by default in output.
  But :slf4j/args are generally anyway already in the signal's formatted
  message, so this ends up just creating duplicate output.

  Likewise markers are generally used more for filtering/xfns than for
  output labelling, so excluding them from default output is sensible.
2025-12-04 21:13:37 +01:00
3 changed files with 38 additions and 35 deletions

View file

@ -26,7 +26,7 @@ All options are available for all signal creator calls:
`:elidable?` --- Should signal be subject to compile-time elision? (default true)
`:allow?` ------ Custom override for usual runtime filtering (true => ALWAYS create signal)
`:trace?` ------ Should tracing be enabled for this signal? (default true iff `:run` provided or OpenTelemetry tracing enabled)
`:trace?` ------ Should tracing be enabled for `:run` form?
`:sample` ------ Sample ?rate ∈ℝ[0,1] for random signal sampling (0.75 => allow 75% of signals, nil => allow all)
`:when` -------- Arb ?form; when present, form must return truthy to allow signal

View file

@ -569,10 +569,7 @@
id-form :id
level-form :level} opts
otel? (get opts :otel? (and clj? present:otel?)) ; Undocumented
otel-tracing? (and otel? enabled:otel-tracing?)
trace? (get opts :trace? (or run-form? otel-tracing?))
trace? (get opts :trace? run-form?)
_
(when-not (contains? #{true false nil} trace?)
(truss/ex-info!
@ -617,7 +614,7 @@
:sample :ns :kind :id :level :filter :when #_:limit #_:limit-by,
:ctx :ctx+ :parent :trace?, :do :let :data :msg :error,
:run :run-form :run-val, :elide? :allow? #_:callsite-id,
:host :thread :otel? :otel/context))]
:host :thread :otel/context))]
(if-let [kvs+ (get opts :kvs+)] ; Undocumented
(if base
@ -641,10 +638,10 @@
(let [record-form
(let [clause [(if run-form? :run :no-run) (if clj? :clj :cljs)]]
(case clause
[:run :clj ] `(Signal. 1 ~'__inst ~'__uid, ~'__ns ~coords ~host-form ~'__thread ~'__otel-context1, ~sample-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root1, ~data-form ~kvs-form ~'_msg_, ~'_run-err '~show-run-form ~show-run-val ~'_end-inst ~'_run-nsecs)
[:run :cljs] `(Signal. 1 ~'__inst ~'__uid, ~'__ns ~coords ~sample-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root1, ~data-form ~kvs-form ~'_msg_, ~'_run-err '~show-run-form ~show-run-val ~'_end-inst ~'_run-nsecs)
[:no-run :clj ] `(Signal. 1 ~'__inst ~'__uid, ~'__ns ~coords ~host-form ~'__thread ~'__otel-context1, ~sample-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root1, ~data-form ~kvs-form ~msg-form, ~error-form nil nil nil nil)
[:no-run :cljs] `(Signal. 1 ~'__inst ~'__uid, ~'__ns ~coords ~sample-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root1, ~data-form ~kvs-form ~msg-form, ~error-form nil nil nil nil)
[:run :clj ] `(Signal. 1 ~'__inst ~'__uid, ~'__ns ~coords ~host-form ~'__thread ~'__otel-context, ~sample-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root1, ~data-form ~kvs-form ~'_msg_, ~'_run-err '~show-run-form ~show-run-val ~'_end-inst ~'_run-nsecs)
[:run :cljs] `(Signal. 1 ~'__inst ~'__uid, ~'__ns ~coords ~sample-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root1, ~data-form ~kvs-form ~'_msg_, ~'_run-err '~show-run-form ~show-run-val ~'_end-inst ~'_run-nsecs)
[:no-run :clj ] `(Signal. 1 ~'__inst ~'__uid, ~'__ns ~coords ~host-form ~'__thread ~'__otel-context, ~sample-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root1, ~data-form ~kvs-form ~msg-form, ~error-form nil nil nil nil)
[:no-run :cljs] `(Signal. 1 ~'__inst ~'__uid, ~'__ns ~coords ~sample-form, ~'__kind ~'__id ~'__level, ~ctx-form ~parent-form ~'__root1, ~data-form ~kvs-form ~msg-form, ~error-form nil nil nil nil)
(truss/ex-info!
(str "Unexpected signal constructor args at "
(sigs/format-callsite ns-form coords)))))
@ -685,12 +682,26 @@
run-fn-form (when run-form? `(fn [] ~run-form))
run-form* (when run-form? `(~'__run-fn-form))
into-let-form
binds-form-base
`[~'__inst ~inst-form
~'__thread ~thread-form
~'__root0 ~root-form0 ; ?{:keys [id uid]}
~'__otel-context
~(when (and clj? enabled:otel-tracing?)
(if run-form?
`(otel-context+span ~'__id ~'__inst ~(get opts :otel/context `(otel-context)) ~(get opts :otel/span-kind))
(do (get opts :otel/context `(otel-context)))))
~'__uid
~(if (and clj? enabled:otel-tracing? trace?)
(auto-> uid-form `(or (otel-span-id ~'__otel-context) (com.taoensso.encore.Ids/genHexId16)))
(auto-> uid-form `(taoensso.telemere/*uid-fn* (if ~'__root0 false true))))]
binds-form-more
(enc/cond!
(not trace?) ; Don't trace
`[~'__otel-context1 nil
~'__uid ~(auto-> uid-form `(taoensso.telemere/*uid-fn* (if ~'__root0 false true)))
~'__root1 ~'__root0 ; Retain, but don't establish
(not trace?) ; Non-tracing signal
`[~'__root1 ~'__root0 ; Retain, but don't establish
~'__run-result
~(when run-form?
`(let [t0# (enc/now-nano*)]
@ -699,10 +710,8 @@
(catch :all t# (RunResult. nil t# (- (enc/now-nano*) t0#))))))]
;; Trace without OpenTelemetry
(or cljs? (not otel-tracing?))
`[~'__otel-context1 nil
~'__uid ~(auto-> uid-form `(taoensso.telemere/*uid-fn* (if ~'__root0 false true)))
~'__root1 (or ~'__root0 ~(when trace? `{:id ~'__id, :uid ~'__uid}))
(or cljs? (not enabled:otel-tracing?))
`[~'__root1 (or ~'__root0 ~(when trace? `{:id ~'__id, :uid ~'__uid}))
~'__run-result
~(when run-form?
`(binding [*trace-root* ~'__root1
@ -713,21 +722,18 @@
(catch :all t# (RunResult. nil t# (- (enc/now-nano*) t0#)))))))]
;; Trace with OpenTelemetry
(and clj? otel-tracing?)
`[~'__otel-context0 ~(get opts :otel/context `(otel-context)) ; Context
~'__otel-context1 ~(if run-form? `(otel-context+span ~'__id ~'__inst ~'__otel-context0 ~(get opts :otel/span-kind)) `~'__otel-context0)
~'__uid ~(auto-> uid-form `(or (otel-span-id ~'__otel-context1) (com.taoensso.encore.Ids/genHexId16)))
~'__root1
(and clj? enabled:otel-tracing?)
`[~'__root1
(or ~'__root0
~(when trace?
`{:id ~'__id, :uid (or (otel-trace-id ~'__otel-context1) (com.taoensso.encore.Ids/genHexId32))}))
`{:id ~'__id, :uid (or (otel-trace-id ~'__otel-context) (com.taoensso.encore.Ids/genHexId32))}))
~'__run-result
~(when run-form?
`(binding [*otel-context* ~'__otel-context1
`(binding [*otel-context* ~'__otel-context
*trace-root* ~'__root1
*trace-parent* {:id ~'__id, :uid ~'__uid}]
(let [otel-scope# (.makeCurrent ~'__otel-context1)
(let [otel-scope# (.makeCurrent ~'__otel-context)
t0# (enc/now-nano*)]
(truss/try*
(do (RunResult. ~run-form* nil (- (enc/now-nano*) t0#)))
@ -744,11 +750,8 @@
(enc/if-not ~allow?
~run-form*
(let [~'__inst ~inst-form
~'__thread ~thread-form
~'__root0 ~root-form0 ; ?{:keys [id uid]}
~@into-let-form ; Inject conditional bindings
(let [~@binds-form-base
~@binds-form-more
signal# ~signal-delay-form]
(dispatch-signal!

View file

@ -447,10 +447,10 @@
(let [sv (with-sig (sig! {:level :info, :parent {:uid :uid1, :foo :bar}}))] (is (sm? sv {:parent {:id :submap/nx :uid :uid1, :foo :bar}}) "Manual `:parent/uid`"))
(let [sv (with-sig (sig! {:level :info, :root {:id :id1, :foo :bar}}))] (is (sm? sv {:root {:id :id1 :uid :submap/nx, :foo :bar}}) "Manual `:root/id`"))
(let [sv (with-sig (sig! {:level :info, :root {:uid :uid1, :foo :bar}}))] (is (sm? sv {:root {:id :submap/nx :uid :uid1, :foo :bar}}) "Manual `:root/uid`"))
(let [sv (with-sig (sig! {:level :info, :otel? false})) ] (is (sm? sv {:uid nil})))
(let [sv (with-sig (sig! {:level :info})) ] (is (sm? sv {:uid nil})))
(let [sv (with-sig (sig! {:level :info, :uid :auto})) ] (is (sm? sv {:uid string?})))
(let [sv (binding [tel/*uid-fn* (fn [_] "my-uid")]
(with-sig (sig! {:level :info, :uid :auto, :otel? false}))) ] (is (sm? sv {:uid "my-uid"})))])
(with-sig (sig! {:level :info, :uid :auto}))) ] (is (sm? sv {:uid "my-uid"})))])
(testing "Auto parent/root"
[(testing "Tracing disabled"
@ -882,7 +882,7 @@
(is (= ((tel/pr-signal-fn {:pr-fn (fn [_] "str")}) sig) (str "str" utils/newline))))]))
(testing "format-signal-fn"
(let [sig (with-sig :raw :trap (tel/event! ::ev-id {:inst t1, :msg ["a" "b"], :otel? false}))]
(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) (dissoc sig :host)) "::ev-id a b\n"))]))])])