prometheus icon indicating copy to clipboard operation
prometheus copied to clipboard

Consider allowing any collector registry in `Cohttp` functor

Open vch9 opened this issue 1 year ago • 0 comments

Hey,

I'm starting to use prometheus, here's a minor problem I've occured:

The documentation says that CollectorRegistry.create is "mostly useful for testing". However, it happens to be more useful in my case because our projects links multiple libraries that relies on prometheus, and therefore registers metrics I don't want. However, the functor Cohttp uses the CollectRegistry.default to collect data, so I basically have to copy/paste it and just change the registry:

let registry = CollectorRegistry.create ()

module Cohttp (Server : Cohttp_lwt.S.Server) = struct
  let callback _conn req _body =
    let open Cohttp in
    let open Lwt_syntax in
    let uri = Request.uri req in
    match (Request.meth req, Uri.path uri) with
    | `GET, "/metrics" ->
        let* data = CollectorRegistry.collect registry in
        let body =
          Fmt.to_to_string Prometheus_app.TextFormat_0_0_4.output data
        in
        let headers =
          Header.init_with "Content-Type" "text/plain; version=0.0.4"
        in
        Server.respond_string ~status:`OK ~headers ~body ()
    | _ -> Server.respond_error ~status:`Bad_request ~body:"Bad request" ()
end

Maybe it would make sense to add another argument to the functor with the registry? Or potentially adding a new functor Cohttp_with_registry?

vch9 avatar Apr 16 '24 09:04 vch9