riemann - Need help in optimising clojure statement -


i'm new clojure , need set riemann config easy edit/add new conditions. have now:

(defn tell-ops  ([to]     (by [:service]         (throttle 3 360             (rollup 2 360                 slackerdefault                 (email to))))) ([to channel]     (by [:service]         (throttle 3 360             (rollup 2 360                 (slacker channel)                 (email to))))))      ............  (where (state "fatal")     (where (service #"^serv1")             (tell-ops "dev.ops1@foo.com" "#dev-ops1"))      (where (service #"^serv2")             (tell-ops "dev.ops2@bar.com"))    ....  ) 

moreover, lacks default statement, if nothing matches, tell-ops "admin@fo.bar"

i think need top level struct

(def services   [{:regex #"^serv1" :mail "foo@bar.com" :channel "#serv1"}    {:regex #"serv2$" :mail "foo@baz.com"} ]) 

so easy add new ones. have no idea how loop throuth array considering absence of :channel in second case , making "default call" if none of regexes matches

thanks in advance

i don't know riemann, think can solve problem using standard clojure data processing tools. idea of top-level structure alert policies. add catch-all policy @ end handle default case. modifying code bit make less riemann-specific:

(defn tell-ops!   [{:keys [mail channel]}]   (when mail (println (str "mail = " mail)))   (when channel (println (str "channel = " channel))))  (def policies   [{:regex #"^serv1.*" :mail "foo@bar.com" :channel "#serv1"}    {:regex #".*serv2$" :mail "foo@baz.com"}     {:regex #".*" :mail "default@bar.com"}])  (defn alert!   [policies service-in-alert]   (-> (drop-while (fn                      [{:keys [regex]}]                      (nil? (re-matches regex service-in-alert)))          policies)     first     tell-ops!)) 

some notes:

  • it's idiomatic use ! in function names functions produce side effects (such posting message channel or sending e-mail)
  • if want hard default tell-ops!function, can use clojure's support default values when destructuring maps: {:keys [mail channel] :or {mail "default@foo.bar" channel "#default-chan"}}

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -