Skip to content

Instantly share code, notes, and snippets.

@antonydenyer
Created September 20, 2012 11:54
Show Gist options
  • Save antonydenyer/3755475 to your computer and use it in GitHub Desktop.
Save antonydenyer/3755475 to your computer and use it in GitHub Desktop.
StatsD ServiceStack request timing Feature
public class StatsDFeature : IPlugin
{
private readonly Statsd _client;
private readonly string _statNamePrefix;
public StatsDFeature(IStatsd client, string statNamePrefix)
{
if (client == null)
throw new ArgumentNullException("client", "client instance must be specified");
if (statNamePrefix == null)
throw new ArgumentNullException("statNamePrefix", "client instance must be specified");
_client = (Statsd)client;
_statNamePrefix = statNamePrefix;
}
public void Register(IAppHost appHost)
{
appHost.RequestFilters.Add(OnBeginRequest);
appHost.ResponseFilters.Add(OnEndRequest);
}
private void OnBeginRequest(IHttpRequest request, IHttpResponse response, object model)
{
request.Items.Add("OnBeginRequest", DateTime.Now.Ticks);
}
private void OnEndRequest(IHttpRequest request, IHttpResponse response, object model)
{
var ticksNow = DateTime.Now.Ticks;
string ticksBefore = request.Items["OnBeginRequest"].ToString();
var ticks = ticksNow - long.Parse(ticksBefore);
var span = new TimeSpan(ticks);
_client.Send<Statsd.Timing>(_statNamePrefix + model.GetType().Name, span.Milliseconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment