Created
September 20, 2012 11:54
-
-
Save antonydenyer/3755475 to your computer and use it in GitHub Desktop.
StatsD ServiceStack request timing Feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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