mirage / prometheus

OCaml library for reporting metrics to a Prometheus server
Apache License 2.0
49 stars 27 forks source link

Consider allowing any collector registry in `Cohttp` functor #54

Open vch9 opened 6 months ago

vch9 commented 6 months ago

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?