From d9c3583631159d09660442109ca339010ade83bc Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Wed, 18 Sep 2024 18:06:03 +0200 Subject: [PATCH] [new] Add `:rate-limit-by` option to all signal creators When present, will cause limits to be per [expansion, by-value] --- README.md | 11 ++++++----- examples.cljc | 11 ++++++----- .../signal-docstrings/signal-options.txt | 1 + projects/main/src/taoensso/telemere/impl.cljc | 18 +++++++++--------- .../shell/src/taoensso/telemere/shell.cljc | 4 ++-- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 409927d..c1b16a2 100644 --- a/README.md +++ b/README.md @@ -74,11 +74,12 @@ It enables you to write code that is **information-verbose by default**. ;; Getting fancy (all costs are conditional!) (t/log! - {:level :debug - :sample-rate 0.75 ; 75% sampling (noop 25% of the time) - :when (my-conditional) - :rate-limit {"1 per sec" [1 1000] - "5 per min" [5 60000]} + {:level :debug + :sample-rate 0.75 ; 75% sampling (noop 25% of the time) + :when (my-conditional) + :rate-limit {"1 per sec" [1 1000] + "5 per min" [5 60000]} + :rate-limit-by my-user-ip-address ; Optional rate-limit scope :do (inc-my-metric!) :let diff --git a/examples.cljc b/examples.cljc index 251d56b..d0c7c25 100644 --- a/examples.cljc +++ b/examples.cljc @@ -34,11 +34,12 @@ ;; Getting fancy (all costs are conditional!) (t/log! - {:level :debug - :sample-rate 0.75 ; 75% sampling (noop 25% of the time) - :when (my-conditional) - :rate-limit {"1 per sec" [1 1000] - "5 per min" [5 60000]} + {:level :debug + :sample-rate 0.75 ; 75% sampling (noop 25% of the time) + :when (my-conditional) + :rate-limit {"1 per sec" [1 1000] + "5 per min" [5 60000]} + :rate-limit-by my-user-ip-address ; Optional rate-limit scope :do (inc-my-metric!) :let diff --git a/projects/main/resources/signal-docstrings/signal-options.txt b/projects/main/resources/signal-docstrings/signal-options.txt index ccb7c61..a36ab4f 100644 --- a/projects/main/resources/signal-docstrings/signal-options.txt +++ b/projects/main/resources/signal-docstrings/signal-options.txt @@ -24,6 +24,7 @@ Signal options (shared by all signal creators): `:sample-rate` - ?rate ∈ℝ[0,1] for signal sampling (0.75 => allow 75% of signals, nil => allow all) `:when` -------- Arb ?form; when present, form must return truthy to allow signal `:rate-limit` -- ?spec as given to `taoensso.telemere/rate-limiter`, see its docstring for details +`:rate-limit-by` When present, rate limits will be enforced independently for each id (any Clojure value!) `:middleware` -- Optional (fn [signal]) => ?modified-signal to apply when signal is created `:trace?` ------ Should tracing be enabled for `:run` form? diff --git a/projects/main/src/taoensso/telemere/impl.cljc b/projects/main/src/taoensso/telemere/impl.cljc index 21fb85a..851088e 100644 --- a/projects/main/src/taoensso/telemere/impl.cljc +++ b/projects/main/src/taoensso/telemere/impl.cljc @@ -378,14 +378,14 @@ '([{:as opts :keys [#_defaults #_elide? #_allow? #_expansion-id, ; Undocumented elidable? location #_location* inst uid middleware, - sample-rate kind ns id level when rate-limit, + sample-rate kind ns id level when rate-limit rate-limit-by, ctx parent root trace?, do let data msg error run & kvs]}]) :signal-allowed? '([{:as opts :keys [#_defaults #_elide? #_allow? #_expansion-id, ; Undocumented elidable? location #_location* #_inst #_uid #_middleware, - sample-rate kind ns id level when rate-limit, + sample-rate kind ns id level when rate-limit rate-limit-by, #_ctx #_parent #_root #_trace?, #_do #_let #_data #_msg #_error #_run #_& #_kvs]}]) :event! ; [id] [id level-or-opts] => allowed? @@ -395,7 +395,7 @@ {:as opts :keys [#_defaults #_elide? #_allow? #_expansion-id, elidable? location #_location* inst uid middleware, - sample-rate kind ns id level when rate-limit, + sample-rate kind ns id level when rate-limit rate-limit-by, ctx parent root trace?, do let data msg error #_run & kvs]}]) :log! ; [msg] [level-or-opts msg] => allowed? @@ -404,7 +404,7 @@ [{:as opts :keys [#_defaults #_elide? #_allow? #_expansion-id, elidable? location #_location* inst uid middleware, - sample-rate kind ns id level when rate-limit, + sample-rate kind ns id level when rate-limit rate-limit-by, ctx parent root trace?, do let data msg error #_run & kvs]} msg]) @@ -414,7 +414,7 @@ [{:as opts :keys [#_defaults #_elide? #_allow? #_expansion-id, elidable? location #_location* inst uid middleware, - sample-rate kind ns id level when rate-limit, + sample-rate kind ns id level when rate-limit rate-limit-by, ctx parent root trace?, do let data msg error #_run & kvs]} error]) @@ -424,7 +424,7 @@ [{:as opts :keys [#_defaults #_elide? #_allow? #_expansion-id, elidable? location #_location* inst uid middleware, - sample-rate kind ns id level when rate-limit, + sample-rate kind ns id level when rate-limit rate-limit-by, ctx parent root trace?, do let data msg error run & kvs]} form]) @@ -434,7 +434,7 @@ [{:as opts :keys [#_defaults #_elide? #_allow? #_expansion-id, rethrow? catch-val, elidable? location #_location* inst uid middleware, - sample-rate kind ns id level when rate-limit, + sample-rate kind ns id level when rate-limit rate-limit-by, ctx parent root trace?, do let data msg error #_run & kvs]} form]) @@ -444,7 +444,7 @@ [{:as opts :keys [#_defaults #_elide? #_allow? #_expansion-id, elidable? location #_location* inst uid middleware, - sample-rate kind ns id level when rate-limit, + sample-rate kind ns id level when rate-limit rate-limit-by, ctx parent root trace?, do let data msg error #_run & kvs]}]) (enc/unexpected-arg! macro-id)))) @@ -616,7 +616,7 @@ (not-empty (dissoc opts :elidable? :location :location* :inst :uid :middleware, - :sample-rate :ns :kind :id :level :filter :when #_:rate-limit, + :sample-rate :ns :kind :id :level :filter :when #_:rate-limit #_:rate-limit-by, :ctx :parent #_:trace?, :do :let :data :msg :error :run, :elide? :allow? #_:expansion-id :otel/context)) diff --git a/projects/shell/src/taoensso/telemere/shell.cljc b/projects/shell/src/taoensso/telemere/shell.cljc index f105d0f..cbd35c2 100644 --- a/projects/shell/src/taoensso/telemere/shell.cljc +++ b/projects/shell/src/taoensso/telemere/shell.cljc @@ -133,7 +133,7 @@ [fallback, ; Unique to shell #_defaults #_elide? #_allow? #_expansion-id, ; Undocumented elidable? location #_location* inst uid middleware, - sample-rate kind ns id level when rate-limit, + sample-rate kind ns id level when rate-limit rate-limit-by, ctx parent root trace?, do let data msg error run & kvs]}])} [opts] @@ -172,7 +172,7 @@ [#_fallback, ; Unique to shell #_defaults #_elide? #_allow? #_expansion-id, ; Undocumented elidable? location #_location* #_inst #_uid #_middleware, - sample-rate kind ns id level when rate-limit, + sample-rate kind ns id level when rate-limit rate-limit-by, #_ctx #_parent #_root #_trace?, #_do #_let #_data #_msg #_error #_run #_& #_kvs]}])} [opts]