Skip to content

Commit

Permalink
Prometheus label / body only support some char. (#747)
Browse files Browse the repository at this point in the history
* Prometheus label / body  only support some char.

* Removing garbage
  • Loading branch information
shinji62 authored and jamtur01 committed Dec 7, 2016
1 parent 998dad9 commit d1e6448
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/riemann/prometheus.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(defn replace-disallowed
"Replaces all existence of disallowed characters with underscore."
[field]
(str/escape field {\space "_", \. "_", \: "_" \- "_"}))
(str/replace (str/replace field #"[^a-zA-Z0-9_]" "_") #"[_]{2,}" "_"))

(defn generate-metricname
"Generates the metric name as per prometheus specification."
Expand All @@ -31,7 +31,7 @@
"Creates a Prometheus label out of a Riemann event."
[filtered-event]
(->> filtered-event
(map #(if-not (nil? (second %)) (str "/" (name (first %)) "/" (second %))))
(map #(if-not (nil? (second %)) (str "/" (replace-disallowed (name (first %))) "/" (second %))))
(str/join "")))

(defn filter-event
Expand Down Expand Up @@ -103,4 +103,4 @@
(let [url (generate-url opts event)
datapoint (generate-datapoint event)]
(if-not (nil? datapoint)
(post-datapoint url datapoint))))))
(post-datapoint url datapoint))))))
22 changes: 22 additions & 0 deletions test/riemann/prometheus_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,25 @@
:conn-timeout 5000
:content-type :json
:throw-entire-message? true}])))))

(deftest ^:prometheus prometheus-test-with-disallow-caractere
(with-mock [calls client/post]
(let [d (prometheus/prometheus {:host "localhost"
:exclude-fields #{:service :metric :tags :time :ttl :state}})]

(testing "an event with a custom field")
(d {:host "testhost"
:service "testservice :scheduling (ms)"
:metric 42
:time 123456789
:state "ok"
:tags ["tag1","tag2"]
:client-test "X-Riemann-Client"})
(is (= 1 (count @calls)))
(is (= (vec (last @calls))
["http://localhost:9091/metrics/job/riemann/host/testhost/instance/localhost/tags/tag1,tag2/host/testhost/client_test/X-Riemann-Client"
{:body "testservice_scheduling_ms_ 42.0\n"
:socket-timeout 5000
:conn-timeout 5000
:content-type :json
:throw-entire-message? true}])))))

0 comments on commit d1e6448

Please sign in to comment.