taoensso / telemere

Structured telemetry library for Clojure/Script
https://www.taoensso.com/telemere
Eclipse Public License 1.0
200 stars 5 forks source link

:kind-filter in add-handler! does not appear to be working #23

Closed johanatan closed 2 months ago

johanatan commented 2 months ago
(defn handler:postgres-handler
  "Returns a Telemere signal handler that stores logs in the PostgreSQL database.

  Options:
    `:storage` - An instance of the Storage protocol for database operations

  Tips:
    - Ensure the storage instance is properly initialized before passing it to the handler
    - This handler will store logs in the application_logs table"

  [{:keys [storage] :as opts}]
  (let [handler-fn
        (fn handler:postgres-handler
          ([]
           ;; Arity-0 called when stopping the handler
           ;; Nothing to do here as we're not maintaining any stateful resources
           )
          ([signal]
           (let [{:keys [data]} signal
                 {:keys [print-console?]} data
                 data (dissoc data :print-console?)]
             (js/console.log (format "kind: %s" (:kind signal)))
             (js/console.log (format "handlers stats: %s" (t/get-handlers-stats)))
             (insert-signal storage (assoc signal :data data))
             (when print-console?
               (console-handler (assoc signal :data data))))))]
    handler-fn))

(defn handler:postgres-trace-handler [{:keys [storage] :as opts}]
  (let [handler-fn
        (fn handler:postgres-trace-handler
          ([]
           ;; todo flush-bufffer
           )
          ([signal]
           ;; tbd
           (js/console.log "got a trace")
           nil))]
    handler-fn))

(defn init-logging!
  "Initializes Telemere logging with the Postgres handler.
   Call this function when your application starts."
  [storage]
  (t/remove-handler! :default/console)
  (t/set-min-level! :log :debug)
  (t/set-min-level! :trace :debug)
  (t/add-handler! :postgres (handler:postgres-handler {:storage storage})
                  {:kind-filter {:disallow :trace}})
  (t/add-handler! :postgres-tracer (handler:postgres-trace-handler {:storage storage})
                  {:kind-filter {:allow :trace}})

  (js/console.log (format "filters: %s" (t/get-filters))))

given this code, all the :trace signals are going to the wrong handler. also, strangely get-handlers-stats is returning NULL always.

ptaoussanis commented 2 months ago

@johanatan Hi Johanatan, I'm about to get some sleep and currently without a REPL to check your example. Will need to look closer tomorrow to be sure, but at least nothing stands out as obviously wrong with your example.

In case it's helpful in the meantime, here's a shorter example:

(tel/with-handler ::my-handler println {:kind-filter {:allow :my-kind}}
  (tel/signal! {:kind :my-kind, :level :info})) ; Should print signals iff `{:kind :my-kind}`

(tel/with-handler ::my-handler println {:kind-filter {:disallow :my-kind}}
  (tel/signal! {:kind :my-kind, :level :info})) ; Should print signals iff NOT `{:kind :my-kind}`

also, strangely get-handlers-stats is returning NULL always.

That is strange, I can't think of any obvious reason that'd be the case. Is get-handlers returning what you expect?

johanatan commented 2 months ago

yea that's not working here.

and get-handlers-stats seems to be returning null because the two handlers have track-stats? set to false despite the documentation saying that it should default to true.

johanatan commented 2 months ago

here is the result of running (t/get-handlers):

{:postgres {:dispatch-opts {:priority 100, :track-stats? false, :kind-filter {:disallow :trace}}, :handler-fn #object[options$logging$handler_COLON_postgres_handler_$_handler_COLON_postgres_handler]},
 :postgres-tracer {:dispatch-opts {:priority 100, :track-stats? false, :kind-filter {:allow :trace}}, :handler-fn #object[options$logging$handler_COLON_postgres_trace_handler_$_handler_COLON_postgres_trace_handler]}}

notice that the kind-filters are specified properly but they are both having no effect. both 'trace' and 'log' are going to the :postgres handler.

ptaoussanis commented 2 months ago

And to confirm- you're for sure running the latest version of Telemere (currently beta 22)?

Could you share what values you have for the following?:

yea that's not working here.

Interesting, these both work on my side. Will try investigate later today once you've confirmed that you're on beta 22 👍

johanatan commented 2 months ago

I'm on beta 14 actually. I'll try with 22 and get back to you.

ptaoussanis commented 2 months ago

Okay, that's the likely source of the trouble then. Please make sure that you're always on the latest beta to ensure that you've got all the latest fixes and features.

In case it's helpful, you can use something like lein-ancient or GitHub's watch feature to be notified of new releases:

68747470733a2f2f7777772e74616f656e73736f2e636f6d2f6173736574732f696d67732f73637265656e73686f742d6769746875622d6e6f74696669636174696f6e732e706e67

johanatan commented 2 months ago

Okay. Thanks. Sorry about that. This was indeed the issue.

johanatan commented 2 months ago

Btw, I prefer tools.deps over lein but I did sign up for the GitHub watch.

ptaoussanis commented 2 months ago

Okay. Thanks. Sorry about that. This was indeed the issue.

No worries, happy to hear everything's working 👍