Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenet committed Jan 28, 2025
1 parent 0e72839 commit 4fa43bf
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/spootnik/reporter/alert.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
[jsonista.core :as json])
(:import (java.time Instant)))

(defprotocol Alert
(send! [client alert]
"Sends `alert`, conforming to `:spootnik.reporter/alert`, via `client` to
alertmanager instance")
(msend! [client alerts]
"Sends `alerts` in bulk, conforming to `:spootnik.reporter/alerts`, via
`client` to alertmanager instance"))

(s/def ::named (s/or :k ident? :s string?))

(s/def :spootnik.reporter.alert.annotation/key ::named)
Expand Down Expand Up @@ -33,6 +41,8 @@
:spootnik.reporter.alert/generator-url
:spootnik.reporter.alert.options/delay-after-ms]))

(s/def :spootnik.reporter/alerts (s/coll-of :spootnik.reporter/alert :min-count 1))

(defn ->alert
[{:as alert :keys [name labels annotations generator-url delay-after-ms]
:or {delay-after-ms (* 60 1000 15)}}]
Expand All @@ -50,9 +60,15 @@
generator-url
(assoc :generatorURL generator-url))))

(defn send!
([alert opts]
(http/post "/api/v2/alerts"
(into {:body (json/write-value-as-string
{:alerts [(->alert (into alert opts))]})}
opts))))
(defrecord AlephClient [])

(extend-protocol Alert
AlephClient
(msend! [client alerts]
@(http/post "/api/v2/alerts"
(into {:body (json/write-value-as-string
{:alerts (map (fn [alert] (->alert alert)) alerts)})
:headers {"Content-Type" "application/json; charset=utf-8"}}
client)))
(send! [client alert]
(msend! client [alert])))

0 comments on commit 4fa43bf

Please sign in to comment.