diff --git a/deps.edn b/deps.edn index aa43141..f8a42d4 100644 --- a/deps.edn +++ b/deps.edn @@ -13,7 +13,8 @@ orchestra/orchestra {:mvn/version "2021.01.01-1"} aero/aero {:mvn/version "1.1.6"} cheshire/cheshire {:mvn/version "5.13.0"} - com.widdindustries/cljc.java-time {:mvn/version "0.1.21"}} + com.widdindustries/cljc.java-time {:mvn/version "0.1.21"} + org.babashka/http-client {:mvn/version "0.3.11"}} ;; --------------------------------------------------------- ;; --------------------------------------------------------- diff --git a/src/dda/backup/infrastructure.clj b/src/dda/backup/infrastructure.clj index 51677e7..f2a73f0 100644 --- a/src/dda/backup/infrastructure.clj +++ b/src/dda/backup/infrastructure.clj @@ -1,6 +1,7 @@ (ns dda.backup.infrastructure (:require [orchestra.core :refer [defn-spec]] [babashka.tasks :as t] + [babashka.http-client :as http] [dda.backup.core.domain :as core])) (defn-spec execute-out! string? @@ -30,4 +31,9 @@ (when debug (println c)) (when-not dry-run - (apply t/shell c))))) \ No newline at end of file + (apply t/shell c))))) + +(defn-spec post! nil? + [url string? + content string?] + (http/post url {:body content})) \ No newline at end of file diff --git a/src/dda/backup/monitoring.clj b/src/dda/backup/monitoring.clj new file mode 100644 index 0000000..fb5700e --- /dev/null +++ b/src/dda/backup/monitoring.clj @@ -0,0 +1,23 @@ +(ns dda.backup.monitoring + (:require + [orchestra.core :refer [defn-spec]] + [dda.backup.monitoring.domain :as domain] + [dda.backup.infrastructure :as i])) + +(def default {:url "http://prometheus-pushgateway.monitoring.svc.cluster.local:9091/metrics/job" + :namespace "default" + :metrics {:kube_job_status_active 0 + :kube_job_status_failed 1 + :kube_job_status_succeeded 0}}) + +(defn- config-w-defaults + [config] + (merge default config)) + +(defn-spec send-metrics! nil? + [config ::domain/config] + (let [config-2-use (config-w-defaults config) + {:keys [url name]} config-2-use] + (try + (i/post! (str url "/" name) (domain/collect-metrics config-2-use)) + (catch Exception e (println (str "Warn: unable to send log" (.getMessage e))))))) diff --git a/src/dda/backup/monitoring/domain.clj b/src/dda/backup/monitoring/domain.clj new file mode 100644 index 0000000..da18be5 --- /dev/null +++ b/src/dda/backup/monitoring/domain.clj @@ -0,0 +1,23 @@ +(ns dda.backup.monitoring.domain + (:require + [orchestra.core :refer [defn-spec]] + [clojure.spec.alpha :as s] + [clojure.string :as st])) + +(s/def ::url string?) +(s/def ::name string?) +(s/def ::namespace string?) +(s/def ::metrics map?) + +(s/def ::config (s/keys :req-un [::url ::name ::metrics ::namespace])) + +(defn-spec collect-metrics string? + [config ::config] + (let [{:keys [metrics namespace]} config] + (str + (->> metrics + (map (fn [entry] (str (name (key entry)) + "{namespace=" namespace "}" " " + (val entry)))) + (st/join "\n")) + "\n"))) \ No newline at end of file diff --git a/test/dda/backup/monitoring/domain_test.clj b/test/dda/backup/monitoring/domain_test.clj new file mode 100644 index 0000000..1ff2e94 --- /dev/null +++ b/test/dda/backup/monitoring/domain_test.clj @@ -0,0 +1,20 @@ +(ns dda.backup.monitoring.domain-test + (:require + [clojure.test :refer [deftest is are testing run-tests]] + [clojure.spec.test.alpha :as st] + [dda.backup.monitoring.domain :as cut])) + +(st/instrument `cut/collect-metrics) + +(deftest should-collect-metrics + (is (= "\n" + (cut/collect-metrics {:url "url" + :name "name" + :namespace "default" + :metrics {}}))) + (is (= "metric1{namespace=default} 1\nmetric2{namespace=default} text\n" + (cut/collect-metrics {:url "url" + :name "name" + :namespace "default" + :metrics {:metric1 1 + :metric2 "text"}}))))