add trace-lwt library

This commit is contained in:
Simon Cruanes 2023-09-13 14:28:55 -04:00
parent 431811c995
commit c76fc129b3
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
5 changed files with 104 additions and 0 deletions

View file

@ -33,4 +33,12 @@
(tags
(trace tracing catapult)))
(package
(name trace-lwt)
(synopsis "Helpers to use Trace from Lwt")
(depends
(trace (= :version))
lwt)
(tags (trace lwt asynchronous)))
; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project

7
src/lwt/dune Normal file
View file

@ -0,0 +1,7 @@
(library
(name trace_lwt)
(public_name trace-lwt)
(synopsis "Helpers to use Trace from Lwt")
(libraries trace.core lwt))

30
src/lwt/trace_lwt.ml Normal file
View file

@ -0,0 +1,30 @@
include Trace_core
let k_parent : explicit_span Lwt.key = Lwt.new_key ()
let[@inline never] with_span_lwt_real_ ?(force_toplevel = false) ?__FUNCTION__
~__FILE__ ~__LINE__ ?data name f =
let parent = Lwt.get k_parent in
let espan =
match parent, force_toplevel with
| _, true | None, _ ->
enter_manual_toplevel_span ~flavor:`Async ?__FUNCTION__ ~__FILE__
~__LINE__ ?data name
| Some parent, _ ->
enter_manual_sub_span ~parent ~flavor:`Async ?__FUNCTION__ ~__FILE__
~__LINE__ ?data name
in
Lwt.with_value k_parent (Some espan) (fun () ->
let fut = f espan.span in
Lwt.on_termination fut (fun () -> exit_manual_span espan);
fut)
let[@inline] with_span_lwt ?force_toplevel ?__FUNCTION__ ~__FILE__ ~__LINE__
?data name f : _ Lwt.t =
if enabled () then
with_span_lwt_real_ ?force_toplevel ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data
name f
else
f 0L

26
src/lwt/trace_lwt.mli Normal file
View file

@ -0,0 +1,26 @@
(** Wrapper for tracing with Lwt.
@since NEXT_RELEASE. *)
include module type of Trace_core
val with_span_lwt :
?force_toplevel:bool ->
?__FUNCTION__:string ->
__FILE__:string ->
__LINE__:int ->
?data:(unit -> (string * user_data) list) ->
string ->
(span -> 'a Lwt.t) ->
'a Lwt.t
(** [with_span_lwt ~__FILE__ ~__LINE__ name f] calls [f span]
where [span] is a new span named with [name]. The span is
traced as being asynchronous, so each collector might represent
it differently.
@param force_toplevel if true, this span will not have a parent even if
there is one in the implicit context; ie it create a new
{!Trace_core.enter_manual_toplevel_span} in any case.
@since NEXT_RELEASE *)

33
trace-lwt.opam Normal file
View file

@ -0,0 +1,33 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.3"
synopsis: "Helpers to use Trace from Lwt"
maintainer: ["Simon Cruanes"]
authors: ["Simon Cruanes"]
license: "MIT"
tags: ["trace" "lwt" "asynchronous"]
homepage: "https://github.com/c-cube/ocaml-trace"
bug-reports: "https://github.com/c-cube/ocaml-trace/issues"
depends: [
"dune" {>= "2.9"}
"trace" {= version}
"lwt"
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"--promote-install-files=false"
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
["dune" "install" "-p" name "--create-install-files" name]
]
dev-repo: "git+https://github.com/c-cube/ocaml-trace.git"