是的!您可以将所需的任何代码放入where
子句的条件位置这里是一个示例配置,它取决于一天中的时间和一周中的一天来决定何时发送“警报”:
; -*- mode: clojure; -*-
>; vim: filetype=clojure
(riemann.repl/start-server! {:port 5557 :host "0.0.0.0"}) ;; this is only safe in a docker container
; Listen on the local interface over TCP (5555), UDP (5555), and websockets
; (5556)
(let [host "0.0.0.0"]
(tcp-server {:host host})
(ws-server {:host host}))
; Expire old events from the index every 5 seconds.
(periodically-expire 5)
(require '[clj-time [core :refer [now hour day-of-week]]])
(let [index (tap :index (index))]
(streams
(where (service "log")
(fixed-event-window 3
(smap
(fn [events]
(let [count-of-failures (count (filter #(= "error" (:type %)) events))
new-event (assoc (first events)
:status "Failure"
:metric count-of-failures
:ttl 300
:total-fail (> count-of-failures 2))]
new-event))
(where (fn [event]
(let [now (clj-time.core/now)]
(and (<= 0 (clj-time.core/hour now) 17)
(<= 2 (clj-time.core/day-of-week now) 6)
(= (:status event) "Failure")
(:total-fail event))))
(throttle 1 200
#(println "emailing about" %)
index)))))))
(tests
(deftest index-test
(let [workday (clj-time.format/parse "2142-01-01T00:00:00.000Z")
weekend (clj-time.format/parse "2142-01-03T00:00:00.000Z")]
(let [index-after-events (with-redefs [clj-time.core/now (constantly workday)]
(inject! [{:service "log"
:type "error"
:time 42}
{:service "log"
:type "error"
:time 43}
{:service "log"
:type "error"
:time 44}]))]
(is (= index-after-events
{:index [{:service "log", :type "error", :time 42, :status "Failure", :metric 3, :ttl 300, :total-fail true}]})))
(let [index-after-events (with-redefs [clj-time.core/now (constantly weekend)]
(inject! [{:service "log"
:type "error"
:time 42}
{:service "log"
:type "error"
:time 43}
{:service "log"
:type "error"
:time 44}]))]
(is (= index-after-events
{:index []}))))))
并运行测试:
root@176ee53d0c62:/test# riemann test sample.config
INFO [2016-10-03 22:07:39,805] main - riemann.bin - Loading /test/sample.config
INFO [2016-10-03 22:07:39,840] main - riemann.repl - REPL server {:port 5557, :host 0.0.0.0} online
Testing riemann.config-test
emailing about {:service log, :type error, :time 42, :status Failure, :metric 3, :ttl 300, :total-fail true}
Ran 1 tests containing 2 assertions.
0 failures, 0 errors.
在这个例子中我使用fixed-event-window
了因为我不知道如何在黎曼的单元测试中使固定时间窗口工作。我认为花费了足够长的时间来弄清楚我认为您会对迄今为止的解决方案感兴趣。如果您将其更改为固定时间窗口,它将以相同的方式工作,但测试不会运行。