Skip to content

Commit

Permalink
escape metric help and label values
Browse files Browse the repository at this point in the history
  • Loading branch information
deadtrickster committed Apr 15, 2016
1 parent 42eded9 commit 664ac6e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions prometheus.formats.text.asd
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
:serial t
:components
((:file "package")
(:file "escape")
(:file "text"))))
:description "Prometheus.io Client Text format")
30 changes: 30 additions & 0 deletions src/formats/text/escape.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(in-package #:prometheus.formats.text)

;; http://cl-cookbook.sourceforge.net/strings.html#manip
(defun replace-all (string part replacement &key (test #'char=))
"Returns a new string in which all the occurences of the part
is replaced with replacement."
(with-output-to-string (out)
(loop with part-length = (length part)
for old-pos = 0 then (+ pos part-length)
for pos = (search part string
:start2 old-pos
:test test)
do (write-string string out
:start old-pos
:end (or pos (length string)))
when pos do (write-string replacement out)
while pos)))

(defun escape-metric-help (help)
(replace-all (replace-all help "\\" "\\\\")
#.(string #\Newline) "\\n"))

(defun escape-label-value (help)
(replace-all
(replace-all
(replace-all
help
"\\" "\\\\")
#.(string #\Newline) "\\n")
"\"" "\\\""))
8 changes: 5 additions & 3 deletions src/formats/text/text.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

(defun print-sample-line (stream name lables lvalues value)
(let ((*read-default-float-format* 'double-float))
(format stream "~a~@[{~{~{~(~A~)=\"~A\"~}~^, ~}}~] ~a~%" name (mapcar #'list lables lvalues) (if (floatp value) (coerce value 'double-float) value))))
(format stream "~a~@[{~{~{~(~A~)=\"~A\"~}~^, ~}}~] ~a~%" name (mapcar (lambda (lname lvalue)
(list lname (if (stringp lvalue)
(escape-label-value lvalue)
lvalue))) lables lvalues) (if (floatp value) (coerce value 'double-float) value))))

(defgeneric metric-to-text (metric name family-lables stream)
(:method ((metric prom:simple-metric) name family-lables stream)
Expand Down Expand Up @@ -48,13 +51,12 @@
(prom:metric-labels metric)
(prom:metric-value metric))))


(defgeneric metric-family-to-text (mf stream)
(:method ((mf prom::metric-family) stream)
(let ((name (prom::metric-family-name mf))
(labels (prom:metric-family-labels mf)))
(format stream "# TYPE ~a ~a~%" name (prom:metric-family-type mf))
(format stream "# HELP ~a ~a~%" name (prom:metric-family-help mf))
(format stream "# HELP ~a ~a~%" name (escape-metric-help (prom:metric-family-help mf)))
(loop for metric in (prom::get-metrics mf) do
(metric-to-text metric name labels stream)))))

Expand Down
14 changes: 8 additions & 6 deletions t/formats/text/basic.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

(subtest "Basic test"
(with-fresh-registry
(let ((rc (prom:make-counter :name "requests_counter" :help "Hunchentoot requests counter" :labels '("type")))
(let ((rc (prom:make-counter :name "requests_counter" :help "Hunch\\entoot
requests counter" :labels '("type")))
(ic (prom:make-int-counter :name "irequests_counter" :help "Hunchentoot requests counter" :labels '("type")))
(tmg (prom:make-gauge :name "total_memory" :help "SBCL total memory"))
(h (prom:make-histogram :name "render_time" :help "qwe" :labels '("type") :buckets '(2 4 6)))
(s (prom:make-summary :name "traffic_summary" :help "traffic summary" :value 12)))
(prom:counter.inc rc :value 5 :labels '("get"))
(prom:counter.inc rc :value 12 :labels '("post"))
(prom:counter.inc rc :value 5 :labels '("ge\"t"))
(prom:counter.inc rc :value 12 :labels '("p\\os
t"))
(prom:counter.inc ic :value 5 :labels '("get"))
(prom:counter.inc ic :value 12 :labels '("post"))
(prom:gauge.set tmg 566)
Expand All @@ -20,9 +22,9 @@
(prom:histogram.observe h 4.5 :labels '("pdf"))
(prom:summary.observe s 43.3d0)
(is (prom.text:marshal) "# TYPE requests_counter counter
# HELP requests_counter Hunchentoot requests counter
requests_counter{type=\"post\"} 12
requests_counter{type=\"get\"} 5
# HELP requests_counter Hunch\\\\entoot\\nrequests counter
requests_counter{type=\"p\\\\os\\nt\"} 12
requests_counter{type=\"ge\\\"t\"} 5
# TYPE irequests_counter counter
# HELP irequests_counter Hunchentoot requests counter
irequests_counter{type=\"post\"} 12
Expand Down

0 comments on commit 664ac6e

Please sign in to comment.