From aec04b1841706fd2187c62ead0bd36cae3ba1c57 Mon Sep 17 00:00:00 2001 From: chriskinsman Date: Fri, 25 Oct 2013 09:41:13 -0700 Subject: [PATCH] Added ability to specify global tags as part of configuration --- src/StatsdClient/Metrics.cs | 47 +++++++++++++++++++++------ src/StatsdClient/MetricsConfig.cs | 1 + src/Tests/MetricConfigurationTests.cs | 45 +++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 13 deletions(-) diff --git a/src/StatsdClient/Metrics.cs b/src/StatsdClient/Metrics.cs index 260f8b7..aea9ad6 100644 --- a/src/StatsdClient/Metrics.cs +++ b/src/StatsdClient/Metrics.cs @@ -6,6 +6,7 @@ public static class Metrics { private static Statsd _statsD; private static string _prefix; + private static string[] _tags; public static void Configure(MetricsConfig config) { @@ -16,6 +17,7 @@ public static void Configure(MetricsConfig config) throw new ArgumentNullException("config.StatsdServername"); _prefix = config.Prefix; + _tags = config.Tags; _statsD = string.IsNullOrEmpty(config.StatsdServerName) ? null : new Statsd(new StatsdUDP(config.StatsdServerName, config.StatsdPort, config.StatsdMaxUDPPacketSize)); @@ -27,7 +29,7 @@ public static void Counter(string statName, T value, double sampleRate = 1.0, { return; } - _statsD.Send(BuildNamespacedStatName(statName), value, sampleRate, tags); + _statsD.Send(BuildNamespacedStatName(statName), value, sampleRate, MergeTags(tags)); } public static void Increment(string statName, double sampleRate = 1.0, string[] tags = null) @@ -36,7 +38,7 @@ public static void Increment(string statName, double sampleRate = 1.0, string[] { return; } - _statsD.Send(BuildNamespacedStatName(statName), 1, sampleRate, tags); + _statsD.Send(BuildNamespacedStatName(statName), 1, sampleRate, MergeTags(tags)); } public static void Decrement(string statName, double sampleRate = 1.0, params string[] tags) @@ -45,7 +47,7 @@ public static void Decrement(string statName, double sampleRate = 1.0, params st { return; } - _statsD.Send(BuildNamespacedStatName(statName), -1, sampleRate, tags); + _statsD.Send(BuildNamespacedStatName(statName), -1, sampleRate, MergeTags(tags)); } public static void Gauge(string statName, T value, double sampleRate = 1.0, string[] tags = null) @@ -54,7 +56,7 @@ public static void Gauge(string statName, T value, double sampleRate = 1.0, s { return; } - _statsD.Send(BuildNamespacedStatName(statName), value, sampleRate, tags); + _statsD.Send(BuildNamespacedStatName(statName), value, sampleRate, MergeTags(tags)); } public static void Histogram(string statName, T value, double sampleRate = 1.0, string[] tags = null) @@ -63,7 +65,7 @@ public static void Histogram(string statName, T value, double sampleRate = 1. { return; } - _statsD.Send(BuildNamespacedStatName(statName), value, sampleRate, tags); + _statsD.Send(BuildNamespacedStatName(statName), value, sampleRate, MergeTags(tags)); } public static void Set(string statName, T value, double sampleRate = 1.0, string[] tags = null) @@ -72,7 +74,7 @@ public static void Set(string statName, T value, double sampleRate = 1.0, str { return; } - _statsD.Send(BuildNamespacedStatName(statName), value, sampleRate, tags); + _statsD.Send(BuildNamespacedStatName(statName), value, sampleRate, MergeTags(tags)); } public static void Timer(string statName, T value, double sampleRate = 1.0, string[] tags = null) @@ -82,13 +84,13 @@ public static void Timer(string statName, T value, double sampleRate = 1.0, s return; } - _statsD.Send(BuildNamespacedStatName(statName), value, sampleRate, tags); + _statsD.Send(BuildNamespacedStatName(statName), value, sampleRate, MergeTags(tags)); } public static IDisposable StartTimer(string name, double sampleRate = 1.0, string[] tags = null) { - return new MetricsTimer(name, sampleRate, tags); + return new MetricsTimer(name, sampleRate, MergeTags(tags)); } public static void Time(Action action, string statName, double sampleRate = 1.0, string[] tags = null) @@ -99,7 +101,7 @@ public static void Time(Action action, string statName, double sampleRate = 1.0, } else { - _statsD.Send(action, BuildNamespacedStatName(statName), sampleRate, tags); + _statsD.Send(action, BuildNamespacedStatName(statName), sampleRate, MergeTags(tags)); } } @@ -110,7 +112,7 @@ public static T Time(Func func, string statName, double sampleRate = 1.0, return func(); } - using (StartTimer(statName, sampleRate, tags)) + using (StartTimer(statName, sampleRate, MergeTags(tags))) { return func(); } @@ -125,5 +127,30 @@ private static string BuildNamespacedStatName(string statName) return _prefix + "." + statName; } + + private static string[] MergeTags(string[] tags) + { + if (tags == null && _tags == null) + { + return null; + } + else if (tags == null && _tags != null) + { + return _tags; + } + else if (tags != null && _tags == null) + { + return tags; + } + else + { + string[] mergedTags = new string[tags.Length + _tags.Length]; + _tags.CopyTo(mergedTags, 0); + tags.CopyTo(mergedTags, _tags.Length); + + return mergedTags; + } + + } } } diff --git a/src/StatsdClient/MetricsConfig.cs b/src/StatsdClient/MetricsConfig.cs index 3504d3d..c11fc80 100644 --- a/src/StatsdClient/MetricsConfig.cs +++ b/src/StatsdClient/MetricsConfig.cs @@ -6,6 +6,7 @@ public class MetricsConfig public int StatsdPort { get; set; } public int StatsdMaxUDPPacketSize { get; set; } public string Prefix { get; set; } + public string[] Tags { get; set; } public const int DefaultStatsdPort = 8125; public const int DefaultStatsdMaxUDPPacketSize = 512; diff --git a/src/Tests/MetricConfigurationTests.cs b/src/Tests/MetricConfigurationTests.cs index a6ea755..baeb961 100644 --- a/src/Tests/MetricConfigurationTests.cs +++ b/src/Tests/MetricConfigurationTests.cs @@ -11,12 +11,12 @@ namespace Tests public class MetricConfigurationTest { private void testReceive(string testServerName, int testPort, string testCounterName, - string expectedOutput) + string expectedOutput, string[] tags = null) { UdpListener udpListener = new UdpListener(testServerName, testPort); - Thread listenThread = new Thread(new ThreadStart(udpListener.Listen)); + Thread listenThread = new Thread(new ParameterizedThreadStart(udpListener.Listen)); listenThread.Start(); - Metrics.Increment(testCounterName); + Metrics.Increment(testCounterName, tags:tags); while(listenThread.IsAlive); Assert.AreEqual(expectedOutput, udpListener.GetAndClearLastMessages()[0]); udpListener.Dispose(); @@ -70,5 +70,44 @@ public void setting_prefix() StatsdClient.Metrics.Configure(metricsConfig); testReceive("127.0.0.1", 8125, "test", "prefix.test:1|c"); } + + [Test] + public void setting_globaltag() + { + var metricsConfig = new MetricsConfig + { + StatsdServerName = "127.0.0.1", + Prefix = "prefix", + Tags = new[] { "tag1" } + }; + StatsdClient.Metrics.Configure(metricsConfig); + testReceive("127.0.0.1", 8125, "test", "prefix.test:1|c|#tag1"); + } + + [Test] + public void setting_localtagonly() + { + var metricsConfig = new MetricsConfig + { + StatsdServerName = "127.0.0.1", + Prefix = "prefix" + }; + StatsdClient.Metrics.Configure(metricsConfig); + testReceive("127.0.0.1", 8125, "test", "prefix.test:1|c|#tag1", new []{ "tag1"}); + } + + [Test] + public void setting_local_andglobal_tag() + { + var metricsConfig = new MetricsConfig + { + StatsdServerName = "127.0.0.1", + Prefix = "prefix", + Tags = new[] { "tag1" } + }; + StatsdClient.Metrics.Configure(metricsConfig); + testReceive("127.0.0.1", 8125, "test", "prefix.test:1|c|#tag1,tag2", new []{ "tag2" }); + } + } }