Skip to content

Instantly share code, notes, and snippets.

@ThomasArdal
Created December 4, 2013 10:20
Show Gist options
  • Save ThomasArdal/7785345 to your computer and use it in GitHub Desktop.
Save ThomasArdal/7785345 to your computer and use it in GitHub Desktop.
using System.Web;
using System.Web.Mvc;
using StatsdClient;
using Stopwatch = System.Diagnostics.Stopwatch;
namespace MvcApplication1.Controllers
{
public class StatsdActionFilter : ActionFilterAttribute
{
private const string StopwatchKey = "stopwatch";
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
HttpContext.Current.Items.Add(StopwatchKey, stopwatch);
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var stopwatch = HttpContext.Current.Items[StopwatchKey] as Stopwatch;
if (stopwatch == null) return;
stopwatch.Stop();
Metrics.Timer(
filterContext.RouteData.Values["controller"] + "." + filterContext.RouteData.Values["action"],
(int) stopwatch.ElapsedMilliseconds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment