Merge pull request 'introduce-monitoring' (#2) from introduce-monitoring into main
Reviewed-on: #2
This commit is contained in:
commit
76a0dae99c
5 changed files with 101 additions and 2 deletions
3
deps.edn
3
deps.edn
|
@ -13,7 +13,8 @@
|
||||||
orchestra/orchestra {:mvn/version "2021.01.01-1"}
|
orchestra/orchestra {:mvn/version "2021.01.01-1"}
|
||||||
aero/aero {:mvn/version "1.1.6"}
|
aero/aero {:mvn/version "1.1.6"}
|
||||||
cheshire/cheshire {:mvn/version "5.13.0"}
|
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"}}
|
||||||
;; ---------------------------------------------------------
|
;; ---------------------------------------------------------
|
||||||
|
|
||||||
;; ---------------------------------------------------------
|
;; ---------------------------------------------------------
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
"Intenden for internal use, represents the infrastructure layer."
|
"Intenden for internal use, represents the infrastructure layer."
|
||||||
(:require [orchestra.core :refer [defn-spec]]
|
(:require [orchestra.core :refer [defn-spec]]
|
||||||
[babashka.tasks :as t]
|
[babashka.tasks :as t]
|
||||||
|
[babashka.http-client :as http]
|
||||||
[dda.backup.core.domain :as core]))
|
[dda.backup.core.domain :as core]))
|
||||||
|
|
||||||
(defn-spec execute-out! string?
|
(defn-spec execute-out! string?
|
||||||
|
@ -31,4 +32,9 @@
|
||||||
(when debug
|
(when debug
|
||||||
(println c))
|
(println c))
|
||||||
(when-not dry-run
|
(when-not dry-run
|
||||||
(apply t/shell c)))))
|
(apply t/shell c)))))
|
||||||
|
|
||||||
|
(defn-spec post! nil?
|
||||||
|
[url string?
|
||||||
|
content string?]
|
||||||
|
(http/post url {:body content}))
|
49
src/dda/backup/monitoring.clj
Normal file
49
src/dda/backup/monitoring.clj
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
(ns dda.backup.monitoring
|
||||||
|
(:require
|
||||||
|
[orchestra.core :refer [defn-spec]]
|
||||||
|
[clojure.spec.alpha :as s]
|
||||||
|
[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 0
|
||||||
|
:kube_job_status_succeeded 0}})
|
||||||
|
|
||||||
|
(s/def ::config (s/keys :req-un [::name]
|
||||||
|
:opt-un [::url ::metrics ::namespace]))
|
||||||
|
|
||||||
|
(defn- config-w-defaults
|
||||||
|
[config]
|
||||||
|
(merge default config))
|
||||||
|
|
||||||
|
(defn-spec send-metrics! nil?
|
||||||
|
[config ::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)))))))
|
||||||
|
|
||||||
|
(defn-spec backup-start-metrics! nil?
|
||||||
|
[config ::config]
|
||||||
|
(send-metrics!
|
||||||
|
{:name "backup"
|
||||||
|
:metrics {:kube_job_status_active 1
|
||||||
|
:kube_job_status_start_time (long (/ (System/currentTimeMillis) 1000))}}))
|
||||||
|
|
||||||
|
(defn-spec backup-success-metrics! nil?
|
||||||
|
[config ::config]
|
||||||
|
(send-metrics!
|
||||||
|
{:name "backup"
|
||||||
|
:metrics {:kube_job_status_succeeded 1
|
||||||
|
:kube_job_status_completion_time (long (/ (System/currentTimeMillis) 1000))}}))
|
||||||
|
|
||||||
|
(defn-spec backup-fail-metrics! nil?
|
||||||
|
[config ::config]
|
||||||
|
(send-metrics!
|
||||||
|
{:name "backup"
|
||||||
|
:metrics {:kube_job_status_failed 1
|
||||||
|
:kube_job_status_completion_time (long (/ (System/currentTimeMillis) 1000))}}))
|
||||||
|
|
23
src/dda/backup/monitoring/domain.clj
Normal file
23
src/dda/backup/monitoring/domain.clj
Normal 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")))
|
20
test/dda/backup/monitoring/domain_test.clj
Normal file
20
test/dda/backup/monitoring/domain_test.clj
Normal 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"}}))))
|
Loading…
Reference in a new issue