Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
allow-prerelease-opam: true

# Until there's a new release of `trace`, we need to pin it.
- run: opam pin https://github.com/c-cube/ocaml-trace.git#HEAD -y -n
- run: opam pin . -y -n
- run: opam install odig imandrakit imandrakit-thread imandrakit-io imandrakit-log
- run: opam exec -- odig odoc --cache-dir=_doc/ imandrakit imandrakit-thread imandrakit-io imandrakit-log
Expand Down
38 changes: 3 additions & 35 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,8 @@ on:
pull_request:

jobs:
build-all-versions:
name: build
timeout-minutes: 15
strategy:
fail-fast: true
matrix:
os:
- ubuntu-latest
#- macos-latest
#- windows-latest
ocaml-compiler:
- '4.14'

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@main
- name: Use OCaml ${{ matrix.ocaml-compiler }}
uses: ocaml/setup-ocaml@v3
with:
ocaml-compiler: ${{ matrix.ocaml-compiler }}
dune-cache: true
allow-prerelease-opam: true

- run: opam pin trace --dev -y -n; opam pin trace-tef --dev -y -n
- run: opam install -t imandrakit imandrakit-log imandrakit-io --deps-only
- run: opam exec -- dune build @install -p imandrakit,imandrakit-log,imandrakit-io
- run: opam exec -- dune build @runtest -p imandrakit,imandrakit-log,imandrakit-io

# install some depopts and build+test again
- run: opam install camlzip
- run: opam exec -- dune build @install @runtest -p imandrakit,imandrakit-log,imandrakit-io

build-ocaml5:
name: build-ocaml5
build:
name: Build and Test
timeout-minutes: 15
strategy:
fail-fast: true
Expand All @@ -52,6 +20,7 @@ jobs:
ocaml-compiler:
- '5.1'
- '5.2'
- '5.3'

runs-on: ${{ matrix.os }}
steps:
Expand All @@ -63,7 +32,6 @@ jobs:
dune-cache: true
allow-prerelease-opam: true

