From 1f1964491b1169119e7745ae85c4cebc861363d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Sv=C3=A4rd?= Date: Tue, 10 Oct 2017 23:13:17 +0200 Subject: [PATCH 1/2] Using CollectorRegistry.clear to re-initialize default registry - seems to work _although_ - underlying registry will be wiped - any references to old "default" registries lying around will be invald --- .../registry/default_registry_test.clj | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/iapetos/registry/default_registry_test.clj diff --git a/test/iapetos/registry/default_registry_test.clj b/test/iapetos/registry/default_registry_test.clj new file mode 100644 index 0000000..d90ad84 --- /dev/null +++ b/test/iapetos/registry/default_registry_test.clj @@ -0,0 +1,36 @@ +(ns iapetos.registry.default_registry_test + (:require [iapetos.registry :as registry] + [iapetos.core :as prometheus] + [iapetos.collector.fn :as fn] + [clojure.test :refer [deftest is testing]]) + (:import [io.prometheus.client CollectorRegistry])) + +(defn init-default-registry [] + (do + (-> registry/default + ^CollectorRegistry (registry/raw) + (.clear)) + (-> registry/default + ;; ...other initializers or registrations + (fn/initialize)))) + +(defn test-fn [] + "EHLO") + +(deftest default-registry + (is (some? (registry/raw registry/default))) + + (testing "default registry can be initialized a first time" + (let [default-registry (init-default-registry)] + (is (some? default-registry)) + (fn/instrument! default-registry #'test-fn) + (is (string? (test-fn))))) + + (testing "default registry can be initialized a second time" + (let [default-registry (init-default-registry)] + (is (some? default-registry)) + (is (some? (prometheus/value default-registry :fn/duration-seconds))) + (fn/instrument! default-registry #'test-fn {:fn-name "test-fn"}) + (is (string? (test-fn))) + (is (= 1.0 (:count (prometheus/value default-registry :fn/duration-seconds {:fn "test-fn"})))))) + ) From 0b9213a4605af8093df1d6127c1ac157d469d3d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Sv=C3=A4rd?= Date: Tue, 10 Oct 2017 23:25:06 +0200 Subject: [PATCH 2/2] Added caveat to readme --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index f27b6a1..ab19164 100644 --- a/README.md +++ b/README.md @@ -322,6 +322,33 @@ This is particularly useful for applications that do not expose an HTTP port themselves but shall still be scraped by Prometheus. By default, metrics will be exposed at `/metrics`. +### Caveats + +#### Using the default registry and reload/refresh + +Many prometheus integration prefer the default registry +(`CollectorRegistry/defaultRegistry`) and cannot be provoked to do +otherwise. Using these and also at the same time using a workflow like +[reloaded](http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded) +may cause the _underlying_ default registry to be initialized multiple +times. A workaround is to use prometheus own `CollectorRegistry.clear` +as a first step in initialization to make sure the default registry is +clean and reusable. This will (of course) completely wipe the current +state of the underlying `CollectorRegistry/defaultRegistry`. + +An example: +``` +(defonce my-default-registry [] + (do + (-> registry/default + ^CollectorRegistry (registry/raw) + (.clear)) + (-> registry/default + ;; initializers or registrations + ;; like... + (fn/initialize)))) +``` + ## License ```