diff --git a/src/spootnik/reporter/alert.clj b/src/spootnik/reporter/alert.clj index 88a604c..3506de0 100644 --- a/src/spootnik/reporter/alert.clj +++ b/src/spootnik/reporter/alert.clj @@ -6,12 +6,8 @@ (: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")) + (-send! [client alert]) + (-msend! [client alerts])) (s/def ::named (s/or :k ident? :s string?)) @@ -46,9 +42,6 @@ (defn ->alert [{:as alert :keys [name labels annotations generator-url delay-after-ms] :or {delay-after-ms (* 60 1000 15)}}] - (when-not (s/valid? :spootnik.reporter/alert alert) - (ex/ex-incorrect! "Invalid alert format" - (s/explain-data :spootnik.reporter/alert alert))) (let [t (Instant/now)] (cond-> {:startsAt (str t) :endsAt (str (.plusMillis t (* delay-after-ms))) @@ -67,12 +60,32 @@ (defrecord AlephClient []) +(s/def :spootnik.reporter.alert.client/server string?) +(s/def :spootnik.reporter.alert/client + (s/keys :req-un [:spootnik.reporter.alert.client/server])) + (extend-protocol Alert AlephClient - (msend! [client alerts] - @(http/post "/api/v2/alerts" + (-msend! [client alerts] + @(http/post (format "%s/api/v2/alerts" (:server client)) (into {:body (alerts-body alerts) :headers {"Content-Type" "application/json; charset=utf-8"}} client))) - (send! [client alert] - (msend! client [alert]))) + (-send! [client alert] + (-msend! client [alert]))) + +(defn send! + "Sends `alert`, conforming to `:spootnik.reporter/alert`, via `client` to + alertmanager instance" + [client alert] + (ex/assert-spec-valid :spootnik.reporter.alert/client client) + (ex/assert-spec-valid :spootnik.reporter/alert alert) + (-send! client alert)) + +(defn msend! + "Sends `alerts` in bulk, conforming to `:spootnik.reporter/alerts`, via `client` + to alertmanager instance" + [client alerts] + (ex/assert-spec-valid :spootnik.reporter.alert/client client) + (ex/assert-spec-valid :spootnik.reporter/alerts alerts) + (-msend! client alerts))