diff --git a/prometheus.pushgateway.asd b/prometheus.pushgateway.asd new file mode 100644 index 0000000..8a37a90 --- /dev/null +++ b/prometheus.pushgateway.asd @@ -0,0 +1,14 @@ +(asdf:defsystem :prometheus.pushgateway + :serial t + :version "0.1" + :licence "MIT" + :depends-on ("prometheus" + "prometheus.formats.text" + "drakma") + :author "Ilya Khaprov " + :components ((:module "src/pushgateway" + :serial t + :components + ((:file "package") + (:file "pushgateway")))) + :description "Prometheus.io Pushgateway client") diff --git a/src/pushgateway/package.lisp b/src/pushgateway/package.lisp new file mode 100644 index 0000000..76c0139 --- /dev/null +++ b/src/pushgateway/package.lisp @@ -0,0 +1,11 @@ +(in-package #:cl-user) + +(defpackage #:prometheus.pushgateway + (:use #:cl #:alexandria) + (:nicknames #:prom.pushgateway) + (:shadow #:push + #:replace + #:delete) + (:export #:push + #:replace + #:delete)) diff --git a/src/pushgateway/pushgateway.lisp b/src/pushgateway/pushgateway.lisp new file mode 100644 index 0000000..843f705 --- /dev/null +++ b/src/pushgateway/pushgateway.lisp @@ -0,0 +1,26 @@ +(in-package #:prometheus.pushgateway) + +(define-constant +default-pushgateway-address+ "localhost:9091" :test #'equal) + +(defun http-request (gateway method job grouping-key content-type body) + (multiple-value-bind (body status-code headers) + (drakma:http-request (format nil "http://~a/metrics/job/~a~:[~;~:*/~{~a~^/~}~]" gateway job grouping-key) + :method method + :content-type content-type + :content body) + (if (>= status-code 400) + (error "Error talking to pushgateway. Response Code: ~a, body: ~a, headers: ~a" status-code body headers)))) + +(defun push (job &key (gateway +default-pushgateway-address+) + (registry prom:*default-registry*) + (grouping-key)) + (http-request gateway :post job grouping-key prom.text:+content-type+ (prom.text:marshal registry))) + +(defun replace (job &key (gateway +default-pushgateway-address+) + (registry prom:*default-registry*) + (grouping-key)) + (http-request gateway :put job grouping-key prom.text:+content-type+ (prom.text:marshal registry))) + +(defun delete (job &key (gateway +default-pushgateway-address+) + (grouping-key)) + (http-request gateway :push job grouping-key prom.text:+content-type+ nil))