refactor to dry

This commit is contained in:
Michael Jerger 2024-02-20 16:14:15 +01:00
parent a14b7640da
commit 8626b91b16
4 changed files with 10 additions and 40 deletions

View file

@ -3,46 +3,22 @@
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
#?(:clj [orchestra.core :refer [defn-spec]] #?(:clj [orchestra.core :refer [defn-spec]]
:cljs [orchestra.core :refer-macros [defn-spec]]) :cljs [orchestra.core :refer-macros [defn-spec]])
[clojure.string :as str]
[dda.c4k-common.yaml :as yaml] [dda.c4k-common.yaml :as yaml]
[dda.c4k-common.predicate :as cp]
[dda.c4k-common.monitoring.monitoring-internal :as int])) [dda.c4k-common.monitoring.monitoring-internal :as int]))
(s/def ::grafana-cloud-user cp/bash-env-string?) (s/def ::grafana-cloud-user ::int/grafana-cloud-user)
(s/def ::grafana-cloud-password cp/bash-env-string?) (s/def ::grafana-cloud-password ::int/grafana-cloud-password)
(s/def ::grafana-cloud-url string?) (s/def ::grafana-cloud-url ::int/grafana-cloud-url)
(s/def ::cluster-name string?) (s/def ::cluster-name ::int/cluster-name)
(s/def ::cluster-stage cp/stage?) (s/def ::cluster-stage ::int/cluster-stage)
(s/def ::node-regex string?)
(s/def ::traefik-regex string?)
(s/def ::kube-state-regex string?)
(s/def ::mon-cfg (s/keys :req-un [::grafana-cloud-url (s/def ::mon-cfg (s/keys :req-un [::grafana-cloud-url
::cluster-name ::cluster-name
::cluster-stage])) ::cluster-stage]))
(s/def ::mon-auth (s/keys :req-un [::grafana-cloud-user (s/def ::mon-auth (s/keys :req-un [::grafana-cloud-user
::grafana-cloud-password])) ::grafana-cloud-password]))
(s/def ::filter-regex (s/keys :req-un [::node-regex
::traefik-regex
::kube-state-regex]))
(def metric-regex {:node-regex (def filter-regex-string int/filter-regex-string)
(str "node_cpu_sec.+|node_load[0-9]+|node_memory_Buf.*|node_memory_Mem.*|"
"node_memory_Cached.*|node_disk_[r,w,i].*|node_filesystem_[s,a].*|"
"node_network_receive_bytes_total|node_network_transmit_bytes_total")
:traefik-regex (str "traefik_entrypoint_.*_total|"
"traefik_entrypoint_.*_seconds_count|"
"traefik_router_.*_total|"
"traefik_router_.*_seconds_count|"
"traefik_service_.*_total|"
"traefik_service_.*_seconds_count|"
"traefik_tls_certs_not_after")
:kube-state-regex (str "kube_pod_container_status_restarts_total|"
"kube_pod_status_reason|kube_node_status_capacity|kube_node_status_allocatable|"
"kube_cronjob_status_active|kube_job_status_failed")})
(def filter-regex-string
(str/join "|" (vals metric-regex)))
(defn-spec generate seq? (defn-spec generate seq?

View file

@ -14,17 +14,11 @@
(s/def ::grafana-cloud-url string?) (s/def ::grafana-cloud-url string?)
(s/def ::cluster-name string?) (s/def ::cluster-name string?)
(s/def ::cluster-stage cp/stage?) (s/def ::cluster-stage cp/stage?)
(s/def ::node-regex string?)
(s/def ::traefik-regex string?)
(s/def ::kube-state-regex string?)
(s/def ::mon-cfg (s/keys :req-un [::grafana-cloud-url (s/def ::mon-cfg (s/keys :req-un [::grafana-cloud-url
::cluster-name ::cluster-name
::cluster-stage])) ::cluster-stage]))
(s/def ::mon-auth (s/keys :req-un [::grafana-cloud-user (s/def ::mon-auth (s/keys :req-un [::grafana-cloud-user
::grafana-cloud-password])) ::grafana-cloud-password]))
(s/def ::filter-regex (s/keys :req-un [::node-regex
::traefik-regex
::kube-state-regex]))
(def metric-regex {:node-regex (def metric-regex {:node-regex
(str "node_cpu_sec.+|node_load[0-9]+|node_memory_Buf.*|node_memory_Mem.*|" (str "node_cpu_sec.+|node_load[0-9]+|node_memory_Buf.*|node_memory_Mem.*|"
@ -44,7 +38,7 @@
(def filter-regex-string (def filter-regex-string
(str/join "|" (vals metric-regex))) (str/join "|" (vals metric-regex)))
(defn-spec generate-prometheus-config cp/map-or-seq? (defn-spec generate-prometheus-config map?
[config ::mon-cfg [config ::mon-cfg
auth ::mon-auth] auth ::mon-auth]
(let [{:keys [grafana-cloud-url cluster-name cluster-stage]} config (let [{:keys [grafana-cloud-url cluster-name cluster-stage]} config
@ -63,7 +57,7 @@
grafana-cloud-password) grafana-cloud-password)
(cm/replace-all-matching-values-by-new-value "FILTER_REGEX" filter-regex-string)))) (cm/replace-all-matching-values-by-new-value "FILTER_REGEX" filter-regex-string))))
(defn-spec generate-config cp/map-or-seq? (defn-spec generate-config map?
[config ::mon-cfg [config ::mon-cfg
auth ::mon-auth] auth ::mon-auth]
(-> (->

View file

@ -1,8 +1,8 @@
(ns dda.c4k-common.monitoring-regex-test (ns dda.c4k-common.monitoring.monitoring-regex-test
(:require (:require
[clojure.test :refer [deftest is are testing run-tests]] [clojure.test :refer [deftest is are testing run-tests]]
[data-test :refer :all] [data-test :refer :all]
[dda.c4k-common.monitoring :as cut])) [dda.c4k-common.monitoring.monitoring-internal :as cut]))
(defn filter-by-regex (defn filter-by-regex
[regex-str collection] [regex-str collection]