Skip to content

Commit ed2a2f5

Browse files
committed
web: track more information about request/response in span
it's useful to know how big a request or a response is. In addition, when used to query CH, being able to retrieve the CH query ID or summary can help debug.
1 parent 2d59983 commit ed2a2f5

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

web.ml

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
182182
let with_curl f = bracket (return @@ Curl.init ()) (fun h -> Curl.cleanup h; return_unit) f
183183
let with_curl_cache f = bracket (return @@ CurlCache.get ()) (fun h -> CurlCache.release h; return_unit) f
184184

185+
let get_curl_data h =
186+
[
187+
"http.response.status_code", `Int (Curl.get_httpcode h);
188+
"http.response.body.size", `Int (int_of_float (Curl.get_sizedownload h));
189+
"http.request.body.size", `Int (int_of_float (Curl.get_sizeupload h));
190+
"server.address", `String (Curl.get_primaryip h);
191+
"network.protocol.version", `String (match Curl.get_http_version h with
192+
| HTTP_VERSION_1_0 -> "1.0" | HTTP_VERSION_1_1 -> "1.1"
193+
| HTTP_VERSION_2 | HTTP_VERSION_2TLS | HTTP_VERSION_2_PRIOR_KNOWLEDGE -> "2"
194+
| HTTP_VERSION_3 -> "3" | HTTP_VERSION_NONE -> "?");
195+
]
196+
185197
let update_timer h timer =
186198
match timer with
187199
| None -> ()
@@ -256,6 +268,8 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
256268
let http_request' ?ua ?timeout ?(verbose=false) ?(setup=ignore) ?timer ?max_size ?(http_1_0=false) ?headers ?body (action:http_action) url =
257269
let open Curl in
258270
let action_name = string_of_http_action action in
271+
let ch_query_id = ref None in
272+
let ch_summary = ref None in
259273

260274
let setup ~headers set_body_and_headers h =
261275
begin match body with
@@ -280,6 +294,14 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
280294
if http_1_0 then set_httpversion h HTTP_VERSION_1_0;
281295
Option.may (set_timeout h) timeout;
282296
Option.may (set_useragent h) ua;
297+
set_headerfunction h (fun s ->
298+
(let k, v = Stre.dividec s ':' in
299+
match String.lowercase_ascii k with
300+
| "x-clickhouse-query-id" -> ch_query_id := Some (String.trim v)
301+
| "x-clickhouse-summary" -> ch_summary := Some (String.trim v)
302+
| _ -> ());
303+
String.length s
304+
);
283305
let () = setup h in
284306
()
285307
in
@@ -295,11 +317,15 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
295317
end;
296318

297319
let describe () =
298-
[
320+
let base = [
299321
"otrace.spankind", `String "CLIENT";
300322
"http.request.method", `String action_name;
301323
"url.full", `String url;
302-
]
324+
] in
325+
match body with
326+
| Some (`Raw (ct,_)) | Some (`Chunked (ct,_)) -> ("http.request.header.content-type", `String ct) :: base
327+
| Some (`Form _) -> ("http.request.header.content-type", `String "application/x-www-form-urlencoded") :: base
328+
| None -> base
303329
in
304330
let explicit_span =
305331
let span_name = Printf.sprintf "devkit.web.%s" action_name in
@@ -321,7 +347,14 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
321347
let t = new Action.timer in
322348
let result = Some (fun h code ->
323349
if verbose then verbose_curl_result nr_http action t h code;
324-
Trace_core.add_data_to_manual_span explicit_span ["http.response.status_code", `Int (Curl.get_httpcode h)];
350+
if Trace_core.enabled () then (
351+
let data = get_curl_data h in
352+
let data = match !ch_query_id with None -> data
353+
| Some v -> ("http.response.header.x-clickhouse-query-id", `String v) :: data in
354+
let data = match !ch_summary with None -> data
355+
| Some v -> ("http.response.header.x-clickhouse-summary", `String v) :: data in
356+
Trace_core.add_data_to_manual_span explicit_span data
357+
);
325358
Trace_core.exit_manual_span explicit_span;
326359
return ()
327360
) in

0 commit comments

Comments
 (0)