Skip to content

Instantly share code, notes, and snippets.

@asemt
Last active September 23, 2017 05:39
Show Gist options
  • Save asemt/5994365 to your computer and use it in GitHub Desktop.
Save asemt/5994365 to your computer and use it in GitHub Desktop.

Persist specific events into Redis datastore:

  1. Clone the Riemann Git repository (clones into directory riemann):
    $ git clone https://github.com/aphyr/riemann.git

  2. Checkout the last stable version (0.2.2 in our case):
    $ cd riemann
    $ git checkout tags/0.2.2

  3. Add Carmine dependency to riemann/project.clj:
    [com.taoensso/carmine "1.12.0"]

  4. Add the Redis persistence functionality to Riemann's config file riemann/config.riemann:

(ns riemann.config
  (:require [taoensso.carmine :as car]))

    ; Assuming a locally running Redis server listening on port 6379.
    (def pool         (car/make-conn-pool))
    (def spec-server1 (car/make-conn-spec))

    (defmacro wcar [& body] `(car/with-conn pool spec-server1 ~@body))

    (defn persist-event [& children]
      (fn [event]
        ; Persist event to a Redis list with name '<SERVICE>.new-events'.
        (try
          (wcar (car/lpush (str (:service event) ".new-events") event))
          (catch Exception e
            (error "Redis datastore not available.")))
        (call-rescue event children))
      )

It's then used this way in riemann/config.riemann:

; Persist events to Redis where service equals 'foo'.
    ; Exclude those 'foo' events where state 'expired'.
    ; Additionally add events to 'index'.
    (where
      (and
        (service "foo")
        (not (state "expired"))
        )
      (persist-event index))
  1. Try it out with a locally running Redis server:
    $ lein run
    Now it's time to generate some events containing :service "foo" so the where filter from above matches.
    If the events of service foo were successfully persisted into Redis, there should be a length greater than zero (3 in our case) in foo.new-events (check with Redis shell):
    $ redis-cli
    redis 127.0.0.1:6379> LLEN foo.new-events
    (integer) 3

If this modified version of Riemann should be deployed with the help of lein tar, then it's necessary to copy riemann/riemann.config into riemann/pkg first. This way the config file is then located in the etc sub directory after extracting the .tar.bz2 archive (created by lein tar) on the production machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment