Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #11/clearing default registry #12

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
36 changes: 36 additions & 0 deletions test/iapetos/registry/default_registry_test.clj
Original file line number Diff line number Diff line change
@@ -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"}))))))
)