Skip to content

Commit

Permalink
feat: inc and dec gauge metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagodaraujo authored and joelfoliveira committed May 16, 2024
1 parent 7f017cf commit ac607d1
Show file tree
Hide file tree
Showing 20 changed files with 280 additions and 129 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ sample:

sample-clean:
@docker compose -f docker-compose.sample.yaml down

benchmark:
dotnet run -c Release --project ./tests/benchmark/Farfetch.LoadShedding.BenchmarkTests/Farfetch.LoadShedding.BenchmarkTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading.Tasks;
using Farfetch.LoadShedding.Tasks;
using Microsoft.AspNetCore.Http;
Expand All @@ -9,8 +8,6 @@ internal class HttpHeaderPriorityResolver : IPriorityResolver
{
internal const string DefaultPriorityHeaderName = "X-Priority";

private const string Separator = "-";

private readonly string _headerName;

public HttpHeaderPriorityResolver()
Expand All @@ -30,21 +27,7 @@ public Task<Priority> ResolveAsync(HttpContext context)
return Task.FromResult(Priority.Normal);
}

var normalizedValue = this.NormalizeHeaderValue(values);

if (!Enum.TryParse(normalizedValue, true, out Priority priority))
{
priority = Priority.Normal;
}

return Task.FromResult(priority);
}

private string NormalizeHeaderValue(string headerValue)
{
return headerValue
.Replace(Separator, string.Empty)
.ToLower();
return Task.FromResult(values.ToString().ParsePriority());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ private static void SubscribeItemDequeuedEvent(
events.ItemDequeued.Subscribe(args =>
{
var method = accessor.GetMethod();
var priority = args.Priority.FormatPriority();
queueItemsGauge.Set(method, args.Priority.FormatPriority(), args.QueueCount);
queueTimeHistogram.Observe(method, args.Priority.FormatPriority(), args.QueueTime.TotalSeconds);
queueItemsGauge.Decrement(method, priority);
queueTimeHistogram.Observe(method, priority, args.QueueTime.TotalSeconds);
});
}

Expand All @@ -88,7 +89,7 @@ private static void SubscribeItemProcessedEvent(
var method = accessor.GetMethod();
var priority = args.Priority.FormatPriority();
concurrencyItemsGauge.Set(method, priority, args.ConcurrencyCount);
concurrencyItemsGauge.Decrement(method, priority);
taskProcessingTimeHistogram.Observe(method, priority, args.ProcessingTime.TotalSeconds);
});
}
Expand Down Expand Up @@ -123,10 +124,10 @@ private static void SubscribeItemProcessingEvent(

events.ItemProcessing.Subscribe(args =>
{
concurrencyItemsGauge.Set(
accessor.GetMethod(),
args.Priority.FormatPriority(),
args.ConcurrencyCount);
var method = accessor.GetMethod();
var priority = args.Priority.FormatPriority();
concurrencyItemsGauge.Increment(method, priority);
});
}

Expand All @@ -139,10 +140,10 @@ private static void SubscribeItemEnqueuedEvent(this Events.ILoadSheddingEvents e

events.ItemEnqueued.Subscribe(args =>
{
queueItemsGauge.Set(
accessor.GetMethod(),
args.Priority.FormatPriority(),
args.QueueCount);
var method = accessor.GetMethod();
var priority = args.Priority.FormatPriority();
queueItemsGauge.Increment(method, priority);
});
}

Expand All @@ -155,8 +156,10 @@ private static void SubscribeRejectedEvent(this Events.ILoadSheddingEvents event

events.Rejected.Subscribe(args =>
{
var method = accessor.GetMethod();
rejectedCounter.Increment(
accessor.GetMethod(),
method,
args.Priority.FormatPriority(),
args.Reason);
});
Expand All @@ -171,10 +174,5 @@ private static string GetMethod(this IHttpContextAccessor accessor)

return accessor.HttpContext.Request.Method.ToUpper();
}