- run: opam pin trace --dev -y -n; opam pin trace-tef --dev -y -n
- run: opam install -t imandrakit imandrakit-log imandrakit-io imandrakit-thread --deps-only
- run: opam exec -- dune build @install -p imandrakit,imandrakit-io,imandrakit-log,imandrakit-thread
- run: opam exec -- dune build @runtest -p imandrakit,imandrakit-io,imandrakit-log,imandrakit-thread
Expand Down
15 changes: 11 additions & 4 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@
(moonpool
(>= 0.11))
(yojson
(and (>= 1.6) (< 3.0)))
(and
(>= 1.6)
(< 3.0)))
(mtime
(>= 2.0))
ppx_deriving
ppx_deriving_yojson
(ppxlib
(and (>= 0.25.0) (< 0.36)))
(and
(>= 0.25.0)
(< 0.36)))
(trace
(and (>= 0.10) (< 0.11)))
(>= 0.12))
(qcheck-core
(and
(>= 0.18)
Expand Down Expand Up @@ -120,7 +124,10 @@
(depends
imandrakit
imandrakit-log
(yojson (and (>= 1.6) (< 3.0)))
(yojson
(and
(>= 1.6)
(< 3.0)))
hex
ppx_deriving
logs
Expand Down
2 changes: 1 addition & 1 deletion imandrakit.opam
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ depends: [
"ppx_deriving"
"ppx_deriving_yojson"
"ppxlib" {>= "0.25.0" & < "0.36"}
"trace" {>= "0.10" & < "0.11"}
"trace" {>= "0.12"}
"qcheck-core" {>= "0.18" & with-test}
"trace-tef" {with-test}
"ocaml-lsp-server" {with-dev-setup}
Expand Down
4 changes: 2 additions & 2 deletions src/leb128/stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ static inline void ix_leb128_varint(unsigned char *str, uint64_t i) {

// write `i` starting at `idx`
CAMLprim value caml_ix_leb128_varint(value _str, intnat idx, int64_t i) {
char *str = Bytes_val(_str);
unsigned char *str = Bytes_val(_str);
ix_leb128_varint(str + idx, i);
return Val_unit;
}

CAMLprim value caml_ix_leb128_varint_byte(value _str, value _idx, value _i) {
CAMLparam3(_str, _idx, _i);
char *str = Bytes_val(_str);
unsigned char *str = Bytes_val(_str);
int idx = Int_val(_idx);
int64_t i = Int64_val(_i);
ix_leb128_varint(str + idx, i);
Expand Down
60 changes: 25 additions & 35 deletions src/log/trace_async.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type span_kind =
[@@deriving eq, twine, show { with_path = false }]

type Trace.extension_event +=
| Ev_link_span of Trace.explicit_span * Trace.explicit_span_ctx
| Ev_link_span of Trace.span * Trace.span
(** Link the given span to the given context. The context isn't the
parent, but the link can be used to correlate both spans. *)
| Ev_record_exn of {
Expand All @@ -20,75 +20,69 @@ type Trace.extension_event +=
error: bool; (** Is this an actual internal error? *)
}
(** Record exception and potentially turn span to an error *)
| Ev_push_async_parent of Trace.explicit_span_ctx
(** Set current async span *)
| Ev_pop_async_parent of Trace.explicit_span_ctx
(** Remove current async span *)
| Ev_push_async_parent of Trace.span (** Set current async span *)
| Ev_pop_async_parent of Trace.span (** Remove current async span *)
| Ev_set_span_kind of Trace.span * span_kind

(** Link the given span to the given context *)
let[@inline] link_spans (sp1 : Trace.explicit_span)
~(src : Trace.explicit_span_ctx) : unit =
let[@inline] link_spans (sp1 : Trace.span) ~(src : Trace.span) : unit =
if Trace.enabled () then Trace.extension_event @@ Ev_link_span (sp1, src)

let[@inline] set_span_kind sp k : unit =
if Trace.enabled () then Trace.extension_event @@ Ev_set_span_kind (sp, k)

(** Current parent scope for async spans *)
let k_span_ctx : Trace.explicit_span_ctx Hmap.key = Hmap.Key.create ()
let k_span_ctx : Trace.span Hmap.key = Hmap.Key.create ()

(** Record exception in the span *)
let add_exn_to_span ~is_error (sp : Trace.span) (exn : exn)
(bt : Printexc.raw_backtrace) =
Trace.extension_event @@ Ev_record_exn { sp; exn; bt; error = is_error }

let push_async_parent (sp : Trace.explicit_span_ctx) : unit =
let push_async_parent (sp : Trace.span) : unit =
Trace.extension_event @@ Ev_push_async_parent sp

let pop_async_parent (sp : Trace.explicit_span_ctx) : unit =
let pop_async_parent (sp : Trace.span) : unit =
Trace.extension_event @@ Ev_pop_async_parent sp

let[@inline] with_async_parent (sp : Trace.explicit_span_ctx) f =
let[@inline] with_async_parent (sp : Trace.span) f =
push_async_parent sp;
Fun.protect ~finally:(fun () -> pop_async_parent sp) f

open struct
let auto_enrich_span_l_ : (Trace.explicit_span -> unit) list Atomic.t =
Atomic.make []
let auto_enrich_span_l_ : (Trace.span -> unit) list Atomic.t = Atomic.make []

let with_span_real_ ~level ~parent ?data ?__FUNCTION__ ~__FILE__ ~__LINE__
name (f : Trace_core.explicit_span * Trace_core.explicit_span_ctx -> 'a) :
'a =
name (f : Trace_core.span * Trace_core.span -> 'a) : 'a =
let span =
Trace.enter_manual_span ~parent ~flavor:`Async ?data ~level ?__FUNCTION__
Trace.enter_span ~parent ~flavor:`Async ?data ~level ?__FUNCTION__
~__FILE__ ~__LINE__ name
in
push_async_parent (Trace_core.ctx_of_span span);
push_async_parent span;

(* apply automatic enrichment *)
if span.span != Trace.Collector.dummy_span then
if span != Trace.Collector.dummy_span then
List.iter (fun f -> f span) (Atomic.get auto_enrich_span_l_);

let cleanup () =
pop_async_parent (Trace_core.ctx_of_span span);
Trace.exit_manual_span span
pop_async_parent span;
Trace.exit_span span
in

try
let x = f (span, Trace.ctx_of_span span) in
let x = f (span, span) in
cleanup ();
x
with e ->
let bt = Printexc.get_raw_backtrace () in
add_exn_to_span ~is_error:true span.span e bt;
add_exn_to_span ~is_error:true span e bt;
cleanup ();
Printexc.raise_with_backtrace e bt
end

(** Wrap [f()] in a async span. *)
let with_span ?(level = Trace.get_default_level ()) ?parent ?data ?__FUNCTION__
~__FILE__ ~__LINE__ name
(f : Trace.explicit_span * Trace.explicit_span_ctx -> 'a) : 'a =
~__FILE__ ~__LINE__ name (f : Trace.span * Trace.span -> 'a) : 'a =
let trace_enabled = Trace.enabled () in
if trace_enabled && level <= Trace.get_current_level () then
with_span_real_ ~level ~parent ?data ?__FUNCTION__ ~__FILE__ ~__LINE__ name
Expand All @@ -98,11 +92,8 @@ let with_span ?(level = Trace.get_default_level ()) ?parent ?data ?__FUNCTION__
| Some p when trace_enabled ->
(* make sure we still link spans in [f()] to [p] *)
let@ () = with_async_parent p in
f (Trace.Collector.dummy_explicit_span, p)
| _ ->
f
( Trace.Collector.dummy_explicit_span,
Trace.Collector.dummy_explicit_span_ctx )
f (Trace.Collector.dummy_span, p)
| _ -> f (Trace.Collector.dummy_span, Trace.Collector.dummy_span)
)

open struct
Expand All @@ -112,21 +103,20 @@ open struct
| Some v -> (name, `String v) :: l
end

let enrich_span_service ?version (span : Trace.explicit_span) : unit =
let enrich_span_service ?version (span : Trace.span) : unit =
let data = [] |> cons_assoc_opt_ "service.version" version in
Trace.add_data_to_manual_span span data
Trace.add_data_to_span span data

let enrich_span_deployment ?id ?name ~deployment (span : Trace.explicit_span) :
unit =
let enrich_span_deployment ?id ?name ~deployment (span : Trace.span) : unit =
let data =
[ "deployment.environment.name", `String deployment ]
|> cons_assoc_opt_ "deployment.id" id
|> cons_assoc_opt_ "deployment.name" name
in
Trace.add_data_to_manual_span span data
Trace.add_data_to_span span data

(** Add a hook that will be called on every explicit span *)
let add_auto_enrich_span (f : Trace.explicit_span -> unit) : unit =
let add_auto_enrich_span (f : Trace.span -> unit) : unit =
while
let l = Atomic.get auto_enrich_span_l_ in
not (Atomic.compare_and_set auto_enrich_span_l_ l (f :: l))
Expand Down
3 changes: 2 additions & 1 deletion src/twine-utils/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(executable
(name main)
(modes (byte exe))
(modes
(byte exe))
(public_name twine_utils)
(package twine-utils)
(preprocess
Expand Down