Skip to content

Commit

Permalink
Add ability to set a metrics prefix (#313)
Browse files Browse the repository at this point in the history
* Add ability to set a metrics prefix

* Trim the names just to be sure the metrics are valid
  • Loading branch information
niemyjski authored Sep 27, 2024
1 parent cfa1c0f commit a3a9f59
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Foundatio/Queues/QueueBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ protected QueueBase(TOptions options) : base(options?.TimeProvider, options?.Log
{
_options = options ?? throw new ArgumentNullException(nameof(options));
_metricsPrefix = $"foundatio.{typeof(T).Name.ToLowerInvariant()}";
if (!String.IsNullOrWhiteSpace(options.MetricsPrefix))
_metricsPrefix = $"{_metricsPrefix}.{options.MetricsPrefix.Trim()}";

QueueId = options.Name + Guid.NewGuid().ToString("N").Substring(10);
QueueId = $"{options.Name.Trim()}{Guid.NewGuid().ToString("N").Substring(10)}";

_serializer = options.Serializer ?? DefaultSerializer.Instance;
options.Behaviors.ForEach(AttachBehavior);
Expand Down
20 changes: 18 additions & 2 deletions src/Foundatio/Queues/SharedQueueOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public class SharedQueueOptions<T> : SharedOptions where T : class
public int Retries { get; set; } = 2;
public TimeSpan WorkItemTimeout { get; set; } = TimeSpan.FromMinutes(5);
public ICollection<IQueueBehavior<T>> Behaviors { get; set; } = new List<IQueueBehavior<T>>();

/// <summary>
/// Allows you to set a prefix on queue metrics. This allows you to have unique metrics for keyed queues (e.g., priority queues).
/// </summary>
public string MetricsPrefix { get; set; }
}

public class SharedQueueOptionsBuilder<T, TOptions, TBuilder> : SharedOptionsBuilder<TOptions, TBuilder>
Expand All @@ -18,8 +23,9 @@ public class SharedQueueOptionsBuilder<T, TOptions, TBuilder> : SharedOptionsBui
{
public TBuilder Name(string name)
{
if (!String.IsNullOrEmpty(name))
Target.Name = name;
if (!String.IsNullOrWhiteSpace(name))
Target.Name = name.Trim();

return (TBuilder)this;
}

Expand Down Expand Up @@ -61,4 +67,14 @@ public TBuilder AddBehavior(IQueueBehavior<T> behavior)

return (TBuilder)this;
}

/// <summary>
/// Allows you to set a prefix on queue metrics. This allows you to have unique metrics for keyed queues (e.g., priority queues).
/// </summary>
public TBuilder MetricsPrefix(string prefix)
{
if (!String.IsNullOrWhiteSpace(prefix))
Target.MetricsPrefix = prefix.Trim();
return (TBuilder)this;
}
}

0 comments on commit a3a9f59

Please sign in to comment.