initial monitoring ability

This commit is contained in:
Michael Jerger 2025-03-11 13:30:13 +01:00
parent 82f7e86a39
commit ef8439342b
5 changed files with 75 additions and 2 deletions
deps.edn
src/dda/backup
test/dda/backup/monitoring

View file

@ -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"}}
;; ---------------------------------------------------------
;; ---------------------------------------------------------

View file

@ -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?
@ -31,3 +32,8 @@
(println c))
(when-not dry-run
(apply t/shell c)))))
(defn-spec post! nil?
[url string?
content string?]
(http/post url {:body content}))

View file

@ -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)))))))

View file

@ -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")))

View file

@ -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"}}))))