From c18a4c9a8b65238c18b5d265e14cdb83c97a87df Mon Sep 17 00:00:00 2001 From: bom Date: Fri, 30 Aug 2024 15:08:45 +0200 Subject: [PATCH] Add spec and test for monitoring --- src/dda/backup/monitoring/domain.clj | 46 +++++++++++++++++++++- test/dda/backup/monitoring/domain_test.clj | 14 +++---- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/dda/backup/monitoring/domain.clj b/src/dda/backup/monitoring/domain.clj index 0d12fca..ac80de2 100644 --- a/src/dda/backup/monitoring/domain.clj +++ b/src/dda/backup/monitoring/domain.clj @@ -1 +1,45 @@ -(ns dda.backup.monitoring.domain) \ No newline at end of file +(ns dda.backup.monitoring.domain + (:require + [dda.backup.restic.domain :as rd] + [orchestra.core :refer [defn-spec]] + [clojure.spec.alpha :as s])) + +(s/def ::type #{"gauge"}) +(s/def ::labels map?) +(s/def ::metric-value pos?) +(s/def ::metric-element (s/keys :req-un [::labels ::metric-value])) +(s/def ::value (s/+ ::metric-element)) +(s/def ::metric (s/keys :req-un [::type + ::value])) +(s/def ::backup-size (s/spec ::metric-value)) + +(s/def ::docstring string?) +(s/def ::__name__ string?) +(s/def ::baseLabels (s/keys :req-un [::__name__])) + +(s/def ::monitoring-data (s/keys :req-un [::baseLabels + ::docstring + ::metric])) + +(s/def ::monitoring-data-seq (s/+ ::monitoring-data)) + +(s/def ::fqdn string?) + + +(s/def ::monitoring-config + (s/keys :req-un [::fqdn + ::backup-size + ::rd/restic-repository + ::rd/backup-path])) + +(defn-spec monitoring-data ::monitoring-data-seq + [config ::monitoring-config] + (let [{:keys [fqdn backup-size restic-repository backup-path]} config] + [{:baseLabels {:__name__ "backup_size"}, + :docstring "Backup size in MB", + :metric {:type "gauge", + :value [{:labels + {:app_name "c4k-forgejo" + :fqdn fqdn + :restic_repo (str restic-repository "/" backup-path)}, + :value backup-size}]}}])) \ No newline at end of file diff --git a/test/dda/backup/monitoring/domain_test.clj b/test/dda/backup/monitoring/domain_test.clj index b19182a..4602e86 100644 --- a/test/dda/backup/monitoring/domain_test.clj +++ b/test/dda/backup/monitoring/domain_test.clj @@ -4,19 +4,17 @@ [clojure.spec.test.alpha :as st] [dda.backup.monitoring.domain :as cut])) -(deftest should-calculate-backup-files-command +(deftest should-generate-monitoring-data (is (= [{:baseLabels {:__name__ "backup_size"}, :docstring "Backup size in MB", :metric {:type "gauge", :value [{:labels {:app_name "c4k-forgejo" :fqdn "repo.prod.meissa.de" - :restic_repo "s3://..."}, + :restic_repo "repo/dir-at-repo"}, :value 123}]}}] - (cut/backup-files-command {:restic-repository "repo" - :backup-path "dir-at-repo" - :execution-directory "dir-to-backup" - :days-to-keep 39 - :months-to-keep 3 - :files ["file2" "file2"]})))) + (cut/monitoring-data {:fqdn "repo.prod.meissa.de" + :restic-repository "repo" + :backup-path "dir-at-repo" + :backup-size 123}))))