Skip to content

Commit 75af768

Browse files
committed
CP-309305: Split Spans.since into chunks for exporting
Http exporting appears to get overwhelmed when too many spans are exported at the same time. This adds the option to export a smaller chunk of spans at a time. This also reduces the size of the file exports as we only check for max file size after exporting all of the finished spans. Signed-off-by: Steven Woods <[email protected]>
1 parent 4870104 commit 75af768

File tree

5 files changed

+62
-11
lines changed

5 files changed

+62
-11
lines changed

ocaml/libs/tracing/tracing.ml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ module Span = struct
351351
let span_id = Span_id.make () in
352352
let extra_context_with_depth =
353353
TraceContext.(
354-
with_added_baggage depth_key (string_of_int depth) extra_context
354+
update_with_baggage depth_key (string_of_int depth) extra_context
355355
)
356356
in
357357
let context : SpanContext.t =
@@ -363,7 +363,7 @@ module Span = struct
363363
match trace_context with
364364
| Some tc ->
365365
let tc_with_depth =
366-
TraceContext.(with_added_baggage depth_key (string_of_int depth) tc)
366+
TraceContext.(update_with_baggage depth_key (string_of_int depth) tc)
367367
in
368368
SpanContext.with_trace_context tc_with_depth context
369369
| None ->
@@ -751,7 +751,7 @@ module Tracer = struct
751751
let tc = SpanContext.context_of_span_context context in
752752
let new_depth = TraceContext.baggage_depth_of tc in
753753
let new_tc =
754-
TraceContext.(with_added_baggage depth_key (string_of_int new_depth) tc)
754+
TraceContext.(update_with_baggage depth_key (string_of_int new_depth) tc)
755755
in
756756
let context = SpanContext.with_trace_context new_tc context in
757757
{
@@ -784,8 +784,17 @@ module Tracer = struct
784784
if not t.enabled then
785785
ok_none (* Do not start span if the max depth has been reached *)
786786
else if parent_depth >= Atomic.get Spans.max_depth then (
787-
let parent_trace_id = Option.fold ~none:"None" ~some:(fun p -> p.Span.context |> SpanContext.span_id_of_span_context |> Span_id.to_string) parent in
788-
debug "Max_span_depth limit reached, not creating span %s (parent %s)" name parent_trace_id ;
787+
let parent_trace_id =
788+
Option.fold ~none:"None"
789+
~some:(fun p ->
790+
p.Span.context
791+
|> SpanContext.span_id_of_span_context
792+
|> Span_id.to_string
793+
)
794+
parent
795+
in
796+
debug "Max_span_depth limit reached, not creating span %s (parent %s)"
797+
name parent_trace_id ;
789798
ok_none
790799
) else
791800
let attributes = Attributes.merge_into t.attributes attributes in
@@ -811,7 +820,7 @@ module Tracer = struct
811820
let new_context : SpanContext.t =
812821
let trace_context =
813822
TraceContext.(
814-
with_added_baggage depth_key (string_of_int new_depth)
823+
update_with_baggage depth_key (string_of_int new_depth)
815824
span.Span.context.trace_context
816825
)
817826
in
@@ -993,7 +1002,7 @@ module Propagator = struct
9931002
in
9941003
let trace_context'' =
9951004
TraceContext.(
996-
with_added_baggage depth_key new_depth trace_context'
1005+
update_with_baggage depth_key new_depth trace_context'
9971006
)
9981007
in
9991008
let carrier' = P.inject_into trace_context'' carrier in

ocaml/libs/tracing/tracing_export.ml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ let export_interval = ref 30.
2424

2525
let set_export_interval t = export_interval := t
2626

27+
let export_chunk_size = Atomic.make 10000
28+
29+
let set_export_chunk_size x = Atomic.set export_chunk_size x
30+
2731
let host_id = ref "localhost"
2832

2933
let set_host_id id = host_id := id
@@ -289,17 +293,40 @@ module Destination = struct
289293
with exn ->
290294
debug "Tracing: unable to export span : %s" (Printexc.to_string exn)
291295

296+
let rec span_info_chunks span_info batch_size =
297+
let rec list_to_chunks_inner l n curr chunks =
298+
if n = 0 then
299+
if List.length l > 0 then
300+
list_to_chunks_inner l batch_size [] ((curr, batch_size) :: chunks)
301+
else
302+
(curr, batch_size) :: chunks
303+
else
304+
match l with
305+
| [] ->
306+
(curr, List.length curr) :: chunks
307+
| h :: t ->
308+
list_to_chunks_inner t (n - 1) (h :: curr) chunks
309+
in
310+
list_to_chunks_inner (fst span_info) batch_size [] []
311+
292312
let flush_spans () =
293313
let ((_span_list, span_count) as span_info) = Spans.since () in
294314
let attributes = [("export.traces.count", string_of_int span_count)] in
295315
let@ parent =
296316
with_tracing ~span_kind:Server ~trace_context:TraceContext.empty
297317
~parent:None ~attributes ~name:"Tracing.flush_spans"
298318
in
299-
TracerProvider.get_tracer_providers ()
300-
|> List.filter TracerProvider.get_enabled
301-
|> List.concat_map TracerProvider.get_endpoints
302-
|> List.iter (export_to_endpoint parent span_info)
319+
let endpoints =
320+
TracerProvider.get_tracer_providers ()
321+
|> List.filter TracerProvider.get_enabled
322+
|> List.concat_map TracerProvider.get_endpoints
323+
in
324+
let span_info_chunks =
325+
span_info_chunks span_info (Atomic.get export_chunk_size)
326+
in
327+
List.iter
328+
(fun s_i -> List.iter (export_to_endpoint parent s_i) endpoints)
329+
span_info_chunks
303330

304331
let delay = Delay.make ()
305332

ocaml/libs/tracing/tracing_export.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ val set_export_interval : float -> unit
2323
Default is every [30.] seconds.
2424
*)
2525

26+
val set_export_chunk_size : int -> unit
27+
(** [set_export_chunk_size size] sets the maximum number of finished spans that
28+
can be exported in one chunk to [size].
29+
30+
Default is 10000 spans.
31+
*)
32+
2633
val set_host_id : string -> unit
2734
(** [set_host_id id] sets the id of the host to [id].
2835

ocaml/xapi/xapi_globs.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,8 @@ let trace_log_dir = ref "/var/log/dt/zipkinv2/json"
10551055

10561056
let export_interval = ref 30.
10571057

1058+
let export_chunk_size = ref 10000
1059+
10581060
let max_spans = ref 10000
10591061

10601062
let max_traces = ref 10000
@@ -1678,6 +1680,11 @@ let other_options =
16781680
, (fun () -> string_of_float !export_interval)
16791681
, "The interval for exports in Tracing"
16801682
)
1683+
; ( "export-chunk-size"
1684+
, Arg.Set_int export_chunk_size
1685+
, (fun () -> string_of_int !export_chunk_size)
1686+
, "The span chunk size for exports in Tracing"
1687+
)
16811688
; ( "max-spans"
16821689
, Arg.Set_int max_spans
16831690
, (fun () -> string_of_int !max_spans)

ocaml/xapi/xapi_observer.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ let initialise_observer ~__context component =
600600

601601
let initialise ~__context =
602602
Tracing.Spans.set_max_depth !Xapi_globs.max_span_depth ;
603+
Tracing_export.set_export_chunk_size !Xapi_globs.export_chunk_size ;
603604
List.iter (initialise_observer_meta ~__context) (startup_components ()) ;
604605
Db.Observer.get_all ~__context
605606
|> List.iter (fun self ->

0 commit comments

Comments
 (0)