private static string FormatPriority(this Priority priority)
{
return priority.ToString().ToLower();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Prometheus;

using PrometheusBase = Prometheus;

namespace Farfetch.LoadShedding.Prometheus.Metrics
Expand All @@ -22,16 +21,27 @@ internal HttpRequestsConcurrencyItemsGauge(
protected override string DefaultName => "http_requests_concurrency_items_total";

/// <summary>
/// Sets the value of the gauge.
/// Increments the value of the gauge.
/// </summary>
/// <param name="method">The method.</param>
/// <param name="priority">The priority.</param>
public void Increment(string method, string priority)
{
this.Metric?
.WithLabels(method, priority)
.Inc();
}

/// <summary>
/// Decrements the value of the gauge.
/// </summary>
/// <param name="method">The method.</param>
/// <param name="priority">The priority.</param>
/// <param name="value">The value.</param>
public void Set(string method, string priority, double value)
public void Decrement(string method, string priority)
{
this.Metric?
.WithLabels(method, priority)
.Set(value);
.Dec();
}

/// <inheritdoc/>
Expand All @@ -40,7 +50,7 @@ protected override Gauge Create(CollectorRegistry registry, MetricOptions option
return PrometheusBase
.Metrics
.WithCustomRegistry(registry)
.CreateGauge(options.Name, Description, new PrometheusBase.GaugeConfiguration
.CreateGauge(options.Name, Description, new GaugeConfiguration
{
LabelNames = new[] { MetricsConstants.MethodLabel, MetricsConstants.PriorityLabel },
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Prometheus;

using PrometheusBase = Prometheus;

namespace Farfetch.LoadShedding.Prometheus.Metrics
Expand All @@ -22,16 +21,27 @@ internal HttpRequestsQueueItemsGauge(
protected override string DefaultName => "http_requests_queue_items_total";

/// <summary>
/// Sets the value of the gauge.
/// Increments the value of the gauge.
/// </summary>
/// <param name="method">The method.</param>
/// <param name="priority">The priority.</param>
public void Increment(string method, string priority)
{
this.Metric?
.WithLabels(method, priority)
.Inc();
}

/// <summary>
/// Decrements the value of the gauge.
/// </summary>
/// <param name="method">The method.</param>
/// <param name="priority">The priority.</param>
/// <param name="value">The value.</param>
public void Set(string method, string priority, double value)
public void Decrement(string method, string priority)
{
this.Metric?
.WithLabels(method, priority)
.Set(value);
.Dec();
}

/// <inheritdoc/>
Expand Down
5 changes: 1 addition & 4 deletions src/Farfetch.LoadShedding/Tasks/ConcurrentCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ public int Decrement()
{
lock (this._locker)
{
if (this._count > 0)
{
this._count--;
}
this._count--;

return this._count;
}
Expand Down
45 changes: 45 additions & 0 deletions src/Farfetch.LoadShedding/Tasks/PriorityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;

namespace Farfetch.LoadShedding.Tasks
{
/// <summary>
/// Extension methods of <see cref="Priority"/>
/// </summary>
public static class PriorityExtensions
{
/// <summary>
/// Parses a string value into a <see cref="Priority"/> enum.
/// The parser is case-insensetive and accepts hyphen. e.g. Normal, Non-Critival or CRITICAL.
/// </summary>
/// <param name="value">Priority string value to be parsed.</param>
/// <returns>The Priority parsed value. Returns Priority.Normal if the value is invalid.</returns>
public static Priority ParsePriority(this string value)
{
if (value is null)
{
return Priority.Normal;
}

var normalizedValue = value
.Replace("-", string.Empty)
.ToLowerInvariant();

if (Enum.TryParse(normalizedValue, true, out Priority priority))
{
return priority;
}

return Priority.Normal;
}

/// <summary>
/// Format a <see cref="Priority"/> enum to a string.
/// </summary>
/// <param name="priority">Priority value to be formatted.</param>
/// <returns>The Priority format string.</returns>
public static string FormatPriority(this Priority priority)
{
return priority.ToString().ToLowerInvariant();
}
}
}
38 changes: 16 additions & 22 deletions src/Farfetch.LoadShedding/Tasks/TaskQueue.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

Expand All @@ -11,12 +9,7 @@ internal class TaskQueue
{
private readonly ConcurrentCounter _counter = new();

private readonly IDictionary<Priority, TaskItemList> _queues = new SortedDictionary<Priority, TaskItemList>()
{
[Priority.Critical] = new TaskItemList(),
[Priority.Normal] = new TaskItemList(),
[Priority.NonCritical] = new TaskItemList(),
};
private readonly TaskItemList[] _queues = new TaskItemList[3] { new(), new(), new() };

public TaskQueue(int limit)
{
Expand Down Expand Up @@ -44,9 +37,8 @@ public void Enqueue(TaskItem item)
public TaskItem Dequeue()
{
var nextQueueItem = this._queues
.FirstOrDefault(x => x.Value.HasItems)
.Value?
.Dequeue();
.FirstOrDefault(x => x.HasItems)
?.Dequeue();

if (nextQueueItem != null)
{
Expand All @@ -58,7 +50,7 @@ public TaskItem Dequeue()

public void Remove(TaskItem item)
{
if (this._queues[item.Priority].Remove(item))
if (this._queues[(int)item.Priority].Remove(item))
{
this.DecrementCounter();
}
Expand All @@ -68,25 +60,22 @@ internal void Clear()
{
foreach (var queue in this._queues)
{
queue.Value.Clear();
queue.Clear();
}
}

private int EnqueueItem(TaskItem item)
{
this._queues[item.Priority].Add(item);

var count = this._counter.Increment();
this._queues[(int)item.Priority].Add(item);

return count;
return this.IncrementCounter();
}

private void RejectLastItem()
{
var lastItem = this._queues
.LastOrDefault(x => x.Value.HasItems)
.Value?
.DequeueLast();
.LastOrDefault(x => x.HasItems)
?.DequeueLast();

if (lastItem == null)
{
Expand All @@ -98,9 +87,14 @@ private void RejectLastItem()
lastItem.Reject();
}

private void DecrementCounter()
private int IncrementCounter()
{
return this._counter.Increment();
}

private int DecrementCounter()
{
this._counter.Decrement();
return this._counter.Decrement();
}
}
}
3 changes: 1 addition & 2 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../src'))" />

<ItemGroup>
<Compile Include="$(MSBuildProjectDirectory)/../Traits.cs" Link="Properties/Traits.cs"/>
<AdditionalFiles Include="..\..\..\stylecop.json" Link="stylecop.json" />
<AdditionalFiles Include="..\..\..\stylecop.json" Link="stylecop.json" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@
<ProjectReference Include="..\..\..\src\Farfetch.LoadShedding\Farfetch.LoadShedding.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="$(MSBuildProjectDirectory)/../Traits.cs" />
</ItemGroup>

</Project>
Loading

0 comments on commit ac607d1

Please sign in to comment.