-
Notifications
You must be signed in to change notification settings - Fork 3
/
alerta.clj
69 lines (61 loc) · 1.93 KB
/
alerta.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
; -*- mode: clojure; -*-
; vim: filetype=clojure
(ns alerta
"Forwards events to Alerta."
(:require [clj-http.client :as http]
[cheshire.core :as json])
(:use [riemann.common :only [localhost]]))
(def version "5.0.1")
(defn- auth-request
[req]
(if-let [api-key (:api-key req)]
(-> req (dissoc :api-key)
(assoc-in [:headers "authorization"]
(str "Key " api-key)))
req))
(defn- post
"POST to the Alerta API."
[url body options]
(http/post
url
(auth-request
(merge
{:body (json/generate-string body)
:content-type :json
:accept :json
:socket-timeout 5000
:conn-timeout 5000
:throw-entire-message? true}
options))))
(defn- format-event
"Formats an event for Alerta."
[event]
{:resource (:host event)
:event (get event :event (:service event))
:environment (get event :environment "Production")
:severity (:state event)
:service [(get event :grid "Platform")]
:group (get event :group "Performance")
:value (:metric event)
:text (:description event)
:tags (into [] (:tags event))
:origin (str "riemann/" (localhost))
:eventType "performanceAlert"
:rawData event})
(defn alert
"Forwards events to Alerta.
```clojure
(let [alert (alerta/alert {:endpoint \"...\"
:api-key \"...\"
:debug true})]
(changed-state alert))
```"
[opts]
(let [opts (merge {:endpoint "http://localhost:8080"} opts)]
(fn [event] (post (str (:endpoint opts) "/alert") (format-event event) opts))))
(defn heartbeat
"Sends heartbeat to Alerta."
[opts]
(let [opts (merge {:endpoint "http://localhost:8080"} opts)
hb-event {:origin (str "riemann/" (localhost)) :tags [version]}]
(fn [event] (post (str (:endpoint opts) "/heartbeat") hb-event opts))))