Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to specify global tags as part of configuration #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions src/StatsdClient/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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));
Expand All @@ -27,7 +29,7 @@ public static void Counter<T>(string statName, T value, double sampleRate = 1.0,
{
return;
}
_statsD.Send<Statsd.Counting,T>(BuildNamespacedStatName(statName), value, sampleRate, tags);
_statsD.Send<Statsd.Counting,T>(BuildNamespacedStatName(statName), value, sampleRate, MergeTags(tags));
}

public static void Increment(string statName, double sampleRate = 1.0, string[] tags = null)
Expand All @@ -36,7 +38,7 @@ public static void Increment(string statName, double sampleRate = 1.0, string[]
{
return;
}
_statsD.Send<Statsd.Counting,int>(BuildNamespacedStatName(statName), 1, sampleRate, tags);
_statsD.Send<Statsd.Counting, int>(BuildNamespacedStatName(statName), 1, sampleRate, MergeTags(tags));
}

public static void Decrement(string statName, double sampleRate = 1.0, params string[] tags)
Expand All @@ -45,7 +47,7 @@ public static void Decrement(string statName, double sampleRate = 1.0, params st
{
return;
}
_statsD.Send<Statsd.Counting,int>(BuildNamespacedStatName(statName), -1, sampleRate, tags);
_statsD.Send<Statsd.Counting, int>(BuildNamespacedStatName(statName), -1, sampleRate, MergeTags(tags));
}

public static void Gauge<T>(string statName, T value, double sampleRate = 1.0, string[] tags = null)
Expand All @@ -54,7 +56,7 @@ public static void Gauge<T>(string statName, T value, double sampleRate = 1.0, s
{
return;
}
_statsD.Send<Statsd.Gauge,T>(BuildNamespacedStatName(statName), value, sampleRate, tags);
_statsD.Send<Statsd.Gauge, T>(BuildNamespacedStatName(statName), value, sampleRate, MergeTags(tags));
}

public static void Histogram<T>(string statName, T value, double sampleRate = 1.0, string[] tags = null)
Expand All @@ -63,7 +65,7 @@ public static void Histogram<T>(string statName, T value, double sampleRate = 1.
{
return;
}
_statsD.Send<Statsd.Histogram,T>(BuildNamespacedStatName(statName), value, sampleRate, tags);
_statsD.Send<Statsd.Histogram, T>(BuildNamespacedStatName(statName), value, sampleRate, MergeTags(tags));
}

public static void Set<T>(string statName, T value, double sampleRate = 1.0, string[] tags = null)
Expand All @@ -72,7 +74,7 @@ public static void Set<T>(string statName, T value, double sampleRate = 1.0, str
{
return;
}
_statsD.Send<Statsd.Set,T>(BuildNamespacedStatName(statName), value, sampleRate, tags);
_statsD.Send<Statsd.Set, T>(BuildNamespacedStatName(statName), value, sampleRate, MergeTags(tags));
}

public static void Timer<T>(string statName, T value, double sampleRate = 1.0, string[] tags = null)
Expand All @@ -82,13 +84,13 @@ public static void Timer<T>(string statName, T value, double sampleRate = 1.0, s
return;
}

_statsD.Send<Statsd.Timing,T>(BuildNamespacedStatName(statName), value, sampleRate, tags);
_statsD.Send<Statsd.Timing, T>(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)
Expand All @@ -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));
}
}

Expand All @@ -110,7 +112,7 @@ public static T Time<T>(Func<T> func, string statName, double sampleRate = 1.0,
return func();
}

using (StartTimer(statName, sampleRate, tags))
using (StartTimer(statName, sampleRate, MergeTags(tags)))
{
return func();
}
Expand All @@ -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;
}

}
}
}
1 change: 1 addition & 0 deletions src/StatsdClient/MetricsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 42 additions & 3 deletions src/Tests/MetricConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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" });
}

}
}