Created
December 4, 2013 10:20
-
-
Save ThomasArdal/7785345 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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