Skip to content

Commit

Permalink
Ability to disable metrics polling
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Dec 18, 2024
1 parent 6c77408 commit a8d50d3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Foundatio.TestHarness/Queue/QueueTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ public virtual async Task CanRunWorkItemWithMetricsAsync()
{
int completedCount = 0;

using var queue = new InMemoryQueue<WorkItemData>(o => o.LoggerFactory(Log).MetricsInterval(TimeSpan.Zero));
using var queue = new InMemoryQueue<WorkItemData>(o => o.LoggerFactory(Log).MetricsPollingInterval(TimeSpan.Zero));

Task Handler(object sender, CompletedEventArgs<WorkItemData> e)
{
Expand Down
19 changes: 14 additions & 5 deletions src/Foundatio/Queues/QueueBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ protected QueueBase(TOptions options) : base(options?.TimeProvider, options?.Log
_totalTimeHistogram = FoundatioDiagnostics.Meter.CreateHistogram<double>(GetFullMetricName("totaltime"), description: "Total time in queue", unit: "ms");
_abandonedCounter = FoundatioDiagnostics.Meter.CreateCounter<long>(GetFullMetricName("abandoned"), description: "Number of abandoned items");

if (!options.MetricsPollingEnabled)
return;

var queueMetricValues = new InstrumentsValues<long, long, long>(() =>
{
if (options.MetricsInterval > TimeSpan.Zero && _nextQueueStatsUpdate >= _timeProvider.GetUtcNow())
if (options.MetricsPollingInterval > TimeSpan.Zero && _nextQueueStatsUpdate >= _timeProvider.GetUtcNow())
return (_queueStats.Queued, _queueStats.Working, _queueStats.Deadletter);

_nextQueueStatsUpdate = _timeProvider.GetUtcNow().UtcDateTime.Add(_options.MetricsInterval);
_nextQueueStatsUpdate = _timeProvider.GetUtcNow().UtcDateTime.Add(_options.MetricsPollingInterval);
try
{
using var _ = FoundatioDiagnostics.ActivitySource.StartActivity("Queue Stats: " + _options.Name);
Expand All @@ -80,9 +83,15 @@ protected QueueBase(TOptions options) : base(options?.TimeProvider, options?.Log
}
}, _logger);

_countGauge = FoundatioDiagnostics.Meter.CreateObservableGauge(GetFullMetricName("count"), () => new Measurement<long>(queueMetricValues.GetValue1()), description: "Number of items in the queue");
_workingGauge = FoundatioDiagnostics.Meter.CreateObservableGauge(GetFullMetricName("working"), () => new Measurement<long>(queueMetricValues.GetValue2()), description: "Number of items currently being processed");
_deadletterGauge = FoundatioDiagnostics.Meter.CreateObservableGauge(GetFullMetricName("deadletter"), () => new Measurement<long>(queueMetricValues.GetValue3()), description: "Number of items in the deadletter queue");
_countGauge = FoundatioDiagnostics.Meter.CreateObservableGauge(GetFullMetricName("count"),
() => new Measurement<long>(queueMetricValues.GetValue1()),
description: "Number of items in the queue");
_workingGauge = FoundatioDiagnostics.Meter.CreateObservableGauge(GetFullMetricName("working"),
() => new Measurement<long>(queueMetricValues.GetValue2()),
description: "Number of items currently being processed");
_deadletterGauge = FoundatioDiagnostics.Meter.CreateObservableGauge(GetFullMetricName("deadletter"),
() => new Measurement<long>(queueMetricValues.GetValue3()),
description: "Number of items in the deadletter queue");
}

public string QueueId { get; protected set; }
Expand Down
33 changes: 28 additions & 5 deletions src/Foundatio/Queues/SharedQueueOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ public class SharedQueueOptions<T> : SharedOptions where T : class
public string MetricsPrefix { get; set; }

/// <summary>
/// How often to update queue metrics. Defaults to 30 seconds.
/// How often to poll queue metrics. These metrics are more expensive to calculate. Defaults to 5 seconds.
/// </summary>
public TimeSpan MetricsInterval { get; set; } = TimeSpan.FromSeconds(30);
public TimeSpan MetricsPollingInterval { get; set; } = TimeSpan.FromSeconds(5);

/// <summary>
/// If metrics that require polling are enabled. These metrics are more expensive to calculate and should be disabled if you are not using them. Defaults to true.
/// </summary>
public bool MetricsPollingEnabled { get; set; } = true;
}

public class SharedQueueOptionsBuilder<T, TOptions, TBuilder> : SharedOptionsBuilder<TOptions, TBuilder>
Expand Down Expand Up @@ -84,14 +89,32 @@ public TBuilder MetricsPrefix(string prefix)
}

/// <summary>
/// How often to update queue metrics. Defaults to 30 seconds.
/// How often to poll queue metrics. These metrics are more expensive to calculate. Defaults to 5 seconds.
/// </summary>
public TBuilder MetricsInterval(TimeSpan interval)
public TBuilder MetricsPollingInterval(TimeSpan interval)
{
if (interval < TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(interval));

Target.MetricsInterval = interval;
Target.MetricsPollingInterval = interval;
return (TBuilder)this;
}

/// <summary>
/// If metrics that require polling are enabled. These metrics are more expensive to calculate and should be disabled if you are not using them. Defaults to true.
/// </summary>
public TBuilder MetricsPollingEnabled(bool enabled)
{
Target.MetricsPollingEnabled = enabled;
return (TBuilder)this;
}

/// <summary>
/// Disable metrics collection for this queue.
/// </summary>
public TBuilder DisableMetricsPolling()
{
Target.MetricsPollingEnabled = false;
return (TBuilder)this;
}
}
2 changes: 1 addition & 1 deletion tests/Foundatio.Tests/Queue/InMemoryQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected override IQueue<SimpleWorkItem> GetQueue(int retries = 1, TimeSpan? wo
.RetryMultipliers(retryMultipliers ?? new[] { 1, 3, 5, 10 })
.WorkItemTimeout(workItemTimeout.GetValueOrDefault(TimeSpan.FromMinutes(5)))
.TimeProvider(timeProvider)
.MetricsInterval(TimeSpan.Zero)
.MetricsPollingInterval(TimeSpan.Zero)
.LoggerFactory(Log));
if (_logger.IsEnabled(LogLevel.Debug))
_logger.LogDebug("Queue Id: {QueueId}", _queue.QueueId);
Expand Down

0 comments on commit a8d50d3

Please sign in to comment.