From 95dbf3e68db83612cbcbf2a57aceb424e4d9e0b9 Mon Sep 17 00:00:00 2001 From: Luke Venediger Date: Tue, 2 Sep 2014 21:10:59 +0200 Subject: [PATCH] Updated to support Calendargrams --- CHANGELOG.md | 3 + README.md | 4 +- StatsdClient/CalendargramRetentionPeriod.cs | 18 +++++ StatsdClient/IStatsd.cs | 80 ++++++++++++--------- StatsdClient/MetricType.cs | 6 +- StatsdClient/Properties/AssemblyInfo.cs | 4 +- StatsdClient/Statsd.cs | 39 ++++++++-- StatsdClient/StatsdClient.csproj | 1 + 8 files changed, 112 insertions(+), 43 deletions(-) create mode 100644 StatsdClient/CalendargramRetentionPeriod.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index e29f0dd..6f50e64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # statsd-csharp-client Changelog +## v1.3.0.0 +* Added support for Calendargrams + ## v1.2.1.0 * Fixed a bug in the tcp output channel's retry logic * Skip DNS resolution on the UDP client if already an IP Address diff --git a/README.md b/README.md index a7c66fe..e609503 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ A simple c# client library for [statsd.net](https://github.com/lukevenediger/statsd.net/) and [statsd](https://github.com/etsy/statsd/). # Features -* Log counts, timings, gauges, sets and raw metrics +* Log counts, timings, gauges, sets, calendargrams and raw metrics * Has an additional API that uses dynamics to create and submit stats * Fault-tolerant client that can be configured to fail silently (with a warning) if misconfigured * IStatsdClient interface for easy mocking in unit tests @@ -36,6 +36,8 @@ statsd.LogGauge( "site.activeUsers", numActiveUsers ); statsd.LogTiming( "site.pageLoad", 100 /* milliseconds */ ); // Log a raw metric statsd.LogRaw ("already.aggregated", 982, 1885837485 /* epoch timestamp */ ); +// Log a calendargram +statsd.LogCalendargram("order.completed", "user_13143", CalendargramRetentionPeriod.HOUR); ``` You can also wrap your code in a `using` block to measure the latency by using the LogTiming(string) extension method: diff --git a/StatsdClient/CalendargramRetentionPeriod.cs b/StatsdClient/CalendargramRetentionPeriod.cs new file mode 100644 index 0000000..d596335 --- /dev/null +++ b/StatsdClient/CalendargramRetentionPeriod.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace StatsdClient +{ + public sealed class CalendargramRetentionPeriod + { + public const string ONE_MINUTE = "1min"; + public const string FIVE_MINUTE = "5min"; + public const string HOUR = "h"; + public const string DAY = "d"; + public const string WEEK = "w"; + public const string DAY_OF_WEEK = "dow"; + public const string MONTH = "m"; + } +} diff --git a/StatsdClient/IStatsd.cs b/StatsdClient/IStatsd.cs index 25a7cd1..54c1629 100644 --- a/StatsdClient/IStatsd.cs +++ b/StatsdClient/IStatsd.cs @@ -1,39 +1,53 @@ using System; namespace StatsdClient { - /// - /// Interface for the statsd.net client - /// - public interface IStatsd - { /// - /// Log a count for a metric + /// Interface for the statsd.net client /// - void LogCount(string name, int count = 1); - /// - /// Log a gauge value - /// - void LogGauge(string name, int value); - /// - /// Log a latency / Timing - /// - void LogTiming(string name, int milliseconds); - /// - /// Log a latency / Timing - /// - void LogTiming(string name, long milliseconds); - /// - /// Log the number of unique occurrances of something - /// - /// - /// - void LogSet(string name, int value); - /// - /// Log a raw metric that will not get aggregated on the server. - /// - /// The metric name. - /// The metric value. - /// (optional) The epoch timestamp. Leave this blank to have the server assign an epoch for you. - void LogRaw(string name, int value, long? epoch = null); - } + public interface IStatsd + { + /// + /// Log a count for a metric + /// + void LogCount(string name, int count = 1); + /// + /// Log a gauge value + /// + void LogGauge(string name, int value); + /// + /// Log a latency / Timing + /// + void LogTiming(string name, int milliseconds); + /// + /// Log a latency / Timing + /// + void LogTiming(string name, long milliseconds); + /// + /// Log the number of unique occurrances of something + /// + /// + /// + void LogSet(string name, int value); + /// + /// Log a calendargram metric + /// + /// The metric namespace + /// The unique value to be counted in the time period + /// The time period, can be one of h,d,dow,w,m + void LogCalendargram(string name, string value, string period); + /// + /// Log a calendargram metric + /// + /// The metric namespace + /// The unique value to be counted in the time period + /// The time period, can be one of h,d,dow,w,m + void LogCalendargram(string name, int value, string period); + /// + /// Log a raw metric that will not get aggregated on the server. + /// + /// The metric name. + /// The metric value. + /// (optional) The epoch timestamp. Leave this blank to have the server assign an epoch for you. + void LogRaw(string name, int value, long? epoch = null); + } } diff --git a/StatsdClient/MetricType.cs b/StatsdClient/MetricType.cs index 811a086..704939a 100644 --- a/StatsdClient/MetricType.cs +++ b/StatsdClient/MetricType.cs @@ -29,6 +29,10 @@ namespace StatsdClient /// /// A raw metric that won't be aggregated on the server. /// - public const string RAW = "r"; + public const string RAW = "r"; + /// + /// A metric that calculates unique hits per hour, day, day-of-week, week or month + /// + public const string CALENDARGRAM = "cg"; } } diff --git a/StatsdClient/Properties/AssemblyInfo.cs b/StatsdClient/Properties/AssemblyInfo.cs index 82348d9..954170b 100644 --- a/StatsdClient/Properties/AssemblyInfo.cs +++ b/StatsdClient/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.1.0")] -[assembly: AssemblyFileVersion("1.2.1.0")] +[assembly: AssemblyVersion("1.3.0.0")] +[assembly: AssemblyFileVersion("1.3.0.0")] diff --git a/StatsdClient/Statsd.cs b/StatsdClient/Statsd.cs index 0271329..959084c 100644 --- a/StatsdClient/Statsd.cs +++ b/StatsdClient/Statsd.cs @@ -9,7 +9,6 @@ namespace StatsdClient /// /// The statsd client library. /// - [DebuggerDisplay("{host}:{port}")] public class Statsd : IStatsd { private string _prefix; @@ -155,6 +154,28 @@ namespace StatsdClient SendMetric(MetricType.SET, name, _prefix, value); } + /// + /// Log a calendargram metric + /// + /// The metric namespace + /// The unique value to be counted in the time period + /// The time period, can be one of h,d,dow,w,m + public void LogCalendargram(string name, string value, string period) + { + SendMetric(MetricType.CALENDARGRAM, name, _prefix, value, period); + } + + /// + /// Log a calendargram metric + /// + /// The metric namespace + /// The unique value to be counted in the time period + /// The time period, can be one of h,d,dow,w,m + public void LogCalendargram(string name, int value, string period) + { + SendMetric(MetricType.CALENDARGRAM, name, _prefix, value, period); + } + /// /// Log a raw metric that will not get aggregated on the server. /// @@ -167,15 +188,21 @@ namespace StatsdClient } private void SendMetric(string metricType, string name, string prefix, int value, string postFix = null) + { + if (value < 0) + { + Trace.TraceWarning(String.Format("Metric value for {0} was less than zero: {1}. Not sending.", name, value)); + return; + } + SendMetric(metricType, name, prefix, value.ToString(), postFix); + } + + private void SendMetric(string metricType, string name, string prefix, string value, string postFix = null) { if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } - if (value < 0) - { - throw new ArgumentOutOfRangeException("value", value, "Cannot be less than zero."); - } _outputChannel.Send(PrepareMetric(metricType, name, prefix, value, postFix)); } @@ -188,7 +215,7 @@ namespace StatsdClient /// /// A value to append to the end of the line. /// The formatted metric - protected virtual string PrepareMetric(string metricType, string name, string prefix, int value, string postFix = null) + protected virtual string PrepareMetric(string metricType, string name, string prefix, string value, string postFix = null) { return (String.IsNullOrEmpty(prefix) ? name : (prefix + "." + name)) + ":" + value diff --git a/StatsdClient/StatsdClient.csproj b/StatsdClient/StatsdClient.csproj index cf88cfc..3814309 100644 --- a/StatsdClient/StatsdClient.csproj +++ b/StatsdClient/StatsdClient.csproj @@ -40,6 +40,7 @@ +