Skip to content

Commit a3a9f59

Browse files
authored
Add ability to set a metrics prefix (#313)
* Add ability to set a metrics prefix * Trim the names just to be sure the metrics are valid
1 parent cfa1c0f commit a3a9f59

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/Foundatio/Queues/QueueBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ protected QueueBase(TOptions options) : base(options?.TimeProvider, options?.Log
4141
{
4242
_options = options ?? throw new ArgumentNullException(nameof(options));
4343
_metricsPrefix = $"foundatio.{typeof(T).Name.ToLowerInvariant()}";
44+
if (!String.IsNullOrWhiteSpace(options.MetricsPrefix))
45+
_metricsPrefix = $"{_metricsPrefix}.{options.MetricsPrefix.Trim()}";
4446

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

4749
_serializer = options.Serializer ?? DefaultSerializer.Instance;
4850
options.Behaviors.ForEach(AttachBehavior);

src/Foundatio/Queues/SharedQueueOptions.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public class SharedQueueOptions<T> : SharedOptions where T : class
99
public int Retries { get; set; } = 2;
1010
public TimeSpan WorkItemTimeout { get; set; } = TimeSpan.FromMinutes(5);
1111
public ICollection<IQueueBehavior<T>> Behaviors { get; set; } = new List<IQueueBehavior<T>>();
12+
13+
/// <summary>
14+
/// Allows you to set a prefix on queue metrics. This allows you to have unique metrics for keyed queues (e.g., priority queues).
15+
/// </summary>
16+
public string MetricsPrefix { get; set; }
1217
}
1318

1419
public class SharedQueueOptionsBuilder<T, TOptions, TBuilder> : SharedOptionsBuilder<TOptions, TBuilder>
@@ -18,8 +23,9 @@ public class SharedQueueOptionsBuilder<T, TOptions, TBuilder> : SharedOptionsBui
1823
{
1924
public TBuilder Name(string name)
2025
{
21-
if (!String.IsNullOrEmpty(name))
22-
Target.Name = name;
26+
if (!String.IsNullOrWhiteSpace(name))
27+
Target.Name = name.Trim();
28+
2329
return (TBuilder)this;
2430
}
2531

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

6268
return (TBuilder)this;
6369
}
70+
71+
/// <summary>
72+
/// Allows you to set a prefix on queue metrics. This allows you to have unique metrics for keyed queues (e.g., priority queues).
73+
/// </summary>
74+
public TBuilder MetricsPrefix(string prefix)
75+
{
76+
if (!String.IsNullOrWhiteSpace(prefix))
77+
Target.MetricsPrefix = prefix.Trim();
78+
return (TBuilder)this;
79+
}
6480
}

0 commit comments

Comments
 (0)