Skip to content

Commit e9e8521

Browse files
committed
CA-412313: xs-trace: introduce a pp command
json_reformat cannot handle newline delimited json, it is easier if we have a command to reformat it ourselves. This can be useful when debugging why a trace is missing elements. Traces are stored as newline-delimited JSON in /var/log/dt/zipkinv2/json, however json_reformat cannot process them directly, and the lines can be very long and difficult to read otherwise. Signed-off-by: Edwin Török <[email protected]>
1 parent 71a4989 commit e9e8521

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

ocaml/xs-trace/dune

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
(executable
2-
(modes exe)
3-
(name xs_trace)
4-
(public_name xs-trace)
5-
(package xapi-tools)
6-
(libraries
7-
uri
8-
tracing
9-
cmdliner
10-
tracing_export
11-
xapi-stdext-unix
12-
zstd
13-
)
14-
)
2+
(modes exe)
3+
(name xs_trace)
4+
(public_name xs-trace)
5+
(package xapi-tools)
6+
(libraries uri tracing cmdliner tracing_export yojson xapi-stdext-unix zstd))
157

168
(rule
17-
(targets xs-trace.1)
18-
(deps (:exe xs_trace.exe))
19-
(action (with-stdout-to %{targets} (run %{exe} --help=groff)))
20-
)
9+
(targets xs-trace.1)
10+
(deps
11+
(:exe xs_trace.exe))
12+
(action
13+
(with-stdout-to
14+
%{targets}
15+
(run %{exe} --help=groff))))
2116

2217
; not expected by the specfile
2318
;(install

ocaml/xs-trace/xs_trace.ml

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ module Exporter = struct
2525
| _ ->
2626
()
2727

28-
(** Export traces from file system to a remote endpoint. *)
29-
let export erase src dst =
30-
let dst = Uri.of_string dst in
31-
let submit_json = submit_json dst in
28+
let iter_src src f =
3229
let rec export_file = function
3330
| path when Sys.is_directory path ->
3431
(* Recursively export trace files. *)
@@ -38,7 +35,7 @@ module Exporter = struct
3835
(* Decompress compressed trace file and submit each line iteratively *)
3936
let args = [|"zstdcat"; path|] in
4037
let ic = Unix.open_process_args_in args.(0) args in
41-
Unixext.lines_iter submit_json ic ;
38+
Unixext.lines_iter f ic ;
4239
match Unix.close_process_in ic with
4340
| Unix.WEXITED 0 ->
4441
()
@@ -47,15 +44,25 @@ module Exporter = struct
4744
)
4845
| path when Filename.check_suffix path ".ndjson" ->
4946
(* Submit traces line by line. *)
50-
Unixext.readfile_line submit_json path
47+
Unixext.readfile_line f path
5148
| path ->
5249
(* Assume any other extension is a valid JSON file. *)
5350
let json = Unixext.string_of_file path in
54-
submit_json json
51+
f json
5552
in
56-
export_file src ;
53+
export_file src
54+
55+
(** Export traces from file system to a remote endpoint. *)
56+
let export erase src dst =
57+
let dst = Uri.of_string dst in
58+
let submit_json = submit_json dst in
59+
iter_src src submit_json;
5760
if erase then
5861
Unixext.rm_rec ~rm_top:true src
62+
63+
let pretty_print src =
64+
iter_src src @@ fun line ->
65+
line |> Yojson.Safe.from_string |> Yojson.Safe.pretty_to_channel ~std:true stdout
5966
end
6067

6168
module Cli = struct
@@ -83,6 +90,11 @@ module Cli = struct
8390
let doc = "copy a trace to an endpoint and erase it afterwards" in
8491
Cmd.(v (info "mv" ~doc) term)
8592

93+
let pp_cmd =
94+
let term = Term.(const Exporter.pretty_print $ src) in
95+
let doc = "Pretty print NDJSON traces" in
96+
Cmd.(v (info "pp" ~doc) term)
97+
8698
let xs_trace_cmd =
8799
let man =
88100
[
@@ -94,7 +106,7 @@ module Cli = struct
94106
let doc = "utility for working with local trace files" in
95107
Cmd.info "xs-trace" ~doc ~version:"0.1" ~man
96108
in
97-
Cmd.group desc [cp_cmd; mv_cmd]
109+
Cmd.group desc [cp_cmd; mv_cmd; pp_cmd]
98110

99111
let main () = Cmd.eval xs_trace_cmd
100112
end

0 commit comments

Comments
 (0)