Hi Tianxiang, In my experience a good way to decouple a non-trivial fn with metrics is to make the fn provide hooks for various events when metrics may be gathered.
(defn nop [& args]) (defn foo->bar [foo {:keys [on-foo on-bar] :or {on-foo nop on-bar nop} :as options}] (on-foo) (let [bar (produce-bar foo)] (on-bar) bar)) Now, when unit testing for functionality of foo->bar you can pass {} as options, which wouldn't do any metrics collection. In actual usage, or for unit testing metrics collection, you would pass {:on-foo #(counter/inc! foo-counter) :on-bar #(counter/inc! bar-counter)} as options. This is a small fn, so it may not be apparent how can providing hooks be useful, but for large fns with multiple metrics-collection I've found providing hooks to be working quite well. HTH Shantanu On Saturday, 12 November 2016 08:24:40 UTC+5:30, Tianxiang Xiong wrote: > > What are some good ways of handling metric gathering (or, more generally, > AOP) in Clojure? > > Suppose we have an application in which there is a transformation > function, foo->bar. Every time a foo is input to foo->bar, we'd like to > increment the foo-counter. Every time a bar is produced, we'd like to > increment the bar-counter. > > To keep foo->bar pure, we can wrap foo->bar in another function like: > > (fn [& args] > (counter/inc! foo-counter) > (let [result (apply f args)] > (counter/inc! bar-counter) > result)) > > But the side effect of incrementing counters makes it difficult to write a > unit test. > > I have a vague idea of what I'd *like* to see, which is the function > returning: > > - The result > - Some operation to be carried out, OR some data allowing the receiver > to carry out some operation? > > I'm sure this is not a new problem for Clojure applications; what are some > strategies for handling it? > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.