add prometheus middleware for httpd

This commit is contained in:
Simon Cruanes 2024-01-18 21:25:49 -05:00
parent da7a27552a
commit 9f9017f26a
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 40 additions and 20 deletions

View file

@ -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)

View file

@ -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

View file

@ -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