@@ -189,7 +189,8 @@ public static CoreTracerBuilder builder() {
189189 private final boolean disableSamplingMechanismValidation ;
190190 private final TimeSource timeSource ;
191191 private final ProfilingContextIntegration profilingContextIntegration ;
192- private boolean injectBaggageAsTags ;
192+ private final boolean injectBaggageAsTags ;
193+ private final boolean flushOnClose ;
193194
194195 /**
195196 * JVM shutdown callback, keeping a reference to it to remove this if DDTracer gets destroyed
@@ -280,6 +281,7 @@ public static class CoreTracerBuilder {
280281 private boolean pollForTracerFlareRequests ;
281282 private boolean pollForTracingConfiguration ;
282283 private boolean injectBaggageAsTags ;
284+ private boolean flushOnClose ;
283285
284286 public CoreTracerBuilder serviceName (String serviceName ) {
285287 this .serviceName = serviceName ;
@@ -413,6 +415,11 @@ public CoreTracerBuilder injectBaggageAsTags(boolean injectBaggageAsTags) {
413415 return this ;
414416 }
415417
418+ public CoreTracerBuilder flushOnClose (boolean flushOnClose ) {
419+ this .flushOnClose = flushOnClose ;
420+ return this ;
421+ }
422+
416423 public CoreTracerBuilder () {
417424 // Apply the default values from config.
418425 config (Config .get ());
@@ -443,6 +450,7 @@ public CoreTracerBuilder config(final Config config) {
443450 partialFlushMinSpans (config .getPartialFlushMinSpans ());
444451 strictTraceWrites (config .isTraceStrictWritesEnabled ());
445452 injectBaggageAsTags (config .isInjectBaggageAsTagsEnabled ());
453+ flushOnClose (config .isCiVisibilityEnabled ());
446454 return this ;
447455 }
448456
@@ -473,7 +481,8 @@ public CoreTracer build() {
473481 profilingContextIntegration ,
474482 pollForTracerFlareRequests ,
475483 pollForTracingConfiguration ,
476- injectBaggageAsTags );
484+ injectBaggageAsTags ,
485+ flushOnClose );
477486 }
478487 }
479488
@@ -504,7 +513,8 @@ private CoreTracer(
504513 final ProfilingContextIntegration profilingContextIntegration ,
505514 final boolean pollForTracerFlareRequests ,
506515 final boolean pollForTracingConfiguration ,
507- final boolean injectBaggageAsTags ) {
516+ final boolean injectBaggageAsTags ,
517+ final boolean flushOnClose ) {
508518
509519 assert localRootSpanTags != null ;
510520 assert defaultSpanTags != null ;
@@ -712,6 +722,7 @@ private CoreTracer(
712722 propagationTagsFactory = PropagationTags .factory (config );
713723 this .profilingContextIntegration = profilingContextIntegration ;
714724 this .injectBaggageAsTags = injectBaggageAsTags ;
725+ this .flushOnClose = flushOnClose ;
715726 this .allowInferredServices = SpanNaming .instance ().namingSchema ().allowInferredServices ();
716727 if (profilingContextIntegration != ProfilingContextIntegration .NoOp .INSTANCE ) {
717728 Map <String , Object > tmp = new HashMap <>(localRootSpanTags );
@@ -1087,6 +1098,9 @@ public CallbackProvider getUniversalCallbackProvider() {
10871098
10881099 @ Override
10891100 public void close () {
1101+ if (flushOnClose ) {
1102+ flush ();
1103+ }
10901104 tracingConfigPoller .stop ();
10911105 pendingTraceBuffer .close ();
10921106 writer .close ();
0 commit comments