4

I'm looking to publish custom performance counters for certain important function calls in the system. I'd like to continously monitor these performance counters in a production environment.

Is there a way for me to mark certain functions with a custom attribute that could measure the time it took to execute given function? I'd like to avoid injecting custom code and thus polluting business-related functions with monitoring code.

How can a code from an attribute track the time it took for function to execute?

Please do not suggest use of a Profiler. I'm not looking to debug or benchmark performance. But simply wanting to track it in on a production 24/7 basis.

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
Igorek
  • 15,716
  • 3
  • 54
  • 92
  • Is it for a web application (ASP.NET) or Desktop (WinForms, WPF)? – keenthinker May 04 '14 at 15:44
  • 1
    or Windows services... this is to monitor performance of certain framework-level code that can be used by various consumers – Igorek May 04 '14 at 15:46
  • 2
    Since you will change (you need to) your code to add the attribute, would it be alright for you if you put your *code* into an *Action* or *Func* and thus wrap the execution into a *measurement function*? This is how i did it for a project i was working for. It is not so elegant as using attributes though, but it doesn't need any (complex) dependencies. – keenthinker May 04 '14 at 16:01
  • 1
    was hoping to make it seamless. But yes, your way would be an alternative I suppose – Igorek May 04 '14 at 16:05
  • @Igorek Many DI containers allows you to do it in a really seamless manner. See the update in my answer. – Yair Nevet May 05 '14 at 07:25

1 Answers1

6

Well, you need some code to execute your function and analyze your attribute so it will know that it needs to execute something (e.g. execution time measurement, in your case) before and after your function - it could be using the Decorator design pattern, Reflection, Proxy object and so on.

As you tagged, it is an aspect-programming issue and you can take advantage of PostSharp third-party, for example, in order to achieve it.

Take a look at this specific page about Performance Counters using PostSharp, it is indeed uses Attributes: http://www.postsharp.net/aspects/examples/performance

To elaborate a little on @pasty's comment, you can intercept your methods invocations with Interception using Unity DI Container, it is indeed requires more code to write, here is a simple sample flow chart of the Unity interception feature:

shows the objects that are involved with interception behaviors in this example

Note to step 3, where a proxy object is actually returned to the client and thanks to that, the interception behavior is achieved.

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
  • can I use the free version of Postsharp for this? – Igorek May 04 '14 at 15:30
  • From PostSharp: _Diagnostics and Threading Pattern Libraries, plus advanced features not found in PostSharp Express_ but you can try it for 45 days no-charge. – Yair Nevet May 04 '14 at 15:32
  • 1
    As an alternative to PostSharp you could have a look at [Microsoft's Patterns & Practices Unity](http://msdn.microsoft.com/en-us/library/dn170416.aspx) - it is possible to intercept the execution of a method using an attribute, but you need to implement it by yourself. – keenthinker May 04 '14 at 16:09
  • I think the PostSharp link doesn't exists anymore. However, there's a blog post addressing this topic: http://www.postsharp.net/blog/post/Microsoft-Application-Insights-adding-custom-metrics-without-modifying-your-source-code – Gustin Nov 02 '16 at 09:13