Using .NET agent API for Custom Instrumentation


APM Insight .NET Agent API helps to track the user defined methods in a web application. It helps instrument specified methods in the web application DLLs for monitoring its performance. It can also be used to track specific parts of code.

Steps to add the API

1. Add a reference to the library DotNetAgent.Api.dll to your web application project.

2. The API dll is available in the API Manager Tool installed location (C:\Program Files (x86)\APM Insight\APM Insight.NET Agent\AgentApi\DotNetAgent.Api.dll).

Note: For Agent below v2.3, locate the API dll from,(C:\Program Files\APM Insight\APM Insight .NET Agent (<x64 or x86>)\AgentApi\DotNetAgent.Api.dll).

3. The API contains a class named CustomTracker to track the performance of a method.

CustomTracker Class and its Methods

  • CustomTracker(Type thisType)

    thisType - The type of current class or base class.

    eg: CustomTracker dotNetAgentCustomTracker = new CustomTracker(this.GetType());

  • CustomTracker(Type thisType, string methodName)

    thisType - The type of current class or base class.

    methodName - The name of the method to be monitored.

    eg: CustomTracker dotNetAgentCustomTracker = new CustomTracker(this.GetType(),"BasicDetails");

  • CustomTracker(Type thisType, string className, string methodName)

    thisType - The type of current class or base class.

    className - The name of the class to be monitored.

    methodName - The name of the method to be monitored.

    eg: CustomTracker dotNetAgentCustomTracker = new CustomTracker(this.GetType(), "EmpController", "BasicDetails");

  • CustomTracker.StartTracker (Type thisType, string className, string methodName)

    This method is used to start the metric collection for the custom method.

    It is not required as it is called in constructor itself.

  • CustomTracker.StopTracker()

    The metric collection will be stopped on calling this method.

    Always call it in a finally block.

4. Create an instance of CustomTracker class at the beginning of a method and invoke StopTracker() at the end of the method.

5. We can create CustomTracker instance with using{} block. The StopTracker() method will be called when disposing object automatically.

Examples

The following examples show the usage of this CustomTracker class:

Example 1: Using the "using" statement:

public ActionResult BasicDetails(int id = 0)
{

AdminBL objadmin = new AdminBL();

using(CustomTracker customTracker = new CustomTracker(base.GetType(),"BasicDetails"))

{

    ASPSite.BL.MYSQLReference.BasicDetails basicDetails = objadmin.getBasicDetails(id);

      EmpApp.Models.BasicDetails basic = getBasicDetailsModel(basicDetails);

      return View(basic);

     }

}


Example 2: Using StartTracker and StopTracker within a try finally block:

public ActionResult BasicDetails(int id = 0)
{

    CustomTracker customTracker = null;

    AdminBL adminBL = new AdminBL();

    try

     {

        customTracker = new CustomTracker(base.GetType(),"BasicDetails");

        ASPSite.BL.MYSQLReference.BasicDetails basicDetails = adminBL.getBasicDetails(id);

        EmpApp.Models.BasicDetails basicDetailsModel = getBasicDetailsModel(basicDetails);

     }

    finally

    {

    customTracker.StopTracker();

    }

return View(basicDetailsModel);

}


Example 3: Using CustomTracker to Instrument part of a code.

public ActionResult BasicDetails(int id = 0)
{

      AdminBL adminBL = new AdminBL();

      using(CustomTracker customTracker = new CustomTracker(base.GetType(),"BasicDetails"))

    {

//Instrumenting part of a code. To check the time taken by the function FetchAllEmployees and the SQL calls made from this function.

using(CustomTracker fetchAllEmpTracker = new CustomTracker(base.GetType(),"FetchAllEmployees"))

          {

     FetchAllEmployees();

            }
ASPSite.BL.MYSQLReference.BasicDetails basicDetails = objadmin.getBasicDetails(id);

EmpApp.Models.BasicDetails basic = getBasicDetailsModel(basicDetails);
}

return View(basicDetailsModel);

Note:

  1. If the method name or class name is not given in the CustomTracker, it will attempt to get the current method name and class name by itself.
  2. The StartTracker() method will be called in constructor by default.
  3. If the agent not installed or the agent service stopped the invoked methods will have no effect.