Skip to content

Commit

Permalink
[mod] [BREAKING] NB completely refactor core
Browse files Browse the repository at this point in the history
This is a major update, but mostly focused on internals.
A few changes may be necessary when upgrading, sincere apologies!

Detailed migration notes will be provided on final release.

** High-level motivation **

  Tufte, Timbre, and a new upcoming library (Telemere) all share similar core
  functionality.

  Rather than implement that core 3 separate times, I'm aiming to unify them
  under a single shared core.

  This'll mean:

    - Less code to maintain.
    - Fewer opportunities for bugs.
    - A unified core feature set and API.
    - An quick+efficient way for core improvements to flow to all libraries as
      soon as improvements are ready.

  While undergoing this work, I took the opportunity to also generally simplify
  and polish Tufte's codebase.

  I plan to do the same for Timbre in future.

** Changes **

  - All the `add-x-handler!` fns have been deprecated.

  - Likewise the `add-handler!` API has been updated for much improved flexibility.
    An `add-legacy-handler!` util has been added to help ease migration.

  - Removed vars: `*min-level*`, `*ns-filter*`, `may-profile?`.
    See the `filtering-help` var for more info.

  - `defnp` and `fnp` no longer add a `defn_`/`fn_` name prefix to pre-named fns.

** New **

  - Added `filtering-help` var that describes the new filtering API.
  - Added `handlers-help`  var that describes the new handlers  API.

  - It's now possible to filter profiling by profiling id.
  - It's now possible to specify minimum levels by namespace.

  - `profiled` and `profile` now both support a number of new options:
    {:keys [id level sample rate-limit filter ...]}, see docstrings for details.

  - Handlers now all support a number of new options:
    {:keys [async sample rate-limit filter-fn ...]}, see docstrings for details.

  - Handler fns now support an optional shutdown arity for releasing resources, etc.

  - A new handler has been added for integration with the upcoming Telemere library.

  - Both compile-time and runtime filter config can now be specified by system values.
  - Both Clj and Cljs             filter config can now be specified by system values.

  - Misc docstring improvements and additions.

** Other improvements **

  - Significantly simpler, cleaner codebase - easing future maintenance.
  - Significantly improved performance.
  • Loading branch information
ptaoussanis committed Sep 27, 2023
1 parent 2f57673 commit 9ad2642
Show file tree
Hide file tree
Showing 8 changed files with 939 additions and 1,063 deletions.
21 changes: 9 additions & 12 deletions examples/clj/src/example/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@
simple ongoing application performance monitoring.
Note that a similar pattern can also be used for Cljs apps.
See the Tufte README for more info!"

{:author "Peter Taoussanis (@ptaoussanis)"}
(:require
[clojure.string :as str]
[ring.middleware.defaults]
[ring.adapter.jetty]
[compojure.core :as comp :refer (defroutes GET POST)]
[compojure.core :as comp :refer [defroutes GET POST]]
[compojure.route :as route]
[hiccup.core :as hiccup]
[taoensso.encore :as enc :refer (have have?)]
[taoensso.encore :as enc :refer [have have?]]
[taoensso.tufte :as tufte]))

(enc/defonce stats-accumulator
"On eval, will register a Tufte handler to accumulate the results from
all unfiltered `profile` data. Deref this to get the accumulated data.
(tufte/profile {:id :endpoint1} ...)
(tufte/profile {:id :endpoint2} ...)
...
@stats-accumulator => Return accumulated performance stats"
"Accumulates results from all unfiltered `profile` calls.
Deref this to get the accumulated data."
(tufte/stats-accumulator))

(tufte/add-accumulating-handler! {:ns-pattern "*"}))
;; Register handler for `profile` results
(tufte/add-handler! :my-accumulating-handler
(tufte/accumulating-handler stats-accumulator)
{:ns-filter "*"})

(defroutes ring-routes
(GET "/" ring-req
Expand Down
5 changes: 3 additions & 2 deletions examples/cljs/src/example/hello.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(ns example.hello
(:require [taoensso.tufte :as tufte
:refer-macros (defnp p profiled profile)]))
:refer [defnp p profiled profile]]))

(tufte/add-basic-println-handler! {})
(tufte/add-handler! :my-print-handler
(tufte/print-handler))

(defn get-x [] (+ 1 1))
(defn get-y [] (+ 2 2))
Expand Down
14 changes: 6 additions & 8 deletions examples/readme_examples.cljc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(ns readme-examples
"Basic examples that appear in the Truss README."
(:require [taoensso.tufte]))
(:require [taoensso.tufte :as tufte]))

(comment

(taoensso.tufte/refer-tufte) ; Setup Tufte's ns imports (works with clj only)
(taoensso.tufte/add-basic-println-handler! {}) ; Send `profile` stats to `println`
(tufte/refer-tufte) ; Setup Tufte's ns imports
(tufte/add-handler! :my-print-handler
(tufte/print-handler)) ; Send `profile` stats to `print`

;;; Let's define a couple dummy fns to simulate doing some expensive work
(defn get-x [] (Thread/sleep 500) "x val")
Expand All @@ -31,11 +32,8 @@

(comment

(ns my-clj-ns ; Clojure namespace
(:require [taoensso.tufte :as tufte :refer (defnp p profiled profile)]))

(ns my-cljs-ns ; ClojureScript namespace
(:require [taoensso.tufte :as tufte :refer-macros (defnp p profiled profile)]))
(ns my-ns
(:require [taoensso.tufte :as tufte :refer [defnp p profiled profile]]))

)

Expand Down
Loading

0 comments on commit 9ad2642

Please sign in to comment.