From 9f9017f26ac612e3134a10c185bc06b8ff5e4fe4 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 18 Jan 2024 21:25:49 -0500 Subject: [PATCH] add prometheus middleware for httpd --- src/prometheus/tiny_httpd_prometheus.ml | 34 ++++++++++++++++++------ src/prometheus/tiny_httpd_prometheus.mli | 18 +++++++------ tests/prometheus/t_prom.ml | 8 +++--- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/prometheus/tiny_httpd_prometheus.ml b/src/prometheus/tiny_httpd_prometheus.ml index 026af9db..d9157ddf 100644 --- a/src/prometheus/tiny_httpd_prometheus.ml +++ b/src/prometheus/tiny_httpd_prometheus.ml @@ -41,10 +41,10 @@ module Counter = struct bpf buf "%s%a %d\n" self.name emit_tags_ self.tags (A.get self.c); () - let[@inline] inc self = A.incr self.c - let[@inline] inc_by self n = ignore (A.fetch_and_add self.c n : int) - let[@inline] dec self = A.decr self.c - let[@inline] dec_by self n = ignore (A.fetch_and_add self.c (-n) : int) + let[@inline] incr self = A.incr self.c + let[@inline] incr_by self n = ignore (A.fetch_and_add self.c n : int) + let[@inline] decr self = A.decr self.c + let[@inline] decr_by self n = ignore (A.fetch_and_add self.c (-n) : int) end module Gauge = struct @@ -63,10 +63,10 @@ module Gauge = struct () let[@inline] set self x = A.set self.g x - let[@inline] inc self = A.incr self.g - let[@inline] inc_by self n = ignore (A.fetch_and_add self.g n : int) - let[@inline] dec self = A.decr self.g - let[@inline] dec_by self n = ignore (A.fetch_and_add self.g (-n) : int) + let[@inline] incr self = A.incr self.g + let[@inline] incr_by self n = ignore (A.fetch_and_add self.g n : int) + let[@inline] decr self = A.decr self.g + let[@inline] decr_by self n = ignore (A.fetch_and_add self.g (-n) : int) end module Registry = struct @@ -86,3 +86,21 @@ module Registry = struct end let global = Registry.create () + +module H = Tiny_httpd + +let http_middleware (reg : Registry.t) : H.Middleware.t = + let c_req = + Counter.create reg "tiny_httpd_requests" ~descr:"number of HTTP requests" + in + let c_err = + Counter.create reg "tiny_httpd_errors" ~descr:"number of HTTP errors" + in + + fun h : H.Middleware.handler -> + fun req ~resp : unit -> + Counter.incr c_req; + h req ~resp:(fun (response : H.Response.t) -> + let code = response.code in + if code < 200 || code >= 300 then Counter.incr c_err; + resp response) diff --git a/src/prometheus/tiny_httpd_prometheus.mli b/src/prometheus/tiny_httpd_prometheus.mli index 0cd603f8..b295f687 100644 --- a/src/prometheus/tiny_httpd_prometheus.mli +++ b/src/prometheus/tiny_httpd_prometheus.mli @@ -24,10 +24,10 @@ module Counter : sig (** A counter, monotonically increasing *) val create : Registry.t -> ?tags:tags -> ?descr:string -> string -> t - val inc : t -> unit - val inc_by : t -> int -> unit - val dec : t -> unit - val dec_by : t -> int -> unit + val incr : t -> unit + val incr_by : t -> int -> unit + val decr : t -> unit + val decr_by : t -> int -> unit end (** Gauges *) @@ -37,13 +37,15 @@ module Gauge : sig val create : Registry.t -> ?tags:tags -> ?descr:string -> string -> t val set : t -> int -> unit - val inc : t -> unit - val inc_by : t -> int -> unit - val dec : t -> unit - val dec_by : t -> int -> unit + val incr : t -> unit + val incr_by : t -> int -> unit + val decr : t -> unit + val decr_by : t -> int -> unit end (* TODO: module Histogram : sig end *) + +val http_middleware : Registry.t -> Tiny_httpd.Middleware.t diff --git a/tests/prometheus/t_prom.ml b/tests/prometheus/t_prom.ml index 8e9fe8b5..f8144b7b 100644 --- a/tests/prometheus/t_prom.ml +++ b/tests/prometheus/t_prom.ml @@ -8,16 +8,16 @@ let g1 = P.Gauge.create reg ~tags:[ "level", "max" ] "yolo_gauge" let () = print_endline "==== first try ===="; - P.Counter.inc_by c1 42; - P.Counter.inc c2; + P.Counter.incr_by c1 42; + P.Counter.incr c2; P.Gauge.set g1 2525; pf "```\n%s\n```\n" @@ P.Registry.emit_str reg let () = print_endline "==== second try===="; - P.Counter.inc_by c1 11; - P.Counter.inc c2; + P.Counter.incr_by c1 11; + P.Counter.incr c2; P.Gauge.set g1 42_000; pf "```\n%s\n```\n" @@ P.Registry.emit_str reg