Skip to content

Commit eef5305

Browse files
committed
feature: diagnostic events in the trace
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent 21c557a commit eef5305

File tree

7 files changed

+71
-1
lines changed

7 files changed

+71
-1
lines changed

bin/common.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,16 @@ let build (root : Workspace_root.t) (builder : Builder.t) =
12571257
match Sys.getenv_opt "DUNE_TRACE" with
12581258
| None ->
12591259
Dune_trace.Category.
1260-
[ Sandbox; Persistent; Process; Rules; Pkg; Promote; Build; File_watcher ]
1260+
[ Sandbox
1261+
; Persistent
1262+
; Process
1263+
; Rules
1264+
; Pkg
1265+
; Promote
1266+
; Build
1267+
; File_watcher
1268+
; Diagnostics
1269+
]
12611270
| Some s ->
12621271
String.split ~on:',' s
12631272
|> List.map ~f:(fun x ->

doc/changes/added/13041.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add diagnostic events to the trace. (#13041, @rgrinberg)

src/dune_trace/category.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type t =
1515
| Debug
1616
| Config
1717
| File_watcher
18+
| Diagnostics
1819

1920
let all =
2021
[ Rpc
@@ -31,6 +32,7 @@ let all =
3132
; Debug
3233
; Config
3334
; File_watcher
35+
; Diagnostics
3436
]
3537
;;
3638

@@ -49,6 +51,7 @@ let to_string = function
4951
| Debug -> "debug"
5052
| Config -> "config"
5153
| File_watcher -> "file_watcher"
54+
| Diagnostics -> "diagnostics"
5255
;;
5356

5457
let of_string =
@@ -79,5 +82,6 @@ module Set = Bit_set.Make (struct
7982
| Debug -> 11
8083
| Config -> 12
8184
| File_watcher -> 13
85+
| Diagnostics -> 14
8286
;;
8387
end)

src/dune_trace/category.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type t =
1313
| Debug
1414
| Config
1515
| File_watcher
16+
| Diagnostics
1617

1718
val to_string : t -> string
1819
val of_string : string -> t option

src/dune_trace/dune_trace.mli

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Category : sig
1616
| Debug
1717
| Config
1818
| File_watcher
19+
| Diagnostics
1920

2021
val of_string : string -> t option
2122
end
@@ -95,6 +96,13 @@ module Event : sig
9596
]
9697
-> t
9798

99+
val error
100+
: Loc.t option
101+
-> [< `Fatal | `User ]
102+
-> Printexc.raw_backtrace option
103+
-> Dyn.t list
104+
-> t
105+
98106
module Rpc : sig
99107
type stage =
100108
[ `Start

src/dune_trace/event.ml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,35 @@ let file_watcher event =
368368
in
369369
Event.instant ~name ~args now File_watcher
370370
;;
371+
372+
let rec json_of_sexp : Sexp.t -> Json.t = function
373+
| Atom s -> Json.string s
374+
| List xs -> Json.list (List.map ~f:json_of_sexp xs)
375+
;;
376+
377+
let error loc kind backtrace memo_stack =
378+
let now = Time.now () in
379+
let name =
380+
match kind with
381+
| `User -> "user"
382+
| `Fatal -> "fatal"
383+
in
384+
let loc =
385+
Option.map loc ~f:(fun loc -> "loc", Json.string (Loc.to_file_colon_line loc))
386+
in
387+
let memo_stack =
388+
match memo_stack with
389+
| [] -> None
390+
| frames ->
391+
let frames =
392+
List.map frames ~f:(fun dyn -> json_of_sexp (Sexp.of_dyn dyn)) |> Json.list
393+
in
394+
Some ("memo", frames)
395+
in
396+
let backtrace =
397+
Option.map backtrace ~f:(fun bt ->
398+
"backtrace", Json.string (Printexc.raw_backtrace_to_string bt))
399+
in
400+
let args = List.filter_opt [ loc; memo_stack; backtrace ] in
401+
Event.instant ~name ~args now Diagnostics
402+
;;

src/dune_util/report_error.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ let gen_report
191191
| Developer -> i_must_not_crash ())
192192
]
193193
in
194+
let () =
195+
try
196+
Dune_trace.emit Diagnostics (fun () ->
197+
Dune_trace.Event.error
198+
loc
199+
(match responsible with
200+
| User -> `User
201+
| Developer -> `Fatal)
202+
backtrace
203+
(List.map memo_stack ~f:Memo.Stack_frame.to_dyn))
204+
with
205+
| _ ->
206+
(* We don't want a failure to write to the trace raise another event *)
207+
()
208+
in
194209
Dune_console.print_user_message { msg with loc; paragraphs }
195210
;;
196211

0 commit comments

Comments
 (0)