Skip to content

Commit fe85fe3

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 aeeba55 commit fe85fe3

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

ocaml/libs/tracing/tracing.ml

Lines changed: 5 additions & 5 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
{
@@ -811,7 +811,7 @@ module Tracer = struct
811811
let new_context : SpanContext.t =
812812
let trace_context =
813813
TraceContext.(
814-
with_added_baggage depth_key (string_of_int new_depth)
814+
update_with_baggage depth_key (string_of_int new_depth)
815815
span.Span.context.trace_context
816816
)
817817
in
@@ -993,7 +993,7 @@ module Propagator = struct
993993
in
994994
let trace_context'' =
995995
TraceContext.(
996-
with_added_baggage depth_key new_depth trace_context'
996+
update_with_baggage depth_key new_depth trace_context'
997997
)
998998
in
999999
let carrier' = P.inject_into trace_context'' carrier in

ocaml/libs/tracing/tracing_export.ml

Lines changed: 26 additions & 2 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,37 @@ module Destination = struct
289293
with exn ->
290294
debug "Tracing: unable to export span : %s" (Printexc.to_string exn)
291295

296+
(*TODO move this into Spans.since itself? *)
297+
let rec span_info_chunks span_info batch_size =
298+
let rec list_to_chunks_inner l n curr chunks =
299+
if n = 0 then
300+
if List.length l > 0 then
301+
list_to_chunks_inner l batch_size [] ((curr, batch_size) :: chunks)
302+
else
303+
(curr, batch_size) :: chunks
304+
else
305+
match l with
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 ()
319+
let endpoints = TracerProvider.get_tracer_providers ()
300320
|> List.filter TracerProvider.get_enabled
301321
|> List.concat_map TracerProvider.get_endpoints
302-
|> List.iter (export_to_endpoint parent span_info)
322+
in
323+
let span_info_chunks = span_info_chunks span_info (Atomic.get export_chunk_size) in
324+
List.iter (fun s_i ->
325+
List.iter (export_to_endpoint parent s_i) endpoints
326+
) span_info_chunks
303327

304328
let delay = Delay.make ()
305329

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
@@ -1051,6 +1051,8 @@ let trace_log_dir = ref "/var/log/dt/zipkinv2/json"
10511051

10521052
let export_interval = ref 30.
10531053

1054+
let export_chunk_size = ref 10000
1055+
10541056
let max_spans = ref 10000
10551057

10561058
let max_traces = ref 10000
@@ -1673,6 +1675,11 @@ let other_options =
16731675
, (fun () -> string_of_float !export_interval)
16741676
, "The interval for exports in Tracing"
16751677
)
1678+
; ( "export-chunk-size"
1679+
, Arg.Set_int export_chunk_size
1680+
, (fun () -> string_of_int !export_chunk_size)
1681+
, "The span chunk size for exports in Tracing"
1682+
)
16761683
; ( "max-spans"
16771684
, Arg.Set_int max_spans
16781685
, (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)