Skip to content

Commit

Permalink
feat otel: add Span_kind.t, add {kind,set_kind} to Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Oct 22, 2024
1 parent 9813ec6 commit adcbd94
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions src/core/opentelemetry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 ->
Expand All @@ -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 *)

Expand Down Expand Up @@ -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}. *)
Expand All @@ -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;
Expand All @@ -915,33 +953,46 @@ 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

let events scope =
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

let links scope =
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 =
Expand All @@ -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 ()

Expand Down Expand Up @@ -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 ()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit adcbd94

Please sign in to comment.