33// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44// </copyright>
55
6+ #nullable enable
7+
68using System ;
79using System . Collections . Concurrent ;
810using System . Collections . Generic ;
1214using Datadog . Trace . Configuration ;
1315using Datadog . Trace . DogStatsd ;
1416using Datadog . Trace . Logging ;
15- using Datadog . Trace . Tagging ;
1617using Datadog . Trace . Telemetry ;
1718using Datadog . Trace . Telemetry . Metrics ;
18- using Datadog . Trace . Vendors . StatsdClient ;
1919
2020namespace Datadog . Trace . Agent
2121{
@@ -25,10 +25,10 @@ internal class AgentWriter : IAgentWriter
2525
2626 private static readonly IDatadogLogger Log = DatadogLogging . GetLoggerFor < AgentWriter > ( ) ;
2727
28- private static readonly ArraySegment < byte > EmptyPayload = new ( new byte [ ] { 0x90 } ) ;
28+ private static readonly ArraySegment < byte > EmptyPayload = new ( [ 0x90 ] ) ;
2929
3030 private readonly ConcurrentQueue < WorkItem > _pendingTraces = new ConcurrentQueue < WorkItem > ( ) ;
31- private readonly IDogStatsd _statsd ;
31+ private readonly IStatsdManager _statsd ;
3232 private readonly Task _flushTask ;
3333 private readonly Task _serializationTask ;
3434 private readonly TaskCompletionSource < bool > _processExit = new TaskCompletionSource < bool > ( ) ;
@@ -43,7 +43,7 @@ internal class AgentWriter : IAgentWriter
4343 private readonly int _batchInterval ;
4444 private readonly IKeepRateCalculator _traceKeepRateCalculator ;
4545
46- private readonly IStatsAggregator _statsAggregator ;
46+ private readonly IStatsAggregator ? _statsAggregator ;
4747
4848 private readonly bool _apmTracingEnabled ;
4949
@@ -67,7 +67,7 @@ internal class AgentWriter : IAgentWriter
6767
6868 private bool _traceMetricsEnabled ;
6969
70- public AgentWriter ( IApi api , IStatsAggregator statsAggregator , IDogStatsd statsd , TracerSettings settings )
70+ public AgentWriter ( IApi api , IStatsAggregator ? statsAggregator , IStatsdManager statsd , TracerSettings settings )
7171 : this ( api , statsAggregator , statsd , maxBufferSize : settings . TraceBufferSize , batchInterval : settings . TraceBatchInterval , apmTracingEnabled : settings . ApmTracingEnabled , initialTracerMetricsEnabled : settings . Manager . InitialMutableSettings . TracerMetricsEnabled )
7272 {
7373 settings . Manager . SubscribeToChanges ( changes =>
@@ -76,16 +76,17 @@ public AgentWriter(IApi api, IStatsAggregator statsAggregator, IDogStatsd statsd
7676 && mutable . TracerMetricsEnabled != changes . PreviousMutable . TracerMetricsEnabled )
7777 {
7878 Volatile . Write ( ref _traceMetricsEnabled , mutable . TracerMetricsEnabled ) ;
79+ _statsd . SetRequired ( StatsdConsumer . AgentWriter , mutable . TracerMetricsEnabled ) ;
7980 }
8081 } ) ;
8182 }
8283
83- public AgentWriter ( IApi api , IStatsAggregator statsAggregator , IDogStatsd statsd , bool automaticFlush = true , int maxBufferSize = 1024 * 1024 * 10 , int batchInterval = 100 , bool apmTracingEnabled = true , bool initialTracerMetricsEnabled = false )
84+ public AgentWriter ( IApi api , IStatsAggregator ? statsAggregator , IStatsdManager statsd , bool automaticFlush = true , int maxBufferSize = 1024 * 1024 * 10 , int batchInterval = 100 , bool apmTracingEnabled = true , bool initialTracerMetricsEnabled = false )
8485 : this ( api , statsAggregator , statsd , MovingAverageKeepRateCalculator . CreateDefaultKeepRateCalculator ( ) , automaticFlush , maxBufferSize , batchInterval , apmTracingEnabled , initialTracerMetricsEnabled )
8586 {
8687 }
8788
88- internal AgentWriter ( IApi api , IStatsAggregator statsAggregator , IDogStatsd statsd , IKeepRateCalculator traceKeepRateCalculator , bool automaticFlush , int maxBufferSize , int batchInterval , bool apmTracingEnabled , bool initialTracerMetricsEnabled )
89+ internal AgentWriter ( IApi api , IStatsAggregator ? statsAggregator , IStatsdManager statsd , IKeepRateCalculator traceKeepRateCalculator , bool automaticFlush , int maxBufferSize , int batchInterval , bool apmTracingEnabled , bool initialTracerMetricsEnabled )
8990 {
9091 _statsAggregator = statsAggregator ;
9192
@@ -104,6 +105,7 @@ internal AgentWriter(IApi api, IStatsAggregator statsAggregator, IDogStatsd stat
104105
105106 _apmTracingEnabled = apmTracingEnabled ;
106107 _traceMetricsEnabled = initialTracerMetricsEnabled ;
108+ _statsd . SetRequired ( StatsdConsumer . AgentWriter , initialTracerMetricsEnabled ) ;
107109
108110 _serializationTask = automaticFlush ? Task . Factory . StartNew ( SerializeTracesLoop , TaskCreationOptions . LongRunning ) : Task . CompletedTask ;
109111 _serializationTask . ContinueWith ( t => Log . Error ( t . Exception , "Error in serialization task" ) , TaskContinuationOptions . OnlyOnFaulted ) ;
@@ -114,7 +116,7 @@ internal AgentWriter(IApi api, IStatsAggregator statsAggregator, IDogStatsd stat
114116 _backBufferFlushTask = _frontBufferFlushTask = Task . CompletedTask ;
115117 }
116118
117- internal event Action Flushed ;
119+ internal event Action ? Flushed ;
118120
119121 internal SpanBuffer ActiveBuffer => _activeBuffer ;
120122
@@ -151,8 +153,12 @@ public void WriteTrace(ArraySegment<Span> trace)
151153
152154 if ( Volatile . Read ( ref _traceMetricsEnabled ) )
153155 {
154- _statsd . Increment ( TracerMetricNames . Queue . EnqueuedTraces ) ;
155- _statsd . Increment ( TracerMetricNames . Queue . EnqueuedSpans , trace . Count ) ;
156+ using var lease = _statsd . TryGetClientLease ( ) ;
157+ if ( lease . Client is { } statsd )
158+ {
159+ statsd . Increment ( TracerMetricNames . Queue . EnqueuedTraces ) ;
160+ statsd . Increment ( TracerMetricNames . Queue . EnqueuedSpans , trace . Count ) ;
161+ }
156162 }
157163 }
158164
@@ -255,7 +261,7 @@ private async Task FlushBuffersTaskLoopAsync()
255261 {
256262 tasks [ 2 ] = Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
257263 await Task . WhenAny ( tasks ) . ConfigureAwait ( false ) ;
258- tasks [ 2 ] = null ;
264+ tasks [ 2 ] = null ! ;
259265
260266 if ( _forceFlush . Task . IsCompleted )
261267 {
@@ -327,8 +333,12 @@ async Task InternalBufferFlush()
327333 {
328334 if ( Volatile . Read ( ref _traceMetricsEnabled ) )
329335 {
330- _statsd . Increment ( TracerMetricNames . Queue . DequeuedTraces , buffer . TraceCount ) ;
331- _statsd . Increment ( TracerMetricNames . Queue . DequeuedSpans , buffer . SpanCount ) ;
336+ using var lease = _statsd . TryGetClientLease ( ) ;
337+ if ( lease . Client is { } statsd )
338+ {
339+ statsd . Increment ( TracerMetricNames . Queue . DequeuedTraces , buffer . TraceCount ) ;
340+ statsd . Increment ( TracerMetricNames . Queue . DequeuedSpans , buffer . SpanCount ) ;
341+ }
332342 }
333343
334344 var droppedTraces = Interlocked . Exchange ( ref _droppedTraces , 0 ) ;
@@ -347,7 +357,7 @@ async Task InternalBufferFlush()
347357 {
348358 droppedP0Traces = Interlocked . Exchange ( ref _droppedP0Traces , 0 ) ;
349359 droppedP0Spans = Interlocked . Exchange ( ref _droppedP0Spans , 0 ) ;
350- Log . Debug < int , int , long , long > ( "Flushing {Spans} spans across {Traces} traces. CanComputeStats is enabled with {DroppedP0Traces} droppedP0Traces and {DroppedP0Spans} droppedP0Spans" , buffer . SpanCount , buffer . TraceCount , droppedP0Traces , droppedP0Spans ) ;
360+ Log . Debug ( "Flushing {Spans} spans across {Traces} traces. CanComputeStats is enabled with {DroppedP0Traces} droppedP0Traces and {DroppedP0Spans} droppedP0Spans" , buffer . SpanCount , buffer . TraceCount , droppedP0Traces , droppedP0Spans ) ;
351361 // Metrics for unsampled traces/spans already recorded
352362 }
353363 else
@@ -388,7 +398,7 @@ async Task InternalBufferFlush()
388398 private void SerializeTrace ( ArraySegment < Span > spans )
389399 {
390400 // Declaring as inline method because only safe to invoke in the context of SerializeTrace
391- SpanBuffer SwapBuffers ( )
401+ SpanBuffer ? SwapBuffers ( )
392402 {
393403 if ( _activeBuffer == _frontBuffer )
394404 {
@@ -525,8 +535,12 @@ private void DropTrace(ArraySegment<Span> spans)
525535
526536 if ( Volatile . Read ( ref _traceMetricsEnabled ) )
527537 {
528- _statsd . Increment ( TracerMetricNames . Queue . DroppedTraces ) ;
529- _statsd . Increment ( TracerMetricNames . Queue . DroppedSpans , spans . Count ) ;
538+ using var lease = _statsd . TryGetClientLease ( ) ;
539+ if ( lease . Client is { } statsd )
540+ {
541+ statsd . Increment ( TracerMetricNames . Queue . DroppedTraces ) ;
542+ statsd . Increment ( TracerMetricNames . Queue . DroppedSpans , spans . Count ) ;
543+ }
530544 }
531545 }
532546
@@ -589,7 +603,7 @@ private void SerializeTracesLoop()
589603 private readonly struct WorkItem
590604 {
591605 public readonly ArraySegment < Span > Trace ;
592- public readonly Action Callback ;
606+ public readonly Action ? Callback ;
593607
594608 public WorkItem ( ArraySegment < Span > trace )
595609 {
0 commit comments