Skip to content

Commit ba7e6f4

Browse files
committed
web: use structured logging
1 parent 1c64d5d commit ba7e6f4

1 file changed

Lines changed: 35 additions & 21 deletions

File tree

web.ml

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -238,28 +238,36 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
238238

239239
let verbose_curl_result nr_http action t h code =
240240
let open Curl in
241-
let b = Buffer.create 10 in
242-
bprintf b "%s #%d %s ⇓%s ⇑%s %s "
243-
(string_of_http_action action) nr_http (Time.compact_duration t#get)
244-
(Action.bytes_string_f @@ get_sizedownload h)
245-
(Action.bytes_string_f @@ get_sizeupload h)
246-
(get_primaryip h)
247-
;
248-
begin match code with
241+
let size_down = get_sizedownload h in
242+
let size_up = get_sizeupload h in
243+
let base = [
244+
"method", string_of_http_action action;
245+
"http_seq", string_of_int nr_http;
246+
"duration", sprintf "%.3f" t#get;
247+
"size_down", Action.bytes_string_f size_down;
248+
"size_down_raw", sprintf "%.0f" size_down;
249+
"size_up", Action.bytes_string_f size_up;
250+
"size_up_raw", sprintf "%.0f" size_up;
251+
"ip", get_primaryip h;
252+
] in
253+
let base = match get_httpcode h with
254+
| 0 -> base
255+
| n -> ("http_status", string_of_int n) :: base
256+
in
257+
match code with
249258
| CURLE_OK ->
250-
bprintf b "HTTP %d %s" (get_httpcode h) (get_effectiveurl h);
251-
begin match get_redirecturl h with
252-
| "" -> ()
253-
| s -> bprintf b " -> %s" s
254-
end;
255-
begin match get_redirectcount h with
256-
| 0 -> ()
257-
| n -> bprintf b " after %d redirects" n
258-
end
259+
let pairs = ("url", get_effectiveurl h) :: base in
260+
let pairs = match get_redirecturl h with "" -> pairs | s -> ("redirect", s) :: pairs in
261+
let pairs = match get_redirectcount h with 0 -> pairs | n -> ("redirect_count", string_of_int n) :: pairs in
262+
log #info ~pairs "http done"
259263
| _ ->
260-
bprintf b "error (%d) %s (errno %d)" (errno code) (strerror code) (Curl.get_oserrno h)
261-
end;
262-
log #info_s (Buffer.contents b)
264+
let pairs =
265+
("err", strerror code) ::
266+
("errno", string_of_int (errno code)) ::
267+
("oserrno", string_of_int (get_oserrno h)) ::
268+
base
269+
in
270+
log #info ~pairs "http error"
263271

264272
(* Given a list of strings, check pre-existing entry starting with `~name`; and adds the concatenation of `~name` and `~value` if not. *)
265273
let add_if_absent ~name ~value strs =
@@ -320,7 +328,13 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
320328
| Some (`Raw (ct,body)) -> sprintf "%s \"%s\"" ct (Stre.shorten ~escape:true 64 body)
321329
| Some (`Chunked (ct,_f)) -> sprintf "%s chunked" ct
322330
in
323-
log #info "%s #%d %s %s" action_name nr_http url body
331+
let pairs = [
332+
"method", action_name;
333+
"http_seq", string_of_int nr_http;
334+
"url", url;
335+
] in
336+
let pairs = if body = "" then pairs else ("body", body) :: pairs in
337+
log #info ~pairs "http start"
324338
end;
325339

326340
let describe () =

0 commit comments

Comments
 (0)