Skip to content

Commit 9a048dc

Browse files
committed
use only one flag and default to 0.0.0.0:9090
1 parent 8ac13f2 commit 9a048dc

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

app/prometheus_unix.ml

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,54 +42,48 @@ module Unix_runtime = struct
4242
]
4343
end
4444

45-
type config = {
46-
port : int option;
47-
addr : string option;
48-
}
45+
type config = string option
4946

5047
module Server = Prometheus_app.Cohttp(Cohttp_lwt_unix.Server)
5148

52-
let serve config = match config.port, config.addr with
53-
| None, _ -> []
54-
| Some port, None ->
55-
let mode = `TCP (`Port port) in
56-
let callback = Server.callback in
57-
let thread = Cohttp_lwt_unix.Server.create ~mode (Cohttp_lwt_unix.Server.make ~callback ()) in
58-
[thread]
59-
| Some port, Some addr ->
49+
let bind addr port =
6050
let open! Unix in
6151
let [@ocaml.warning "-partial-match"] addrinfo :: _ =
62-
getaddrinfo addr (Int.to_string port) [AI_SOCKTYPE SOCK_STREAM] in
52+
getaddrinfo addr port [AI_SOCKTYPE SOCK_STREAM] in
6353
let socket = socket ~cloexec:true addrinfo.ai_family addrinfo.ai_socktype addrinfo.ai_protocol in
6454
let () = setsockopt socket SO_REUSEADDR true in
6555
let callback = Server.callback in
66-
let () = listen socket 20 in
6756
let () = bind socket addrinfo.ai_addr in
57+
let () = listen socket 20 in
6858
let mode = `TCP (`Socket (Lwt_unix.of_unix_file_descr socket)) in
6959
let thread = Cohttp_lwt_unix.Server.create ~mode (Cohttp_lwt_unix.Server.make ~callback ()) in
7060
[thread]
7161

72-
let listen_prometheus_addr =
73-
let open! Cmdliner in
74-
let doc =
75-
Arg.info ~docs:"MONITORING OPTIONS" ~docv:"ADDR" ~doc:
76-
"Ip address on which to provide Prometheus metrics over HTTP."
77-
["listen-prometheus-addr"]
78-
in
79-
Arg.(value @@ opt (some string) None doc)
62+
let serve config =
63+
let addr = "0.0.0.0" in
64+
let port = "9090" in
65+
match config with
66+
| None -> []
67+
| Some config_s ->
68+
try
69+
match (String.split_on_char ':' config_s) with
70+
| [] -> bind addr port
71+
| port :: [] -> bind addr port
72+
| addr :: port :: [] -> bind addr port
73+
with
74+
| Match_failure _ -> Printf.printf "ERROR: Incorrect addr:port pair specified, prometheus listener not starting.\n"; flush_all (); []
75+
[@@ocaml.warning "-partial-match"]
8076

8177
let listen_prometheus =
8278
let open! Cmdliner in
8379
let doc =
84-
Arg.info ~docs:"MONITORING OPTIONS" ~docv:"PORT" ~doc:
85-
"Port on which to provide Prometheus metrics over HTTP."
80+
Arg.info ~docs:"MONITORING OPTIONS" ~docv:"ADDR_PORT" ~doc:
81+
"Address and port on which to provide Prometheus metrics over HTTP."
8682
["listen-prometheus"]
8783
in
88-
Arg.(value @@ opt (some int) None doc)
84+
Arg.(value @@ opt (some string) None doc)
8985

90-
let opts =
91-
let combine port addr = { port; addr } in
92-
Cmdliner.Term.(const combine $ listen_prometheus $ listen_prometheus_addr)
86+
let opts = listen_prometheus
9387

9488
let () =
9589
let add (info, collector) =

0 commit comments

Comments
 (0)