Yes! you can put any code you want into the conditional positon of a where
clause here is an example config that depends on the time of day and day of week to decide when to send "alerts":
; -*- 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 []}))))))
and run the tests:
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.
In this example I used fixed-event-window
because I couldn't figure out how to make fixed-time-window work in a unit test in riemann. I think it's taken long enough to figure that out that I thought you would be interested in seeing the solution thus far. If you change it to a fixed-time-window it will work the same, except the tests won't run.