2019-11-06 18:28:26 +00:00
|
|
|
open Lwt.Infix
|
2019-05-12 21:25:25 +00:00
|
|
|
|
2019-11-06 18:28:26 +00:00
|
|
|
let src = Logs.Src.create "influx" ~doc:"influx metrics reporter"
|
|
|
|
module Log = (val Logs.src_log src : Logs.LOG)
|
2019-05-12 21:25:25 +00:00
|
|
|
|
2019-11-06 18:28:26 +00:00
|
|
|
let vmname = Metrics.field ~doc:"name of the virtual machine" "vm" Metrics.String
|
2019-05-12 21:25:25 +00:00
|
|
|
|
2019-11-06 18:28:26 +00:00
|
|
|
module Make (T : Mirage_time.S) (S : Mirage_stack.V4) = struct
|
2019-05-12 21:25:25 +00:00
|
|
|
|
2019-11-06 18:28:26 +00:00
|
|
|
let timer conn get host stack dst =
|
|
|
|
let datas =
|
|
|
|
Metrics.SM.fold (fun src (tags, data) acc ->
|
|
|
|
let name = Metrics.Src.name src in
|
|
|
|
Metrics_influx.encode_line_protocol (host@tags) data name :: acc)
|
|
|
|
(get ()) []
|
2019-05-12 21:25:25 +00:00
|
|
|
in
|
2019-11-06 18:28:26 +00:00
|
|
|
let datas = String.concat "" datas in
|
|
|
|
let write flow =
|
|
|
|
Log.debug (fun m -> m "sending measurements");
|
|
|
|
S.TCPV4.write flow (Cstruct.of_string datas) >|= function
|
|
|
|
| Ok () -> ()
|
|
|
|
| Error e ->
|
|
|
|
Log.err (fun m -> m "error %a writing to metrics" S.TCPV4.pp_write_error e);
|
|
|
|
conn := None
|
2019-05-12 21:25:25 +00:00
|
|
|
in
|
2019-11-06 18:28:26 +00:00
|
|
|
match !conn with
|
|
|
|
| None ->
|
|
|
|
begin
|
|
|
|
Log.debug (fun m -> m "creating connection");
|
|
|
|
S.TCPV4.create_connection (S.tcpv4 stack) dst >>= function
|
|
|
|
| Error msg ->
|
|
|
|
Log.err (fun m -> m "couldn't create connection %a"
|
|
|
|
S.TCPV4.pp_error msg);
|
|
|
|
Lwt.return_unit
|
|
|
|
| Ok flow ->
|
|
|
|
conn := Some flow;
|
|
|
|
write flow
|
|
|
|
end
|
|
|
|
| Some f -> write f
|
|
|
|
|
|
|
|
let timer_loop get host interval stack dst () =
|
|
|
|
let conn = ref None in
|
|
|
|
let rec one () =
|
|
|
|
Lwt.join [
|
|
|
|
timer conn get host stack dst;
|
2019-05-12 21:25:25 +00:00
|
|
|
T.sleep_ns (Duration.of_sec interval)
|
|
|
|
] >>= fun () ->
|
2019-11-06 18:28:26 +00:00
|
|
|
(one[@tailcall]) ()
|
|
|
|
in
|
|
|
|
one ()
|
|
|
|
|
|
|
|
let create ?(interval = 10) ?hostname stack dst =
|
|
|
|
let get_cache, reporter = Metrics.cache_reporter () in
|
|
|
|
Metrics.set_reporter reporter;
|
|
|
|
Metrics.enable_all ();
|
|
|
|
Metrics_lwt.init_periodic (fun () -> T.sleep_ns (Duration.of_sec interval));
|
|
|
|
Metrics_lwt.periodically (OS.MM.malloc_metrics ~tags:Metrics.Tags.[]);
|
|
|
|
let host = match hostname with None -> [] | Some host -> [vmname host] in
|
|
|
|
Lwt.async (timer_loop get_cache host interval stack dst)
|
2019-05-12 21:25:25 +00:00
|
|
|
end
|
|
|
|
|