1

我对 clojure 很陌生,需要设置一个riemann配置,以便轻松编辑/添加新条件。我们现在拥有的:

(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"))
   ....

)

此外,它缺少默认语句,例如 if nothing match, tell-ops "admin@fo.bar"

我想我需要像顶级结构这样的东西

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

这样很容易添加新的。但是我不知道如何在第二种情况下考虑到缺少 :channel 并在没有正则表达式匹配的情况下进行“默认调用”来循环该数组

提前致谢

4

1 回答 1

1

我对 Riemann 了解不多,但我认为您可以使用标准的 Clojure 数据处理工具解决您的问题。我喜欢您对警报策略的顶级结构的想法。我会在最后添加一个包罗万象的策略来处理默认情况。稍微修改您的代码以使其不那么特定于黎曼:

(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!))

一些注意事项:

  • !在函数名称中使用会产生副作用的函数(例如向频道发布消息或发送电子邮件)是惯用的
  • 如果你想要tell-ops!函数的硬默认值,你可以在解构映射时使用 Clojure 对默认值的支持:{:keys [mail channel] :or {mail "default@foo.bar" channel "#default-chan"}}
于 2015-08-25T08:50:19.037 回答