11package datadog .trace .common .writer ;
22
33import static datadog .trace .api .sampling .PrioritySampling .UNSET ;
4+ import static java .util .concurrent .TimeUnit .MINUTES ;
45
56import datadog .trace .core .DDSpan ;
67import datadog .trace .core .monitor .HealthMetrics ;
8+ import datadog .trace .relocate .api .RatelimitedLogger ;
79import java .util .Collection ;
810import java .util .List ;
911import java .util .concurrent .TimeUnit ;
@@ -28,6 +30,8 @@ public abstract class RemoteWriter implements Writer {
2830
2931 private static final Logger log = LoggerFactory .getLogger (RemoteWriter .class );
3032
33+ private final RatelimitedLogger rlLog = new RatelimitedLogger (log , 1 , MINUTES );
34+
3135 protected final TraceProcessingWorker traceProcessingWorker ;
3236 private final PayloadDispatcher dispatcher ;
3337 private final boolean alwaysFlush ;
@@ -63,10 +67,14 @@ protected RemoteWriter(
6367
6468 @ Override
6569 public void write (final List <DDSpan > trace ) {
66- // We can't add events after shutdown otherwise it will never complete shutting down.
67- if (!closed ) {
70+ if (closed ) {
71+ // We can't add events after shutdown otherwise it will never complete shutting down.
72+ log .debug ("Dropped due to shutdown: {}" , trace );
73+ handleDroppedTrace (trace );
74+ } else {
6875 if (trace .isEmpty ()) {
69- handleDroppedTrace ("Trace was empty" , trace , UNSET );
76+ log .debug ("Dropped an empty trace." );
77+ handleDroppedTrace (trace );
7078 } else {
7179 final DDSpan root = trace .get (0 );
7280 final int samplingPriority = root .samplingPriority ();
@@ -79,26 +87,28 @@ public void write(final List<DDSpan> trace) {
7987 log .debug ("Enqueued for single span sampling: {}" , trace );
8088 break ;
8189 case DROPPED_BY_POLICY :
82- handleDroppedTrace ("Dropping policy is active" , trace , samplingPriority );
90+ log .debug ("Dropped by the policy: {}" , trace );
91+ handleDroppedTrace (trace );
8392 break ;
8493 case DROPPED_BUFFER_OVERFLOW :
85- handleDroppedTrace ("Trace written to overfilled buffer" , trace , samplingPriority );
94+ if (log .isDebugEnabled ()) {
95+ log .debug ("Dropped due to a buffer overflow: {}" , trace );
96+ } else {
97+ rlLog .warn ("Dropped due to a buffer overflow: [{} spans]" , trace .size ());
98+ }
99+ handleDroppedTrace (trace );
86100 break ;
87101 }
88102 }
89- } else {
90- handleDroppedTrace ("Trace written after shutdown." , trace , UNSET );
91103 }
92104 if (alwaysFlush ) {
93105 flush ();
94106 }
95107 }
96108
97- private void handleDroppedTrace (
98- final String reason , final List <DDSpan > trace , final int samplingPriority ) {
99- log .debug ("{}. Counted but dropping trace: {}" , reason , trace );
100- healthMetrics .onFailedPublish (
101- trace .isEmpty () ? 0 : trace .get (0 ).samplingPriority (), trace .size ());
109+ private void handleDroppedTrace (final List <DDSpan > trace ) {
110+ int samplingPriority = trace .isEmpty () ? UNSET : trace .get (0 ).samplingPriority ();
111+ healthMetrics .onFailedPublish (samplingPriority , trace .size ());
102112 incrementDropCounts (trace .size ());
103113 }
104114
0 commit comments