@@ -127,7 +127,7 @@ let ok_none = Ok None
127127module Status = struct
128128 type status_code = Unset | Ok | Error [@@ deriving rpcty ]
129129
130- type t = {status_code : status_code ; description : string option }
130+ type t = {status_code : status_code ; _description : string option }
131131end
132132
133133module Attributes = struct
151151module SpanContext = struct
152152 type t = {trace_id : string ; span_id : string } [@@ deriving rpcty ]
153153
154+ let context trace_id span_id = {trace_id; span_id}
155+
154156 let to_traceparent t = Printf. sprintf " 00-%s-%s-01" t.trace_id t.span_id
155157
156158 let of_traceparent traceparent =
@@ -167,7 +169,7 @@ module SpanContext = struct
167169end
168170
169171module SpanLink = struct
170- type t = {context : SpanContext .t ; attributes : (string * string ) list }
172+ type t = {_context : SpanContext .t ; _attributes : (string * string ) list }
171173end
172174
173175module Span = struct
@@ -208,7 +210,7 @@ module Span = struct
208210 (* Using gettimeofday over Mtime as it is better for sharing timestamps between the systems *)
209211 let begin_time = Unix. gettimeofday () in
210212 let end_time = None in
211- let status : Status.t = {status_code= Status. Unset ; description = None } in
213+ let status : Status.t = {status_code= Status. Unset ; _description = None } in
212214 let links = [] in
213215 let events = [] in
214216 {
@@ -250,7 +252,7 @@ module Span = struct
250252 let set_span_kind span span_kind = {span with span_kind}
251253
252254 let add_link span context attributes =
253- let link : SpanLink.t = {context; attributes} in
255+ let link : SpanLink.t = {_context = context; _attributes = attributes} in
254256 {span with links= link :: span .links}
255257
256258 let add_event span name attributes =
@@ -263,7 +265,7 @@ module Span = struct
263265 | exn , stacktrace -> (
264266 let msg = Printexc. to_string exn in
265267 let exn_type = Printexc. exn_slot_name exn in
266- let description =
268+ let _description =
267269 Some
268270 (Printf. sprintf " Error: %s Type: %s Backtrace: %s" msg exn_type
269271 stacktrace
@@ -286,17 +288,17 @@ module Span = struct
286288 span.attributes
287289 (Attributes. of_list exn_attributes)
288290 in
289- {span with status= {status_code; description }; attributes}
291+ {span with status= {status_code; _description }; attributes}
290292 | _ ->
291293 span
292294 )
293295
294296 let set_ok span =
295- let description = None in
297+ let _description = None in
296298 let status_code = Status. Ok in
297299 match span.status.status_code with
298300 | Unset ->
299- {span with status= {status_code; description }}
301+ {span with status= {status_code; _description }}
300302 | _ ->
301303 span
302304end
@@ -311,7 +313,7 @@ module Spans = struct
311313 Hashtbl. length spans
312314 )
313315
314- let max_spans = Atomic. make 1000
316+ let max_spans = Atomic. make 2500
315317
316318 let set_max_spans x = Atomic. set max_spans x
317319
@@ -519,8 +521,8 @@ module TracerProvider = struct
519521 get_tracer_providers_unlocked
520522
521523 let set ?enabled ?attributes ?endpoints ~uuid () =
522- let update_provider (provider : t ) ?( enabled = provider.enabled) attributes
523- endpoints =
524+ let update_provider (provider : t ) enabled attributes endpoints =
525+ let enabled = Option. value ~default: provider.enabled enabled in
524526 let attributes : string Attributes.t =
525527 Option. fold ~none: provider.attributes ~some: Attributes. of_list
526528 attributes
@@ -537,7 +539,7 @@ module TracerProvider = struct
537539 let provider =
538540 match Hashtbl. find_opt tracer_providers uuid with
539541 | Some (provider : t ) ->
540- update_provider provider ? enabled attributes endpoints
542+ update_provider provider enabled attributes endpoints
541543 | None ->
542544 fail " The TracerProvider : %s does not exist" uuid
543545 in
@@ -564,9 +566,9 @@ module TracerProvider = struct
564566end
565567
566568module Tracer = struct
567- type t = {name : string ; provider : TracerProvider .t }
569+ type t = {_name : string ; provider : TracerProvider .t }
568570
569- let create ~name ~provider = {name; provider}
571+ let create ~name ~provider = {_name = name; provider}
570572
571573 let no_op =
572574 let provider : TracerProvider.t =
@@ -577,7 +579,7 @@ module Tracer = struct
577579 ; enabled= false
578580 }
579581 in
580- {name = " " ; provider}
582+ {_name = " " ; provider}
581583
582584 let get_tracer ~name =
583585 if Atomic. get observe then (
@@ -598,7 +600,7 @@ module Tracer = struct
598600 let span_of_span_context context name : Span.t =
599601 {
600602 context
601- ; status= {status_code= Status. Unset ; description = None }
603+ ; status= {status_code= Status. Unset ; _description = None }
602604 ; name
603605 ; parent= None
604606 ; span_kind= SpanKind. Client (* This will be the span of the client call*)
@@ -624,6 +626,30 @@ module Tracer = struct
624626 let span = Span. start ~attributes ~name ~parent ~span_kind () in
625627 Spans. add_to_spans ~span ; Ok (Some span)
626628
629+ let update_span_with_parent span (parent : Span.t option ) =
630+ if Atomic. get observe then
631+ match parent with
632+ | None ->
633+ Some span
634+ | Some parent ->
635+ span
636+ |> Spans. remove_from_spans
637+ |> Option. map (fun existing_span ->
638+ let old_context = Span. get_context existing_span in
639+ let new_context : SpanContext.t =
640+ SpanContext. context
641+ (SpanContext. trace_id_of_span_context parent.context)
642+ old_context.span_id
643+ in
644+ let updated_span = {existing_span with parent= Some parent} in
645+ let updated_span = {updated_span with context= new_context} in
646+
647+ let () = Spans. add_to_spans ~span: updated_span in
648+ updated_span
649+ )
650+ else
651+ Some span
652+
627653 let finish ?error span =
628654 Ok
629655 (Option. map
@@ -673,6 +699,13 @@ let with_tracing ?(attributes = []) ?(parent = None) ~name f =
673699 ) else
674700 f None
675701
702+ let with_child_trace ?attributes parent ~name f =
703+ match parent with
704+ | None ->
705+ f None
706+ | Some _ as parent ->
707+ with_tracing ?attributes ~parent ~name f
708+
676709module EnvHelpers = struct
677710 let traceparent_key = " TRACEPARENT"
678711
0 commit comments