Skip to content

Commit 847aeba

Browse files
committed
CP-48195: Tracing -- Move create\set\destroy\...
Moves the following: - `create` under `TracerProvider`; - `set` under `TracerProvider`; - `destroy` under `TracerProvider; - `get_tracer_providers` unde `TracerProvider`; - `get_tracer` under `Tracer`. Adds documentation for `TracerProvider` module. It kept being confusing of what `Tracing.set` does unless I was going through the implementation again and again. Therefore, I moved some of the functions so that their functionality becomes (hopefully) more intuitive. Signed-off-by: Gabriel Buica <danutgabriel.buica@cloud.com>
1 parent e360251 commit 847aeba

File tree

7 files changed

+162
-131
lines changed

7 files changed

+162
-131
lines changed

ocaml/libs/tracing/test_tracing.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let with_observe_mode_check flag f = f () ; assert_observe_mode flag
4646

4747
let get_provider name_label =
4848
let providers =
49-
Tracing.get_tracer_providers ()
49+
Tracing.TracerProvider.get_tracer_providers ()
5050
|> List.filter (fun provider ->
5151
String.equal
5252
(Tracing.TracerProvider.get_name_label provider)
@@ -60,11 +60,14 @@ let get_provider name_label =
6060
Alcotest.failf "expected only one provider"
6161

6262
let create_with (enabled, attributes, endpoints, name_label, uuid) =
63-
let () = Tracing.create ~enabled ~attributes ~endpoints ~name_label ~uuid in
63+
let () =
64+
Tracing.TracerProvider.create ~enabled ~attributes ~endpoints ~name_label
65+
~uuid
66+
in
6467
get_provider name_label
6568

6669
let test_destroy_all_providers uuids =
67-
let () = List.iter (fun uuid -> Tracing.destroy ~uuid) uuids in
70+
let () = List.iter (fun uuid -> Tracing.TracerProvider.destroy ~uuid) uuids in
6871
assert_observe_mode false
6972

7073
let test_create_and_destroy () =
@@ -145,7 +148,7 @@ let test_create_and_destroy () =
145148

146149
let test_set_tracer_provider () =
147150
let test_set_with provider (enabled, attributes, endpoints, uuid) =
148-
Tracing.set ~enabled ~attributes ~endpoints ~uuid () ;
151+
Tracing.TracerProvider.set ~enabled ~attributes ~endpoints ~uuid () ;
149152
let updated_provider =
150153
provider |> Tracing.TracerProvider.get_name_label |> get_provider
151154
in

ocaml/libs/tracing/tracing.ml

Lines changed: 90 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,82 @@ module TracerProvider = struct
477477
let get_endpoints t = t.endpoints
478478

479479
let get_enabled t = t.enabled
480+
481+
let lock = Mutex.create ()
482+
483+
let tracer_providers = Hashtbl.create 100
484+
485+
let create ~enabled ~attributes ~endpoints ~name_label ~uuid =
486+
let provider : t =
487+
let endpoints = List.map endpoint_of_string endpoints in
488+
let attributes = Attributes.of_list attributes in
489+
{name_label; attributes; endpoints; enabled}
490+
in
491+
Xapi_stdext_threads.Threadext.Mutex.execute lock (fun () ->
492+
( match Hashtbl.find_opt tracer_providers uuid with
493+
| None ->
494+
Hashtbl.add tracer_providers uuid provider
495+
| Some _ ->
496+
(* CP-45469: It is ok not to have an exception here since it is unlikely that the
497+
user has caused the issue, so no need to propagate back. It is also
498+
handy to not change the control flow since calls like cluster_pool_resync
499+
might not be aware that a TracerProvider has already been created.*)
500+
error "Tracing : TracerProvider %s already exists" name_label
501+
) ;
502+
if enabled then set_observe true
503+
)
504+
505+
let get_tracer_providers_unlocked () =
506+
Hashtbl.fold (fun _ provider acc -> provider :: acc) tracer_providers []
507+
508+
let get_tracer_providers () =
509+
Xapi_stdext_threads.Threadext.Mutex.execute lock
510+
get_tracer_providers_unlocked
511+
512+
let set ?enabled ?attributes ?endpoints ~uuid () =
513+
let update_provider (provider : t) enabled attributes endpoints =
514+
let enabled = Option.value ~default:provider.enabled enabled in
515+
let attributes : string Attributes.t =
516+
Option.fold ~none:provider.attributes ~some:Attributes.of_list
517+
attributes
518+
in
519+
let endpoints =
520+
Option.fold ~none:provider.endpoints
521+
~some:(List.map endpoint_of_string)
522+
endpoints
523+
in
524+
{provider with enabled; attributes; endpoints}
525+
in
526+
527+
Xapi_stdext_threads.Threadext.Mutex.execute lock (fun () ->
528+
let provider =
529+
match Hashtbl.find_opt tracer_providers uuid with
530+
| Some (provider : t) ->
531+
update_provider provider enabled attributes endpoints
532+
| None ->
533+
failwith
534+
(Printf.sprintf "The TracerProvider : %s does not exist" uuid)
535+
in
536+
Hashtbl.replace tracer_providers uuid provider ;
537+
if
538+
List.for_all
539+
(fun provider -> not provider.enabled)
540+
(get_tracer_providers_unlocked ())
541+
then (
542+
set_observe false ;
543+
Xapi_stdext_threads.Threadext.Mutex.execute Spans.lock (fun () ->
544+
Hashtbl.clear Spans.spans ;
545+
Hashtbl.clear Spans.finished_spans
546+
)
547+
) else
548+
set_observe true
549+
)
550+
551+
let destroy ~uuid =
552+
Xapi_stdext_threads.Threadext.Mutex.execute lock (fun () ->
553+
let _ = Hashtbl.remove tracer_providers uuid in
554+
if Hashtbl.length tracer_providers = 0 then set_observe false else ()
555+
)
480556
end
481557

482558
module Tracer = struct
@@ -495,6 +571,19 @@ module Tracer = struct
495571
in
496572
{name= ""; provider}
497573

574+
let get_tracer ~name =
575+
let providers =
576+
Xapi_stdext_threads.Threadext.Mutex.execute TracerProvider.lock
577+
TracerProvider.get_tracer_providers_unlocked
578+
in
579+
580+
match List.find_opt TracerProvider.get_enabled providers with
581+
| Some provider ->
582+
create ~name ~provider
583+
| None ->
584+
warn "No provider found for tracing %s" name ;
585+
no_op
586+
498587
let span_of_span_context context name : Span.t =
499588
{
500589
context
@@ -549,100 +638,12 @@ module Tracer = struct
549638
Spans.finished_span_hashtbl_is_empty ()
550639
end
551640

552-
let lock = Mutex.create ()
553-
554-
let tracer_providers = Hashtbl.create 100
555-
556-
let get_tracer_providers_unlocked () =
557-
Hashtbl.fold (fun _ provider acc -> provider :: acc) tracer_providers []
558-
559-
let get_tracer_providers () =
560-
Xapi_stdext_threads.Threadext.Mutex.execute lock get_tracer_providers_unlocked
561-
562-
let set ?enabled ?attributes ?endpoints ~uuid () =
563-
let update_provider (provider : TracerProvider.t) enabled attributes endpoints
564-
=
565-
let enabled = Option.value ~default:provider.enabled enabled in
566-
let attributes : string Attributes.t =
567-
Option.fold ~none:provider.attributes ~some:Attributes.of_list attributes
568-
in
569-
let endpoints =
570-
Option.fold ~none:provider.endpoints
571-
~some:(List.map endpoint_of_string)
572-
endpoints
573-
in
574-
{provider with enabled; attributes; endpoints}
575-
in
576-
577-
Xapi_stdext_threads.Threadext.Mutex.execute lock (fun () ->
578-
let provider =
579-
match Hashtbl.find_opt tracer_providers uuid with
580-
| Some (provider : TracerProvider.t) ->
581-
update_provider provider enabled attributes endpoints
582-
| None ->
583-
failwith
584-
(Printf.sprintf "The TracerProvider : %s does not exist" uuid)
585-
in
586-
Hashtbl.replace tracer_providers uuid provider ;
587-
if
588-
List.for_all
589-
(fun provider -> not provider.TracerProvider.enabled)
590-
(get_tracer_providers_unlocked ())
591-
then (
592-
set_observe false ;
593-
Xapi_stdext_threads.Threadext.Mutex.execute Spans.lock (fun () ->
594-
Hashtbl.clear Spans.spans ;
595-
Hashtbl.clear Spans.finished_spans
596-
)
597-
) else
598-
set_observe true
599-
)
600-
601-
let create ~enabled ~attributes ~endpoints ~name_label ~uuid =
602-
let provider : TracerProvider.t =
603-
let endpoints = List.map endpoint_of_string endpoints in
604-
let attributes = Attributes.of_list attributes in
605-
{name_label; attributes; endpoints; enabled}
606-
in
607-
Xapi_stdext_threads.Threadext.Mutex.execute lock (fun () ->
608-
( match Hashtbl.find_opt tracer_providers uuid with
609-
| None ->
610-
Hashtbl.add tracer_providers uuid provider
611-
| Some _ ->
612-
(* CP-45469: It is ok not to have an exception here since it is unlikely that the
613-
user has caused the issue, so no need to propagate back. It is also
614-
handy to not change the control flow since calls like cluster_pool_resync
615-
might not be aware that a TracerProvider has already been created.*)
616-
error "Tracing : TracerProvider %s already exists" name_label
617-
) ;
618-
if enabled then set_observe true
619-
)
620-
621-
let destroy ~uuid =
622-
Xapi_stdext_threads.Threadext.Mutex.execute lock (fun () ->
623-
let _ = Hashtbl.remove tracer_providers uuid in
624-
if Hashtbl.length tracer_providers = 0 then set_observe false else ()
625-
)
626-
627-
let get_tracer ~name =
628-
let providers =
629-
Xapi_stdext_threads.Threadext.Mutex.execute lock
630-
get_tracer_providers_unlocked
631-
in
632-
633-
match List.find_opt TracerProvider.get_enabled providers with
634-
| Some provider ->
635-
Tracer.create ~name ~provider
636-
| None ->
637-
(* warn "No provider found for tracing %s" name ; *)
638-
Tracer.no_op
639-
640641
let enable_span_garbage_collector ?(timeout = 86400.) () =
641642
Spans.GC.initialise_thread ~timeout
642643

643644
let with_tracing ?(attributes = []) ?(parent = None) ~name f =
644645
if Atomic.get observe then (
645-
let tracer = get_tracer ~name in
646+
let tracer = Tracer.get_tracer ~name in
646647
match Tracer.start ~tracer ~attributes ~name ~parent () with
647648
| Ok span -> (
648649
try

ocaml/libs/tracing/tracing.mli

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ end
110110
module Tracer : sig
111111
type t
112112

113+
val get_tracer : name:string -> t
114+
113115
val span_of_span_context : SpanContext.t -> string -> Span.t
114116

115117
val start :
@@ -131,40 +133,61 @@ module Tracer : sig
131133
val finished_span_hashtbl_is_empty : unit -> bool
132134
end
133135

136+
(** [TracerProvider] module provides ways to intereact with the tracer providers.
137+
*)
134138
module TracerProvider : sig
139+
(** Type that represents a tracer provider.*)
135140
type t
136141

142+
val create :
143+
enabled:bool
144+
-> attributes:(string * string) list
145+
-> endpoints:string list
146+
-> name_label:string
147+
-> uuid:string
148+
-> unit
149+
(** [create ~enabled ~attributes ~endpoints ~name_label ~uuid] initializes a
150+
tracer provider based on the following parameters: [enabled], [attributes],
151+
[endpoints], [name_label], and [uuid]. *)
152+
153+
val set :
154+
?enabled:bool
155+
-> ?attributes:(string * string) list
156+
-> ?endpoints:string list
157+
-> uuid:string
158+
-> unit
159+
-> unit
160+
(** [set ?enabled ?attributes ?endpoints ~uuid ()] updates the tracer provider
161+
identified by the given [uuid] with the new configuration paremeters:
162+
[enabled], [attributes], and [endpoints].
163+
164+
If any of the configuration parameters are
165+
missing, the old ones are kept.
166+
167+
Raises [Failure] if there are no tracer provider with the given [uuid].
168+
*)
169+
170+
val destroy : uuid:string -> unit
171+
(** [destroy ~uuid] destroys the tracer provider with the given [uuid].
172+
If there are no tracer provider with the given [uuid], it does nothing.
173+
*)
174+
175+
val get_tracer_providers : unit -> t list
176+
(** [get_tracer_providers] returns a list of all existing tracer providers. *)
177+
137178
val get_name_label : t -> string
179+
(** [get_name_label provider] returns the name label of the [provider]. *)
138180

139181
val get_attributes : t -> (string * string) list
182+
(** [get_attributes provider] returns the list of attributes of the [provider]. *)
140183

141184
val get_endpoints : t -> endpoint list
185+
(** [get_endpoints provider] returns list of endpoints of the [provider]. *)
142186

143187
val get_enabled : t -> bool
188+
(** [get_name_label provider] returns whether or not the [provider] is enabled. *)
144189
end
145190

146-
val set :
147-
?enabled:bool
148-
-> ?attributes:(string * string) list
149-
-> ?endpoints:string list
150-
-> uuid:string
151-
-> unit
152-
-> unit
153-
154-
val create :
155-
enabled:bool
156-
-> attributes:(string * string) list
157-
-> endpoints:string list
158-
-> name_label:string
159-
-> uuid:string
160-
-> unit
161-
162-
val destroy : uuid:string -> unit
163-
164-
val get_tracer_providers : unit -> TracerProvider.t list
165-
166-
val get_tracer : name:string -> Tracer.t
167-
168191
val enable_span_garbage_collector : ?timeout:float -> unit -> unit
169192

170193
val with_tracing :

ocaml/libs/tracing/tracing_export.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ module Destination = struct
280280
let@ parent =
281281
with_tracing ~parent:None ~attributes ~name:"Tracing.flush_spans"
282282
in
283-
get_tracer_providers ()
283+
TracerProvider.get_tracer_providers ()
284284
|> List.filter TracerProvider.get_enabled
285285
|> List.concat_map TracerProvider.get_endpoints
286286
|> List.iter (export_to_endpoint parent span_list)

ocaml/tests/test_observer.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ end
6262

6363
module TracerProvider = struct
6464
let assert_num_observers ~__context x =
65-
let providers = get_tracer_providers () in
65+
let providers = TracerProvider.get_tracer_providers () in
6666
Alcotest.(check int)
6767
(Printf.sprintf "%d provider(s) exists in lib " x)
6868
x (List.length providers)
6969

7070
let find_provider_exn ~name =
71-
let providers = get_tracer_providers () in
71+
let providers = TracerProvider.get_tracer_providers () in
7272
match
7373
List.find_opt (fun x -> TracerProvider.get_name_label x = name) providers
7474
with
@@ -135,12 +135,12 @@ let test_create ~__context ?(name_label = "test-observer") ?(enabled = false) ()
135135
self
136136

137137
let start_test_span () =
138-
let tracer = get_tracer ~name:"test-observer" in
138+
let tracer = Tracer.get_tracer ~name:"test-observer" in
139139
let span = Tracer.start ~tracer ~name:"test_task" ~parent:None () in
140140
span
141141

142142
let start_test_trace () =
143-
let tracer = get_tracer ~name:"test-observer" in
143+
let tracer = Tracer.get_tracer ~name:"test-observer" in
144144
let root =
145145
Tracer.start ~tracer ~name:"test_task" ~parent:None ()
146146
|> Result.value ~default:None

0 commit comments

Comments
 (0)