Skip to content

Commit

Permalink
Add rate limit for trace rate log
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock committed Feb 27, 2024
1 parent 7df424a commit d381d47
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tracer/src/Datadog.Trace/Sampling/RateLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public bool Allowed(Span span)

public abstract void OnDisallowed(Span span, int count, int intervalMs, int maxTracesPerInterval);

public virtual void OnRefresh(int intervalMs, int checksInLastInterval, int allowedInLastInterval)
{
}

public abstract void OnFinally(Span span);

public float GetEffectiveRate()
Expand Down Expand Up @@ -134,6 +138,7 @@ private void WaitForRefresh()
_windowAllowed = 0;
_windowChecks = 0;
_windowBegin = now;
OnRefresh(_intervalMilliseconds, _previousWindowChecks, _previousWindowAllowed);
}

while (_intervalQueue.TryPeek(out var time) && now.Subtract(time) > _interval)
Expand Down
18 changes: 17 additions & 1 deletion tracer/src/Datadog.Trace/Sampling/TracerRateLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

using System.Threading;
using Datadog.Trace.Logging;

namespace Datadog.Trace.Sampling
{
internal class TracerRateLimiter : RateLimiter
{
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor<TracerRateLimiter>();
private bool _warningWritten;

public TracerRateLimiter(int? maxTracesPerInterval)
: base(maxTracesPerInterval)
Expand All @@ -18,7 +20,21 @@ public TracerRateLimiter(int? maxTracesPerInterval)

public override void OnDisallowed(Span span, int count, int intervalMs, int maxTracesPerInterval)
{
Log.Warning<string, int, int>("Dropping trace id {TraceId} with count of {Count} for last {Interval}ms.", span.Context.RawTraceId, count, intervalMs);
if (!Volatile.Read(ref _warningWritten))
{
Log.Warning<string, int, int>("Dropping trace id {TraceId} with count of {Count} for last {Interval}ms.", span.Context.RawTraceId, count, intervalMs);
_warningWritten = true;
}
}

public override void OnRefresh(int intervalMs, int checksInLastInterval, int allowedInLastInterval)
{
if (Volatile.Read(ref _warningWritten))
{
Log.Warning<int, int, int>("Trace rate limit interval reset: Allowed {Allowed} traces out of {Checked} in last {Interval}ms.", allowedInLastInterval, checksInLastInterval, intervalMs);
}

_warningWritten = false;
}

public override void OnFinally(Span span)
Expand Down

0 comments on commit d381d47

Please sign in to comment.