From 0d750cd86c1832fbdf8a82080e03309173041e16 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 26 Feb 2024 14:00:58 -0500 Subject: [PATCH] fix prometheus --- src/Tiny_httpd.ml | 2 + src/Tiny_httpd.mli | 57 ++++++++++++++++++++++++ src/bin/http_of_dir.ml | 4 +- src/core/dune | 4 +- src/core/pool.ml | 2 +- src/prometheus/common_.ml | 3 -- src/prometheus/common_p_.ml | 3 ++ src/prometheus/dune | 5 ++- src/prometheus/tiny_httpd_prometheus.ml | 21 +++++---- src/prometheus/tiny_httpd_prometheus.mli | 6 +-- 10 files changed, 83 insertions(+), 24 deletions(-) delete mode 100644 src/prometheus/common_.ml create mode 100644 src/prometheus/common_p_.ml diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index 422a7cdb..6263199a 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -6,6 +6,8 @@ module Html = Tiny_httpd_html module IO = Tiny_httpd_core.IO module Pool = Tiny_httpd_core.Pool module Log = Tiny_httpd_core.Log +module Server = Tiny_httpd_core.Server +include Server open struct let get_max_connection_ ?(max_connections = 64) () : int = diff --git a/src/Tiny_httpd.mli b/src/Tiny_httpd.mli index 167b5873..2eb3afbb 100644 --- a/src/Tiny_httpd.mli +++ b/src/Tiny_httpd.mli @@ -108,6 +108,63 @@ module Pool = Tiny_httpd_core.Pool module Dir = Tiny_httpd_unix.Dir +(** {2 HTML combinators} *) + module Html = Tiny_httpd_html (** Alias to {!Tiny_httpd_html} @since 0.12 *) + +(** {2 Main server types} *) + +module Server = Tiny_httpd_core.Server + +include module type of struct + include Server +end + +val create : + ?masksigpipe:bool -> + ?max_connections:int -> + ?timeout:float -> + ?buf_size:int -> + ?get_time_s:(unit -> float) -> + ?new_thread:((unit -> unit) -> unit) -> + ?addr:string -> + ?port:int -> + ?sock:Unix.file_descr -> + ?middlewares:([ `Encoding | `Stage of int ] * Middleware.t) list -> + unit -> + t +(** Create a new webserver using UNIX abstractions. + + The server will not do anything until {!run} is called on it. + Before starting the server, one can use {!add_path_handler} and + {!set_top_handler} to specify how to handle incoming requests. + + @param masksigpipe if true, block the signal {!Sys.sigpipe} which otherwise + tends to kill client threads when they try to write on broken sockets. Default: [true]. + + @param buf_size size for buffers (since 0.11) + + @param new_thread a function used to spawn a new thread to handle a + new client connection. By default it is {!Thread.create} but one + could use a thread pool instead. + See for example {{: https://github.com/c-cube/tiny-httpd-moonpool-bench/blob/0dcbbffb4fe34ea4ad79d46343ad0cebb69ca69f/examples/t1.ml#L31} + this use of moonpool}. + + @param middlewares see {!add_middleware} for more details. + + @param max_connections maximum number of simultaneous connections. + @param timeout connection is closed if the socket does not do read or + write for the amount of second. Default: 0.0 which means no timeout. + timeout is not recommended when using proxy. + @param addr address (IPv4 or IPv6) to listen on. Default ["127.0.0.1"]. + @param port to listen on. Default [8080]. + @param sock an existing socket given to the server to listen on, e.g. by + systemd on Linux (or launchd on macOS). If passed in, this socket will be + used instead of the [addr] and [port]. If not passed in, those will be + used. This parameter exists since 0.10. + + @param get_time_s obtain the current timestamp in seconds. + This parameter exists since 0.11. +*) diff --git a/src/bin/http_of_dir.ml b/src/bin/http_of_dir.ml index 468f7e32..38f20820 100644 --- a/src/bin/http_of_dir.ml +++ b/src/bin/http_of_dir.ml @@ -1,6 +1,6 @@ module S = Tiny_httpd -module U = Tiny_httpd_util -module D = Tiny_httpd_dir +module U = Tiny_httpd.Util +module D = Tiny_httpd.Dir module Pf = Printf module Log = Tiny_httpd.Log diff --git a/src/core/dune b/src/core/dune index a8983d14..a04707ef 100644 --- a/src/core/dune +++ b/src/core/dune @@ -2,14 +2,14 @@ (library (name tiny_httpd_core) (public_name tiny_httpd.core) - (private_modules parse_) + (private_modules parse_ common_) (libraries threads seq hmap iostream (select log.ml from (logs -> log.logs.ml) (-> log.default.ml)))) (rule - (targets Tiny_httpd_atomic_.ml) + (targets Atomic_.ml) (deps (:bin ./gen/mkshims.exe)) (action diff --git a/src/core/pool.ml b/src/core/pool.ml index 1a441944..fc9a0461 100644 --- a/src/core/pool.ml +++ b/src/core/pool.ml @@ -1,4 +1,4 @@ -module A = Tiny_httpd_atomic_ +module A = Atomic_ type 'a list_ = Nil | Cons of int * 'a * 'a list_ diff --git a/src/prometheus/common_.ml b/src/prometheus/common_.ml deleted file mode 100644 index bb70b2d7..00000000 --- a/src/prometheus/common_.ml +++ /dev/null @@ -1,3 +0,0 @@ -module A = Tiny_httpd_atomic_ - -let spf = Printf.sprintf diff --git a/src/prometheus/common_p_.ml b/src/prometheus/common_p_.ml new file mode 100644 index 00000000..812670ab --- /dev/null +++ b/src/prometheus/common_p_.ml @@ -0,0 +1,3 @@ +module A = Tiny_httpd_core.Atomic_ + +let spf = Printf.sprintf diff --git a/src/prometheus/dune b/src/prometheus/dune index 5da724e5..3439a474 100644 --- a/src/prometheus/dune +++ b/src/prometheus/dune @@ -4,9 +4,10 @@ (name tiny_httpd_prometheus) (public_name tiny_httpd.prometheus) (synopsis "Metrics using prometheus") - (private_modules common_ time_) + (private_modules common_p_ time_) + (flags :standard -open Tiny_httpd_core) (libraries - tiny_httpd unix + tiny_httpd.core unix (select time_.ml from (mtime mtime.clock.os -> time_.mtime.ml) (-> time_.default.ml)))) diff --git a/src/prometheus/tiny_httpd_prometheus.ml b/src/prometheus/tiny_httpd_prometheus.ml index 325018a8..b3ec4e39 100644 --- a/src/prometheus/tiny_httpd_prometheus.ml +++ b/src/prometheus/tiny_httpd_prometheus.ml @@ -2,7 +2,7 @@ https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format *) -open Common_ +open Common_p_ let bpf = Printf.bprintf @@ -175,9 +175,7 @@ end let global = Registry.create () -module H = Tiny_httpd - -let http_middleware (reg : Registry.t) : H.Middleware.t = +let http_middleware (reg : Registry.t) : Server.Middleware.t = let c_req = Counter.create reg "tiny_httpd_requests" ~descr:"number of HTTP requests" in @@ -189,11 +187,11 @@ let http_middleware (reg : Registry.t) : H.Middleware.t = ~buckets:[ 0.001; 0.01; 0.1; 0.5; 1.; 5.; 10. ] in - fun h : H.Middleware.handler -> + fun h : Server.Middleware.handler -> fun req ~resp : unit -> let start = Time_.now_us () in Counter.incr c_req; - h req ~resp:(fun (response : H.Response.t) -> + h req ~resp:(fun (response : Response.t) -> let code = response.code in let elapsed_us = Time_.now_us () -. start in @@ -203,13 +201,14 @@ let http_middleware (reg : Registry.t) : H.Middleware.t = if code < 200 || code >= 400 then Counter.incr c_err; resp response) -let add_route_to_server (server : H.t) (reg : registry) : unit = - H.add_route_handler server H.Route.(exact "metrics" @/ return) @@ fun _req -> +let add_route_to_server (server : Server.t) (reg : registry) : unit = + Server.add_route_handler server Route.(exact "metrics" @/ return) + @@ fun _req -> let str = Registry.emit_str reg in - H.Response.make_string @@ Ok str + Response.make_string @@ Ok str -let instrument_server (server : H.t) reg : unit = - H.add_middleware ~stage:(`Stage 1) server (http_middleware reg); +let instrument_server (server : Server.t) reg : unit = + Server.add_middleware ~stage:(`Stage 1) server (http_middleware reg); add_route_to_server server reg module GC_metrics = struct diff --git a/src/prometheus/tiny_httpd_prometheus.mli b/src/prometheus/tiny_httpd_prometheus.mli index b634943d..dfac868a 100644 --- a/src/prometheus/tiny_httpd_prometheus.mli +++ b/src/prometheus/tiny_httpd_prometheus.mli @@ -77,13 +77,13 @@ end end *) -val http_middleware : Registry.t -> Tiny_httpd.Middleware.t +val http_middleware : Registry.t -> Server.Middleware.t (** Middleware to get basic metrics about HTTP requests *) -val add_route_to_server : Tiny_httpd.t -> Registry.t -> unit +val add_route_to_server : Server.t -> Registry.t -> unit (** Add a "/metrics" route to the server *) -val instrument_server : Tiny_httpd.t -> Registry.t -> unit +val instrument_server : Server.t -> Registry.t -> unit (** Add middleware and route *) module GC_metrics : sig