mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-08 04:05:35 -05:00
fix prometheus
This commit is contained in:
parent
04be73ee00
commit
0d750cd86c
10 changed files with 83 additions and 24 deletions
|
|
@ -6,6 +6,8 @@ module Html = Tiny_httpd_html
|
||||||
module IO = Tiny_httpd_core.IO
|
module IO = Tiny_httpd_core.IO
|
||||||
module Pool = Tiny_httpd_core.Pool
|
module Pool = Tiny_httpd_core.Pool
|
||||||
module Log = Tiny_httpd_core.Log
|
module Log = Tiny_httpd_core.Log
|
||||||
|
module Server = Tiny_httpd_core.Server
|
||||||
|
include Server
|
||||||
|
|
||||||
open struct
|
open struct
|
||||||
let get_max_connection_ ?(max_connections = 64) () : int =
|
let get_max_connection_ ?(max_connections = 64) () : int =
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,63 @@ module Pool = Tiny_httpd_core.Pool
|
||||||
|
|
||||||
module Dir = Tiny_httpd_unix.Dir
|
module Dir = Tiny_httpd_unix.Dir
|
||||||
|
|
||||||
|
(** {2 HTML combinators} *)
|
||||||
|
|
||||||
module Html = Tiny_httpd_html
|
module Html = Tiny_httpd_html
|
||||||
(** Alias to {!Tiny_httpd_html}
|
(** Alias to {!Tiny_httpd_html}
|
||||||
@since 0.12 *)
|
@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.
|
||||||
|
*)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
module S = Tiny_httpd
|
module S = Tiny_httpd
|
||||||
module U = Tiny_httpd_util
|
module U = Tiny_httpd.Util
|
||||||
module D = Tiny_httpd_dir
|
module D = Tiny_httpd.Dir
|
||||||
module Pf = Printf
|
module Pf = Printf
|
||||||
module Log = Tiny_httpd.Log
|
module Log = Tiny_httpd.Log
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,14 @@
|
||||||
(library
|
(library
|
||||||
(name tiny_httpd_core)
|
(name tiny_httpd_core)
|
||||||
(public_name tiny_httpd.core)
|
(public_name tiny_httpd.core)
|
||||||
(private_modules parse_)
|
(private_modules parse_ common_)
|
||||||
(libraries threads seq hmap iostream
|
(libraries threads seq hmap iostream
|
||||||
(select log.ml from
|
(select log.ml from
|
||||||
(logs -> log.logs.ml)
|
(logs -> log.logs.ml)
|
||||||
(-> log.default.ml))))
|
(-> log.default.ml))))
|
||||||
|
|
||||||
(rule
|
(rule
|
||||||
(targets Tiny_httpd_atomic_.ml)
|
(targets Atomic_.ml)
|
||||||
(deps
|
(deps
|
||||||
(:bin ./gen/mkshims.exe))
|
(:bin ./gen/mkshims.exe))
|
||||||
(action
|
(action
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
module A = Tiny_httpd_atomic_
|
module A = Atomic_
|
||||||
|
|
||||||
type 'a list_ = Nil | Cons of int * 'a * 'a list_
|
type 'a list_ = Nil | Cons of int * 'a * 'a list_
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
module A = Tiny_httpd_atomic_
|
|
||||||
|
|
||||||
let spf = Printf.sprintf
|
|
||||||
3
src/prometheus/common_p_.ml
Normal file
3
src/prometheus/common_p_.ml
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module A = Tiny_httpd_core.Atomic_
|
||||||
|
|
||||||
|
let spf = Printf.sprintf
|
||||||
|
|
@ -4,9 +4,10 @@
|
||||||
(name tiny_httpd_prometheus)
|
(name tiny_httpd_prometheus)
|
||||||
(public_name tiny_httpd.prometheus)
|
(public_name tiny_httpd.prometheus)
|
||||||
(synopsis "Metrics using prometheus")
|
(synopsis "Metrics using prometheus")
|
||||||
(private_modules common_ time_)
|
(private_modules common_p_ time_)
|
||||||
|
(flags :standard -open Tiny_httpd_core)
|
||||||
(libraries
|
(libraries
|
||||||
tiny_httpd unix
|
tiny_httpd.core unix
|
||||||
(select time_.ml from
|
(select time_.ml from
|
||||||
(mtime mtime.clock.os -> time_.mtime.ml)
|
(mtime mtime.clock.os -> time_.mtime.ml)
|
||||||
(-> time_.default.ml))))
|
(-> time_.default.ml))))
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format
|
https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format
|
||||||
*)
|
*)
|
||||||
|
|
||||||
open Common_
|
open Common_p_
|
||||||
|
|
||||||
let bpf = Printf.bprintf
|
let bpf = Printf.bprintf
|
||||||
|
|
||||||
|
|
@ -175,9 +175,7 @@ end
|
||||||
|
|
||||||
let global = Registry.create ()
|
let global = Registry.create ()
|
||||||
|
|
||||||
module H = Tiny_httpd
|
let http_middleware (reg : Registry.t) : Server.Middleware.t =
|
||||||
|
|
||||||
let http_middleware (reg : Registry.t) : H.Middleware.t =
|
|
||||||
let c_req =
|
let c_req =
|
||||||
Counter.create reg "tiny_httpd_requests" ~descr:"number of HTTP requests"
|
Counter.create reg "tiny_httpd_requests" ~descr:"number of HTTP requests"
|
||||||
in
|
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. ]
|
~buckets:[ 0.001; 0.01; 0.1; 0.5; 1.; 5.; 10. ]
|
||||||
in
|
in
|
||||||
|
|
||||||
fun h : H.Middleware.handler ->
|
fun h : Server.Middleware.handler ->
|
||||||
fun req ~resp : unit ->
|
fun req ~resp : unit ->
|
||||||
let start = Time_.now_us () in
|
let start = Time_.now_us () in
|
||||||
Counter.incr c_req;
|
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 code = response.code in
|
||||||
|
|
||||||
let elapsed_us = Time_.now_us () -. start 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;
|
if code < 200 || code >= 400 then Counter.incr c_err;
|
||||||
resp response)
|
resp response)
|
||||||
|
|
||||||
let add_route_to_server (server : H.t) (reg : registry) : unit =
|
let add_route_to_server (server : Server.t) (reg : registry) : unit =
|
||||||
H.add_route_handler server H.Route.(exact "metrics" @/ return) @@ fun _req ->
|
Server.add_route_handler server Route.(exact "metrics" @/ return)
|
||||||
|
@@ fun _req ->
|
||||||
let str = Registry.emit_str reg in
|
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 =
|
let instrument_server (server : Server.t) reg : unit =
|
||||||
H.add_middleware ~stage:(`Stage 1) server (http_middleware reg);
|
Server.add_middleware ~stage:(`Stage 1) server (http_middleware reg);
|
||||||
add_route_to_server server reg
|
add_route_to_server server reg
|
||||||
|
|
||||||
module GC_metrics = struct
|
module GC_metrics = struct
|
||||||
|
|
|
||||||
|
|
@ -77,13 +77,13 @@ end
|
||||||
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 *)
|
(** 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 *)
|
(** 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 *)
|
(** Add middleware and route *)
|
||||||
|
|
||||||
module GC_metrics : sig
|
module GC_metrics : sig
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue