Updated to support Calendargrams

This commit is contained in:
Luke Venediger
2014-09-02 21:10:59 +02:00
parent 174de459fc
commit 95dbf3e68d
8 changed files with 112 additions and 43 deletions

View File

@@ -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

View File

@@ -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:

View File

@@ -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";
}
}

View File

@@ -1,39 +1,53 @@
using System;
namespace StatsdClient
{
/// <summary>
/// Interface for the statsd.net client
/// </summary>
public interface IStatsd
{
/// <summary>
/// Log a count for a metric
/// Interface for the statsd.net client
/// </summary>
void LogCount(string name, int count = 1);
/// <summary>
/// Log a gauge value
/// </summary>
void LogGauge(string name, int value);
/// <summary>
/// Log a latency / Timing
/// </summary>
void LogTiming(string name, int milliseconds);
/// <summary>
/// Log a latency / Timing
/// </summary>
void LogTiming(string name, long milliseconds);
/// <summary>
/// Log the number of unique occurrances of something
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
void LogSet(string name, int value);
/// <summary>
/// Log a raw metric that will not get aggregated on the server.
/// </summary>
/// <param name="name">The metric name.</param>
/// <param name="value">The metric value.</param>
/// <param name="epoch">(optional) The epoch timestamp. Leave this blank to have the server assign an epoch for you.</param>
void LogRaw(string name, int value, long? epoch = null);
}
public interface IStatsd
{
/// <summary>
/// Log a count for a metric
/// </summary>
void LogCount(string name, int count = 1);
/// <summary>
/// Log a gauge value
/// </summary>
void LogGauge(string name, int value);
/// <summary>
/// Log a latency / Timing
/// </summary>
void LogTiming(string name, int milliseconds);
/// <summary>
/// Log a latency / Timing
/// </summary>
void LogTiming(string name, long milliseconds);
/// <summary>
/// Log the number of unique occurrances of something
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
void LogSet(string name, int value);
/// <summary>
/// Log a calendargram metric
/// </summary>
/// <param name="name">The metric namespace</param>
/// <param name="value">The unique value to be counted in the time period</param>
/// <param name="period">The time period, can be one of h,d,dow,w,m</param>
void LogCalendargram(string name, string value, string period);
/// <summary>
/// Log a calendargram metric
/// </summary>
/// <param name="name">The metric namespace</param>
/// <param name="value">The unique value to be counted in the time period</param>
/// <param name="period">The time period, can be one of h,d,dow,w,m</param>
void LogCalendargram(string name, int value, string period);
/// <summary>
/// Log a raw metric that will not get aggregated on the server.
/// </summary>
/// <param name="name">The metric name.</param>
/// <param name="value">The metric value.</param>
/// <param name="epoch">(optional) The epoch timestamp. Leave this blank to have the server assign an epoch for you.</param>
void LogRaw(string name, int value, long? epoch = null);
}
}

View File

@@ -29,6 +29,10 @@ namespace StatsdClient
/// <summary>
/// A raw metric that won't be aggregated on the server.
/// </summary>
public const string RAW = "r";
public const string RAW = "r";
/// <summary>
/// A metric that calculates unique hits per hour, day, day-of-week, week or month
/// </summary>
public const string CALENDARGRAM = "cg";
}
}

View File

@@ -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")]

View File

@@ -9,7 +9,6 @@ namespace StatsdClient
/// <summary>
/// The statsd client library.
/// </summary>
[DebuggerDisplay("{host}:{port}")]
public class Statsd : IStatsd
{
private string _prefix;
@@ -155,6 +154,28 @@ namespace StatsdClient
SendMetric(MetricType.SET, name, _prefix, value);
}
/// <summary>
/// Log a calendargram metric
/// </summary>
/// <param name="name">The metric namespace</param>
/// <param name="value">The unique value to be counted in the time period</param>
/// <param name="period">The time period, can be one of h,d,dow,w,m</param>
public void LogCalendargram(string name, string value, string period)
{
SendMetric(MetricType.CALENDARGRAM, name, _prefix, value, period);
}
/// <summary>
/// Log a calendargram metric
/// </summary>
/// <param name="name">The metric namespace</param>
/// <param name="value">The unique value to be counted in the time period</param>
/// <param name="period">The time period, can be one of h,d,dow,w,m</param>
public void LogCalendargram(string name, int value, string period)
{
SendMetric(MetricType.CALENDARGRAM, name, _prefix, value, period);
}
/// <summary>
/// Log a raw metric that will not get aggregated on the server.
/// </summary>
@@ -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
/// <param name="value"></param>
/// <param name="postFix">A value to append to the end of the line.</param>
/// <returns>The formatted metric</returns>
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

View File

@@ -40,6 +40,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CalendargramRetentionPeriod.cs" />
<Compile Include="ConnectionType.cs" />
<Compile Include="IOutputChannel.cs" />
<Compile Include="IStatsd.cs" />