mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 19:25:32 -05:00
add prometheus middleware for httpd
This commit is contained in:
parent
da7a27552a
commit
9f9017f26a
3 changed files with 40 additions and 20 deletions
|
|
@ -41,10 +41,10 @@ module Counter = struct
|
||||||
bpf buf "%s%a %d\n" self.name emit_tags_ self.tags (A.get self.c);
|
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] incr self = A.incr self.c
|
||||||
let[@inline] inc_by self n = ignore (A.fetch_and_add self.c n : int)
|
let[@inline] incr_by self n = ignore (A.fetch_and_add self.c n : int)
|
||||||
let[@inline] dec self = A.decr self.c
|
let[@inline] decr self = A.decr self.c
|
||||||
let[@inline] dec_by self n = ignore (A.fetch_and_add self.c (-n) : int)
|
let[@inline] decr_by self n = ignore (A.fetch_and_add self.c (-n) : int)
|
||||||
end
|
end
|
||||||
|
|
||||||
module Gauge = struct
|
module Gauge = struct
|
||||||
|
|
@ -63,10 +63,10 @@ module Gauge = struct
|
||||||
()
|
()
|
||||||
|
|
||||||
let[@inline] set self x = A.set self.g x
|
let[@inline] set self x = A.set self.g x
|
||||||
let[@inline] inc self = A.incr self.g
|
let[@inline] incr self = A.incr self.g
|
||||||
let[@inline] inc_by self n = ignore (A.fetch_and_add self.g n : int)
|
let[@inline] incr_by self n = ignore (A.fetch_and_add self.g n : int)
|
||||||
let[@inline] dec self = A.decr self.g
|
let[@inline] decr self = A.decr self.g
|
||||||
let[@inline] dec_by self n = ignore (A.fetch_and_add self.g (-n) : int)
|
let[@inline] decr_by self n = ignore (A.fetch_and_add self.g (-n) : int)
|
||||||
end
|
end
|
||||||
|
|
||||||
module Registry = struct
|
module Registry = struct
|
||||||
|
|
@ -86,3 +86,21 @@ module Registry = struct
|
||||||
end
|
end
|
||||||
|
|
||||||
let global = Registry.create ()
|
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)
|
||||||
|
|
|
||||||
|
|
@ -24,10 +24,10 @@ module Counter : sig
|
||||||
(** A counter, monotonically increasing *)
|
(** A counter, monotonically increasing *)
|
||||||
|
|
||||||
val create : Registry.t -> ?tags:tags -> ?descr:string -> string -> t
|
val create : Registry.t -> ?tags:tags -> ?descr:string -> string -> t
|
||||||
val inc : t -> unit
|
val incr : t -> unit
|
||||||
val inc_by : t -> int -> unit
|
val incr_by : t -> int -> unit
|
||||||
val dec : t -> unit
|
val decr : t -> unit
|
||||||
val dec_by : t -> int -> unit
|
val decr_by : t -> int -> unit
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Gauges *)
|
(** Gauges *)
|
||||||
|
|
@ -37,13 +37,15 @@ module Gauge : sig
|
||||||
|
|
||||||
val create : Registry.t -> ?tags:tags -> ?descr:string -> string -> t
|
val create : Registry.t -> ?tags:tags -> ?descr:string -> string -> t
|
||||||
val set : t -> int -> unit
|
val set : t -> int -> unit
|
||||||
val inc : t -> unit
|
val incr : t -> unit
|
||||||
val inc_by : t -> int -> unit
|
val incr_by : t -> int -> unit
|
||||||
val dec : t -> unit
|
val decr : t -> unit
|
||||||
val dec_by : t -> int -> unit
|
val decr_by : t -> int -> unit
|
||||||
end
|
end
|
||||||
|
|
||||||
(* TODO:
|
(* TODO:
|
||||||
module Histogram : sig
|
module Histogram : sig
|
||||||
end
|
end
|
||||||
*)
|
*)
|
||||||
|
|
||||||
|
val http_middleware : Registry.t -> Tiny_httpd.Middleware.t
|
||||||
|
|
|
||||||
|
|
@ -8,16 +8,16 @@ let g1 = P.Gauge.create reg ~tags:[ "level", "max" ] "yolo_gauge"
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
print_endline "==== first try ====";
|
print_endline "==== first try ====";
|
||||||
P.Counter.inc_by c1 42;
|
P.Counter.incr_by c1 42;
|
||||||
P.Counter.inc c2;
|
P.Counter.incr c2;
|
||||||
P.Gauge.set g1 2525;
|
P.Gauge.set g1 2525;
|
||||||
|
|
||||||
pf "```\n%s\n```\n" @@ P.Registry.emit_str reg
|
pf "```\n%s\n```\n" @@ P.Registry.emit_str reg
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
print_endline "==== second try====";
|
print_endline "==== second try====";
|
||||||
P.Counter.inc_by c1 11;
|
P.Counter.incr_by c1 11;
|
||||||
P.Counter.inc c2;
|
P.Counter.incr c2;
|
||||||
P.Gauge.set g1 42_000;
|
P.Gauge.set g1 42_000;
|
||||||
|
|
||||||
pf "```\n%s\n```\n" @@ P.Registry.emit_str reg
|
pf "```\n%s\n```\n" @@ P.Registry.emit_str reg
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue