From a3a9f5947ea8c2b3c78d3227bde0510699f1c198 Mon Sep 17 00:00:00 2001 From: Blake Niemyjski Date: Fri, 27 Sep 2024 11:51:54 -0500 Subject: [PATCH] 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 --- src/Foundatio/Queues/QueueBase.cs | 4 +++- src/Foundatio/Queues/SharedQueueOptions.cs | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Foundatio/Queues/QueueBase.cs b/src/Foundatio/Queues/QueueBase.cs index 07f53e8f..185057ae 100644 --- a/src/Foundatio/Queues/QueueBase.cs +++ b/src/Foundatio/Queues/QueueBase.cs @@ -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); diff --git a/src/Foundatio/Queues/SharedQueueOptions.cs b/src/Foundatio/Queues/SharedQueueOptions.cs index 944e7589..cf1caa5b 100644 --- a/src/Foundatio/Queues/SharedQueueOptions.cs +++ b/src/Foundatio/Queues/SharedQueueOptions.cs @@ -9,6 +9,11 @@ public class SharedQueueOptions : SharedOptions where T : class public int Retries { get; set; } = 2; public TimeSpan WorkItemTimeout { get; set; } = TimeSpan.FromMinutes(5); public ICollection> Behaviors { get; set; } = new List>(); + + /// + /// Allows you to set a prefix on queue metrics. This allows you to have unique metrics for keyed queues (e.g., priority queues). + /// + public string MetricsPrefix { get; set; } } public class SharedQueueOptionsBuilder : SharedOptionsBuilder @@ -18,8 +23,9 @@ public class SharedQueueOptionsBuilder : SharedOptionsBui { public TBuilder Name(string name) { - if (!String.IsNullOrEmpty(name)) - Target.Name = name; + if (!String.IsNullOrWhiteSpace(name)) + Target.Name = name.Trim(); + return (TBuilder)this; } @@ -61,4 +67,14 @@ public TBuilder AddBehavior(IQueueBehavior behavior) return (TBuilder)this; } + + /// + /// Allows you to set a prefix on queue metrics. This allows you to have unique metrics for keyed queues (e.g., priority queues). + /// + public TBuilder MetricsPrefix(string prefix) + { + if (!String.IsNullOrWhiteSpace(prefix)) + Target.MetricsPrefix = prefix.Trim(); + return (TBuilder)this; + } }