From adcbd943377e1b4be4e143851944a89c921ab145 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 22 Oct 2024 13:26:40 -0400 Subject: [PATCH] feat otel: add `Span_kind.t`, add {kind,set_kind} to `Scope` --- src/core/opentelemetry.ml | 80 +++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 11 deletions(-) diff --git a/src/core/opentelemetry.ml b/src/core/opentelemetry.ml index 8a0ce9d..285697d 100644 --- a/src/core/opentelemetry.ml +++ b/src/core/opentelemetry.ml @@ -822,6 +822,29 @@ end = struct let make ~message ~code = { message; code } 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} *) (** Scopes. @@ -845,6 +868,8 @@ module Scope : sig val status : t -> Span_status.t option + val kind : t -> Span_kind.t option + val make : trace_id:Trace_id.t -> span_id:Span_id.t -> @@ -855,6 +880,14 @@ module Scope : sig unit -> 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 (** Turn the scope into a span context *) @@ -884,6 +917,10 @@ module Scope : sig Note that this function will be 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 (** The opaque key necessary to access/set the ambient scope with {!Ambient_context}. *) @@ -904,6 +941,7 @@ end = struct | Attr of key_value * item_list | Span_link of Span_link.t * item_list | Span_status of Span_status.t * item_list + | Span_kind of Span_kind.t * item_list type t = { trace_id: Trace_id.t; @@ -915,7 +953,8 @@ end = struct let rec loop acc = function | Nil -> acc | 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 loop [] scope.items @@ -923,7 +962,9 @@ end = struct let rec loop acc = function | Nil -> acc | 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 loop [] scope.items @@ -931,17 +972,27 @@ end = struct let rec loop acc = function | Nil -> acc | 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 loop [] scope.items let status scope = - let rec loop acc = function - | Nil -> acc + let rec loop = function + | Nil -> None | 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 - 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 () : t = @@ -959,6 +1010,11 @@ end = struct in { 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 = Span_ctx.make ~trace_id:self.trace_id ~parent_id:self.span_id () @@ -993,9 +1049,11 @@ end = struct scope.items (links ()) let set_status (scope : t) (status : Span_status.t) : unit = - if Collector.has_backend () then ( + if Collector.has_backend () then 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 () @@ -1023,7 +1081,7 @@ module Span : sig type id = Span_id.t - type nonrec kind = span_span_kind = + type kind = Span_kind.t = | Span_kind_unspecified | Span_kind_internal | Span_kind_server @@ -1068,7 +1126,7 @@ end = struct type id = Span_id.t - type nonrec kind = span_span_kind = + type kind = Span_kind.t = | Span_kind_unspecified | Span_kind_internal | Span_kind_server