feat otel: add Span_kind.t, add {kind,set_kind} to Scope

This commit is contained in:
Simon Cruanes 2024-10-22 13:26:40 -04:00
parent fdee7fe2dd
commit c71caa93be

View file

@ -834,6 +834,29 @@ end = struct
let make ~message ~code = { message; code } let make ~message ~code = { message; code }
end end
(** @since NEXT_RELEASE *)
module Span_kind : sig
open Proto.Trace
type t = span_span_kind =
| Span_kind_unspecified
| Span_kind_internal
| Span_kind_server
| Span_kind_client
| Span_kind_producer
| Span_kind_consumer
end = struct
open Proto.Trace
type t = span_span_kind =
| Span_kind_unspecified
| Span_kind_internal
| Span_kind_server
| Span_kind_client
| Span_kind_producer
| Span_kind_consumer
end
(** {2 Scopes} *) (** {2 Scopes} *)
(** Scopes. (** Scopes.
@ -857,6 +880,8 @@ module Scope : sig
val status : t -> Span_status.t option val status : t -> Span_status.t option
val kind : t -> Span_kind.t option
val make : val make :
trace_id:Trace_id.t -> trace_id:Trace_id.t ->
span_id:Span_id.t -> span_id:Span_id.t ->
@ -867,6 +892,14 @@ module Scope : sig
unit -> unit ->
t t
val to_span_link :
?trace_state:string ->
?attrs:key_value list ->
?dropped_attributes_count:int ->
t ->
Span_link.t
(** Turn the scope into a span link *)
val to_span_ctx : t -> Span_ctx.t val to_span_ctx : t -> Span_ctx.t
(** Turn the scope into a span context *) (** Turn the scope into a span context *)
@ -896,6 +929,10 @@ module Scope : sig
Note that this function will be Note that this function will be
called only if there is an instrumentation backend. *) called only if there is an instrumentation backend. *)
val set_kind : t -> Span_kind.t -> unit
(** Set the span's kind.
@since NEXT_RELEASE *)
val ambient_scope_key : t Ambient_context.key val ambient_scope_key : t Ambient_context.key
(** The opaque key necessary to access/set the ambient scope with (** The opaque key necessary to access/set the ambient scope with
{!Ambient_context}. *) {!Ambient_context}. *)
@ -916,6 +953,7 @@ end = struct
| Attr of key_value * item_list | Attr of key_value * item_list
| Span_link of Span_link.t * item_list | Span_link of Span_link.t * item_list
| Span_status of Span_status.t * item_list | Span_status of Span_status.t * item_list
| Span_kind of Span_kind.t * item_list
type t = { type t = {
trace_id: Trace_id.t; trace_id: Trace_id.t;
@ -927,7 +965,8 @@ end = struct
let rec loop acc = function let rec loop acc = function
| Nil -> acc | Nil -> acc
| Attr (attr, l) -> loop (attr :: acc) l | Attr (attr, l) -> loop (attr :: acc) l
| Ev (_, l) | Span_link (_, l) | Span_status (_, l) -> loop acc l | Ev (_, l) | Span_kind (_, l) | Span_link (_, l) | Span_status (_, l) ->
loop acc l
in in
loop [] scope.items loop [] scope.items
@ -935,7 +974,9 @@ end = struct
let rec loop acc = function let rec loop acc = function
| Nil -> acc | Nil -> acc
| Ev (event, l) -> loop (event :: acc) l | Ev (event, l) -> loop (event :: acc) l
| Attr (_, l) | Span_link (_, l) | Span_status (_, l) -> loop acc l | Attr (_, l) | Span_kind (_, l) | Span_link (_, l) | Span_status (_, l)
->
loop acc l
in in
loop [] scope.items loop [] scope.items
@ -943,17 +984,27 @@ end = struct
let rec loop acc = function let rec loop acc = function
| Nil -> acc | Nil -> acc
| Span_link (span_link, l) -> loop (span_link :: acc) l | Span_link (span_link, l) -> loop (span_link :: acc) l
| Ev (_, l) | Attr (_, l) | Span_status (_, l) -> loop acc l | Ev (_, l) | Span_kind (_, l) | Attr (_, l) | Span_status (_, l) ->
loop acc l
in in
loop [] scope.items loop [] scope.items
let status scope = let status scope =
let rec loop acc = function let rec loop = function
| Nil -> acc | Nil -> None
| Span_status (status, _) -> Some status | Span_status (status, _) -> Some status
| Ev (_, l) | Attr (_, l) | Span_link (_, l) -> loop acc l | Ev (_, l) | Attr (_, l) | Span_kind (_, l) | Span_link (_, l) -> loop l
in in
loop None scope.items loop scope.items
let kind scope =
let rec loop = function
| Nil -> None
| Span_kind (k, _) -> Some k
| Ev (_, l) | Span_status (_, l) | Attr (_, l) | Span_link (_, l) ->
loop l
in
loop scope.items
let make ~trace_id ~span_id ?(events = []) ?(attrs = []) ?(links = []) ?status let make ~trace_id ~span_id ?(events = []) ?(attrs = []) ?(links = []) ?status
() : t = () : t =
@ -971,6 +1022,11 @@ end = struct
in in
{ trace_id; span_id; items } { trace_id; span_id; items }
let[@inline] to_span_link ?trace_state ?attrs ?dropped_attributes_count
(self : t) : Span_link.t =
Span_link.make ?trace_state ?attrs ?dropped_attributes_count
~trace_id:self.trace_id ~span_id:self.span_id ()
let[@inline] to_span_ctx (self : t) : Span_ctx.t = let[@inline] to_span_ctx (self : t) : Span_ctx.t =
Span_ctx.make ~trace_id:self.trace_id ~parent_id:self.span_id () Span_ctx.make ~trace_id:self.trace_id ~parent_id:self.span_id ()
@ -1008,6 +1064,9 @@ end = struct
if Collector.has_backend () then if Collector.has_backend () then
scope.items <- Span_status (status, scope.items) scope.items <- Span_status (status, scope.items)
let set_kind (scope : t) (k : Span_kind.t) : unit =
if Collector.has_backend () then scope.items <- Span_kind (k, scope.items)
let ambient_scope_key : t Ambient_context.key = Ambient_context.create_key () let ambient_scope_key : t Ambient_context.key = Ambient_context.create_key ()
let get_ambient_scope ?scope () : t option = let get_ambient_scope ?scope () : t option =
@ -1034,7 +1093,7 @@ module Span : sig
type id = Span_id.t type id = Span_id.t
type nonrec kind = span_span_kind = type kind = Span_kind.t =
| Span_kind_unspecified | Span_kind_unspecified
| Span_kind_internal | Span_kind_internal
| Span_kind_server | Span_kind_server
@ -1079,7 +1138,7 @@ end = struct
type id = Span_id.t type id = Span_id.t
type nonrec kind = span_span_kind = type kind = Span_kind.t =
| Span_kind_unspecified | Span_kind_unspecified
| Span_kind_internal | Span_kind_internal
| Span_kind_server | Span_kind_server