From 5692d2ec42d695191bfa632dc5459042390e2474 Mon Sep 17 00:00:00 2001 From: Eric Fontana Date: Fri, 15 May 2015 10:41:09 -0400 Subject: [PATCH] Switched to use Nuget component for StatsD instead of including the code. --- .../TimberWinR.ServiceHost.csproj | 7 + TimberWinR.ServiceHost/packages.config | 1 + TimberWinR.TestGenerator/test7-tw.json | 1 + TimberWinR/NStatsD/Client.cs | 178 ------------------ .../NStatsD/StatsDConfigurationSection.cs | 51 ----- TimberWinR/Outputs/StatsD.cs | 26 ++- TimberWinR/TimberWinR.csproj | 8 +- TimberWinR/packages.config | 1 + TimberWix/Product.wxs | 2 + chocolateyUninstall.ps1.template | 2 +- 10 files changed, 35 insertions(+), 242 deletions(-) delete mode 100644 TimberWinR/NStatsD/Client.cs delete mode 100644 TimberWinR/NStatsD/StatsDConfigurationSection.cs diff --git a/TimberWinR.ServiceHost/TimberWinR.ServiceHost.csproj b/TimberWinR.ServiceHost/TimberWinR.ServiceHost.csproj index f38acbf..fdddbb4 100644 --- a/TimberWinR.ServiceHost/TimberWinR.ServiceHost.csproj +++ b/TimberWinR.ServiceHost/TimberWinR.ServiceHost.csproj @@ -42,6 +42,13 @@ False ..\packages\RapidRegex.Core.1.0.0.4\lib\net40\RapidRegex.Core.dll + + False + ..\packages\StatsdClient.1.0.0.19\lib\net35\StatsdClient.dll + + + ..\packages\StatsdClient.1.0.0.19\lib\net35\StatsdClient.Configuration.dll + diff --git a/TimberWinR.ServiceHost/packages.config b/TimberWinR.ServiceHost/packages.config index 5c5c672..11c659e 100644 --- a/TimberWinR.ServiceHost/packages.config +++ b/TimberWinR.ServiceHost/packages.config @@ -1,5 +1,6 @@  + \ No newline at end of file diff --git a/TimberWinR.TestGenerator/test7-tw.json b/TimberWinR.TestGenerator/test7-tw.json index f9f22e6..03df2d8 100644 --- a/TimberWinR.TestGenerator/test7-tw.json +++ b/TimberWinR.TestGenerator/test7-tw.json @@ -25,6 +25,7 @@ "StatsD": [ { "type": "Win32-TailLog", + "namespace": "timberwinrtest", "port": 8125, "host": "devlexicesnu003.mycompany.svc", "increment": ["apache.response.%{response}"], diff --git a/TimberWinR/NStatsD/Client.cs b/TimberWinR/NStatsD/Client.cs deleted file mode 100644 index 1843b96..0000000 --- a/TimberWinR/NStatsD/Client.cs +++ /dev/null @@ -1,178 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Linq; -using System.Net.Sockets; -using System.Threading; - -namespace NStatsD -{ - public sealed class Client - { - private static UdpClient _client; - - public static string Host { get; set; } - public static int Port { get; set; } - - Client() - { - if (Config != null) - { - var host = Config.Server.Host; - var port = Config.Server.Port; - _client = new UdpClient(host, port); - } - else - { - Config = new StatsDConfigurationSection(); - Config.Server.Host = Host;; - Config.Server.Port = Port; - _client = new UdpClient(Host, Port); - } - } - - public static Client Current - { - get { return CurrentClient.Instance; } - } - - class CurrentClient - { - static CurrentClient() { } - - internal static readonly Client Instance = new Client(); - } - - private StatsDConfigurationSection _config; - public StatsDConfigurationSection Config - { - get - { - if (_config == null) - { - _config = (StatsDConfigurationSection)ConfigurationManager.GetSection("statsD"); - } - - if(_config != null) - _config.Prefix = ValidatePrefix(_config.Prefix); - - return _config; - } - set - { - _config = value; - _config.Prefix = ValidatePrefix(_config.Prefix); - } - } - - private string ValidatePrefix(string prefix) - { - if (string.IsNullOrWhiteSpace(prefix)) - return prefix; - - if (prefix.EndsWith(".")) - return prefix; - - return string.Format("{0}.", prefix); - } - - /// - /// Sends timing statistics. - /// - /// Name of statistic being updated. - /// The timing it took to complete. - /// Tells StatsD how often to sample this value. Defaults to 1 (send all values). - /// A callback for when the send is complete. Defaults to null. - public void Timing(string stat, long time, double sampleRate = 1, AsyncCallback callback = null) - { - var data = new Dictionary { { stat, string.Format("{0}|ms", time) } }; - - Send(data, sampleRate, callback); - } - - /// - /// Increments a counter - /// - /// Name of statistic being updated. - /// Tells StatsD how often to sample this value. Defaults to 1 (send all values). - /// A callback for when the send is complete. Defaults to null. - public void Increment(string stat, double sampleRate = 1, AsyncCallback callback = null) - { - UpdateStats(stat, 1, sampleRate, callback); - } - - /// - /// Decrements a counter - /// - /// Name of statistic being updated. - /// Tells StatsD how often to sample this value. Defaults to 1 (send all values). - /// A callback for when the send is complete. Defaults to null. - public void Decrement(string stat, double sampleRate = 1, AsyncCallback callback = null) - { - UpdateStats(stat, -1, sampleRate, callback); - } - - /// - /// Updates a counter by an arbitrary amount - /// - /// Name of statistic being updated. - /// The value of the metric. - /// Tells StatsD how often to sample this value. Defaults to 1 (send all values). - /// A callback for when the send is complete. Defaults to null. - public void Gauge(string stat, int value, double sampleRate = 1, AsyncCallback callback = null) - { - var data = new Dictionary { { stat, string.Format("{0}|g", value) } }; - Send(data, sampleRate, callback); - } - - /// - /// Updates a counter by an arbitrary amount - /// - /// Name of statistic(s) being updated. - /// The amount to adjust the counter - /// Tells StatsD how often to sample this value. Defaults to 1 (send all values). - /// A callback for when the send is complete. Defaults to null. - public void UpdateStats(string stat, int delta = 1, double sampleRate = 1, AsyncCallback callback = null) - { - var dictionary = new Dictionary { { stat, string.Format("{0}|c", delta) } }; - Send(dictionary, sampleRate, callback); - } - - private static int _seed = Environment.TickCount; - private static readonly ThreadLocal random = new ThreadLocal(() => new Random(Interlocked.Increment(ref _seed))); - - private void Send(Dictionary data, double sampleRate, AsyncCallback callback) - { - if (!Config.Enabled) - return; - - if (sampleRate < 1) - { - var nextRand = random.Value.NextDouble(); - if (nextRand <= sampleRate) - { - var sampledData = data.Keys.ToDictionary(stat => stat, - stat => string.Format("{0}|@{1}", data[stat], sampleRate)); - SendToStatsD(sampledData, callback); - } - } - else - { - SendToStatsD(data, callback); - } - } - - private void SendToStatsD(Dictionary sampledData, AsyncCallback callback) - { - var prefix = Config.Prefix; - var encoding = new System.Text.ASCIIEncoding(); - - foreach (var stat in sampledData.Keys) - { - var stringToSend = string.Format("{0}{1}:{2}", prefix, stat, sampledData[stat]); - var sendData = encoding.GetBytes(stringToSend); - _client.BeginSend(sendData, sendData.Length, callback, null); - } - } - } -} diff --git a/TimberWinR/NStatsD/StatsDConfigurationSection.cs b/TimberWinR/NStatsD/StatsDConfigurationSection.cs deleted file mode 100644 index 4e3a479..0000000 --- a/TimberWinR/NStatsD/StatsDConfigurationSection.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Configuration; - -namespace NStatsD -{ - public class StatsDConfigurationSection : ConfigurationSection - { - [ConfigurationProperty("enabled", DefaultValue = "true", IsRequired = false)] - public bool Enabled - { - get { return (bool)this["enabled"]; } - set { this["enabled"] = value; } - } - - [ConfigurationProperty("server")] - public ServerElement Server - { - get { return (ServerElement)this["server"]; } - set { this["server"] = value; } - } - - [ConfigurationProperty("prefix", DefaultValue = "", IsRequired = false)] - public string Prefix - { - get { return (string)this["prefix"]; } - set { this["prefix"] = value; } - } - - public override bool IsReadOnly() - { - return false; - } - } - - - public class ServerElement : ConfigurationElement - { - [ConfigurationProperty("host", DefaultValue = "localhost", IsRequired = true)] - public string Host - { - get { return (string)this["host"]; } - set { this["host"] = value; } - } - - [ConfigurationProperty("port", DefaultValue = "8125", IsRequired = false)] - public int Port - { - get { return (int)this["port"]; } - set { this["port"] = value; } - } - } -} diff --git a/TimberWinR/Outputs/StatsD.cs b/TimberWinR/Outputs/StatsD.cs index 73b680a..f657e02 100644 --- a/TimberWinR/Outputs/StatsD.cs +++ b/TimberWinR/Outputs/StatsD.cs @@ -15,6 +15,7 @@ using System.Threading.Tasks; using RapidRegex.Core; using System.Text.RegularExpressions; using System.Globalization; +using StatsdClient; using TimberWinR.Parser; namespace TimberWinR.Outputs @@ -86,8 +87,13 @@ namespace TimberWinR.Outputs _numThreads = parameters.NumThreads; _jsonQueue = new List(); - NStatsD.Client.Host = _host; - NStatsD.Client.Port = _port; + var metricsConfig = new MetricsConfig + { + StatsdServerName = _host, + Prefix = parameters.Namespace, + }; + + StatsdClient.Metrics.Configure(metricsConfig); for (int i = 0; i < _numThreads; i++) { @@ -249,7 +255,7 @@ namespace TimberWinR.Outputs private string BuildMetricPath(string metric, JObject json) { - return string.Format("{0}.{1}.{2}", ExpandField(_params.Namespace, json), ExpandField(_params.Sender, json), ExpandField(metric, json)); + return string.Format("{0}.{1}", ExpandField(_params.Sender, json), ExpandField(metric, json)); } private void TransmitStats(List messages) @@ -305,7 +311,7 @@ namespace TimberWinR.Outputs int value; if (int.TryParse(gaugeName, out value)) { - NStatsD.Client.Current.Gauge(metricPath, value, _params.SampleRate); + Metrics.Gauge(metricPath, value); } } } @@ -317,10 +323,10 @@ namespace TimberWinR.Outputs { string metricPath = BuildMetricPath(_params.Timings[i], json); string timingName = ExpandField(_params.Timings[i + 1], json); - long value; - if (long.TryParse(timingName, out value)) + int value; + if (int.TryParse(timingName, out value)) { - NStatsD.Client.Current.Timing(metricPath, value, _params.SampleRate); + Metrics.Timer(metricPath, value, _params.SampleRate); } } } @@ -335,7 +341,7 @@ namespace TimberWinR.Outputs int value; if (int.TryParse(countName, out value)) { - NStatsD.Client.Current.UpdateStats(metricPath, value, _params.SampleRate); + Metrics.Counter(metricPath, value, _params.SampleRate); } } } @@ -345,7 +351,7 @@ namespace TimberWinR.Outputs foreach (var metric in _params.Increments) { string metricPath = BuildMetricPath(metric, json); - NStatsD.Client.Current.Increment(metricPath, _params.SampleRate); + Metrics.Counter(metricPath, 1,_params.SampleRate); } } @@ -355,7 +361,7 @@ namespace TimberWinR.Outputs foreach (var metric in _params.Increments) { string metricPath = BuildMetricPath(metric, json); - NStatsD.Client.Current.Decrement(metricPath, _params.SampleRate); + Metrics.Counter(metricPath, -1, _params.SampleRate); } } } diff --git a/TimberWinR/TimberWinR.csproj b/TimberWinR/TimberWinR.csproj index 31c0c63..cd132ce 100644 --- a/TimberWinR/TimberWinR.csproj +++ b/TimberWinR/TimberWinR.csproj @@ -67,6 +67,12 @@ ..\packages\RestSharp.105.0.0\lib\net4\RestSharp.dll + + ..\packages\StatsdClient.1.0.0.19\lib\net35\StatsdClient.dll + + + ..\packages\StatsdClient.1.0.0.19\lib\net35\StatsdClient.Configuration.dll + @@ -114,8 +120,6 @@ - - diff --git a/TimberWinR/packages.config b/TimberWinR/packages.config index 4e4a911..242109b 100644 --- a/TimberWinR/packages.config +++ b/TimberWinR/packages.config @@ -9,6 +9,7 @@ + \ No newline at end of file diff --git a/TimberWix/Product.wxs b/TimberWix/Product.wxs index 1f99f46..85d8984 100644 --- a/TimberWix/Product.wxs +++ b/TimberWix/Product.wxs @@ -54,6 +54,8 @@ + + diff --git a/chocolateyUninstall.ps1.template b/chocolateyUninstall.ps1.template index fb90328..186357c 100644 --- a/chocolateyUninstall.ps1.template +++ b/chocolateyUninstall.ps1.template @@ -2,6 +2,6 @@ $packageName = 'TimberWinR-${version}' # arbitrary name for the package, used in $installerType = 'msi' #only one of these: exe, msi, msu $scriptPath = $(Split-Path $MyInvocation.MyCommand.Path) $fileFullPath = Join-Path $scriptPath 'TimberWinR-${version}.0.msi' -$silentArgs = '{54D22382-5BDE-4CD7-90D4-91AEC0D9B447} /quiet' +$silentArgs = '{E001D138-669B-4604-88C5-02C756461C15} /quiet' $validExitCodes = @(0) #please insert other valid exit codes here, exit codes for ms http://msdn.microsoft.com/en-us/library/aa368542(VS.85).aspx UnInstall-ChocolateyPackage "$packageName" "$installerType" "$silentArgs" "fileFullPath" -validExitCodes $validExitCodes