-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #276 from gojek/feature/EV-37
[Feature/EV 37] Integrate Native Prometheus client in Ziggurat
- Loading branch information
Showing
11 changed files
with
170 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ name: Ziggurat CI | |
|
||
on: | ||
push: | ||
branches: | ||
tags: | ||
pull_request: | ||
branches: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,7 @@ pom.xml | |
.factorypath | ||
.project | ||
.settings | ||
.clj-kondo/* | ||
.calva/* | ||
.lsp/* | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(ns ziggurat.prometheus-exporter | ||
(:require [iapetos.core :as prometheus] | ||
[iapetos.standalone :as standalone] | ||
[ziggurat.config :refer [prometheus-config]])) | ||
|
||
(def registry | ||
(atom (prometheus/collector-registry))) | ||
|
||
(defn get-metric-value [kw-metric labels] | ||
(prometheus/value (@registry kw-metric labels))) | ||
|
||
(defn register-collecter-if-not-exist [collector metric-name label-vec] | ||
(let [counter (@registry metric-name {:labels label-vec})] | ||
(when-not counter | ||
(let [new-counter (collector metric-name {:labels label-vec}) | ||
new-registry (prometheus/register @registry new-counter)] | ||
(reset! registry new-registry) | ||
new-counter)))) | ||
|
||
(defn update-counter [namespace metric tags value] | ||
(let [metric-name {:name metric :namespace namespace} | ||
label-vec (vec (keys tags))] | ||
(register-collecter-if-not-exist prometheus/gauge metric-name label-vec) | ||
(prometheus/inc (@registry metric-name tags) value) | ||
metric-name)) | ||
|
||
(defn report-histogram [namespace metric tags integer-value] | ||
(let [metric-name {:name metric :namespace namespace} | ||
label-vec (vec (keys tags))] | ||
(register-collecter-if-not-exist prometheus/histogram metric-name label-vec) | ||
(prometheus/observe (@registry metric-name tags) integer-value))) | ||
|
||
(defonce httpd (atom nil)) | ||
|
||
(defn start-http-server [] | ||
(when-not @httpd | ||
(reset! httpd (standalone/metrics-server @registry | ||
{:port | ||
(get (prometheus-config) | ||
:port 8002)})))) | ||
|
||
(defn stop-http-server [] | ||
(when-let [server @httpd] | ||
(.close server) | ||
(reset! httpd nil))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
(ns ziggurat.prometheus-exporter-test | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[ziggurat.prometheus-exporter :as prometheus-exporter] | ||
[iapetos.core :as prometheus])) | ||
|
||
(defn clear-prometheus-registry [] | ||
(reset! prometheus-exporter/registry (prometheus/collector-registry))) | ||
|
||
(deftest test-register-collecter-if-not-exist | ||
(testing "Should register gauge if it does not exist" | ||
(clear-prometheus-registry) | ||
(let [tags {:actor-topic "food" :app_name "delivery"} | ||
metric-name {:name "test-gauge" :namespace "bar"} | ||
label-vec (vec (keys tags))] | ||
(is (nil? (@prometheus-exporter/registry metric-name)) "Gauge should not exist initially") | ||
(prometheus-exporter/register-collecter-if-not-exist prometheus/gauge metric-name label-vec) | ||
(is (not (nil? (@prometheus-exporter/registry metric-name))) "Gauge should be registered in the registry")) | ||
|
||
(testing "Gauge not registered if it exists" | ||
(clear-prometheus-registry) | ||
(let [tags {:actor-topic "food" :app_name "delivery"} | ||
metric-name "test-gauge" | ||
label-vec (vec (keys tags)) | ||
existing-gauge (prometheus-exporter/register-collecter-if-not-exist prometheus/gauge metric-name label-vec)] | ||
(let [expected-nil (prometheus-exporter/register-collecter-if-not-exist prometheus/gauge metric-name label-vec)] | ||
(is (not (nil? existing-gauge))) | ||
(is (nil? expected-nil) "Should retrieve the existing gauge")))))) | ||
|
||
(deftest test-update-counter | ||
(testing "Updating counter can increments its value" | ||
(clear-prometheus-registry) | ||
(let [namespace "test_namespace" | ||
metric "test_metric" | ||
old_value 5.0 | ||
increment 2 | ||
new-value (+ old_value increment) | ||
tags {:some "label"} | ||
_ (prometheus-exporter/update-counter namespace metric tags old_value)] | ||
(prometheus-exporter/update-counter namespace metric tags increment) | ||
(@prometheus-exporter/registry (assoc tags :name metric :namespace namespace)) | ||
(let [gauge-value (prometheus-exporter/get-metric-value (keyword (str namespace "/" metric)) tags)] | ||
(is (= new-value gauge-value) "Gauge value should be incremented")))) | ||
|
||
(testing "Updating counter can decrement its value" | ||
(clear-prometheus-registry) | ||
(let [namespace "test_namespace" | ||
metric "test_metric" | ||
old_value 5.0 | ||
decrement -2 | ||
new-value (+ old_value decrement) | ||
tags {:some "label"} | ||
_ (prometheus-exporter/update-counter namespace metric tags old_value)] | ||
(prometheus-exporter/update-counter namespace metric tags decrement) | ||
(@prometheus-exporter/registry (assoc tags :name metric :namespace namespace)) | ||
(let [gauge-value (prometheus-exporter/get-metric-value (keyword (str namespace "/" metric)) tags)] | ||
(is (= new-value gauge-value) "Gauge value should be incremented"))))) | ||
|
||
(deftest test-report-histogram | ||
(testing "should register histogram when it does not exist" | ||
(clear-prometheus-registry) | ||
(let [namespace "test_namespace" | ||
metric "test_metric" | ||
value 5.0 | ||
tags {:some "label"} | ||
label (assoc tags :name metric :namespace namespace)] | ||
(is (nil? (@prometheus-exporter/registry label))) | ||
(prometheus-exporter/report-histogram namespace metric tags value) | ||
(is (not (nil? (@prometheus-exporter/registry label)))) | ||
(let [gauge-value (prometheus-exporter/get-metric-value (keyword (str namespace "/" metric)) tags)] | ||
(is (= (gauge-value :sum) 5.0)) | ||
(is (= (gauge-value :count) 1.0))))) | ||
(testing "should update histogram when it exist and increment the count" | ||
(clear-prometheus-registry) | ||
(let [namespace "test_namespace" | ||
metric "test_metric" | ||
value 5.0 | ||
tags {:some "label"} | ||
label (assoc tags :name metric :namespace namespace) | ||
_ (prometheus-exporter/report-histogram namespace metric tags value)] | ||
(is (not (nil? (@prometheus-exporter/registry label)))) | ||
(prometheus-exporter/report-histogram namespace metric tags value) | ||
(let [gauge-value (prometheus-exporter/get-metric-value (keyword (str namespace "/" metric)) tags)] | ||
(is (= (gauge-value :count) 2.0)))))) |