Metrics API

The Metrics API provides metrics about internal resources used by Telerik Data Access. It allows you to observe the performance and the scalability of the system, the amounts of database connections, parallel context instances, and transactions.

Using the History Class

The Telerik.OpenAccess.Diagnostics.History class provides historic execution information regarding the application behavior:

Retrieving All Active Database Instances

The History.OpenDatabases static method allows you to retrieve a list of all active database instances. The method returns a string array with the connection names of all opened database instances. The following example demonstrates you how to use the OpenDatabases method.

using (EntitiesModel dbContext = new EntitiesModel())
{
   string[] databases = History.OpenDatabases();
   Debug.Assert(databases.Length == 0);

   var categories = dbContext.Categories;

   databases = History.OpenDatabases();
   Debug.Assert(databases.Length == 1);
}
Using dbContext As New EntitiesModel()
 Dim databases() As String = History.OpenDatabases()
 Debug.Assert(databases.Length = 0)

 Dim categories = dbContext.Categories

 databases = History.OpenDatabases()
 Debug.Assert(databases.Length = 1)
End Using

If no database, scope or context instance is available, the OpenDatabases method returns the possible history entry points. In the following example the original context is disposed first, and then the OpenDatabases method is called. The OpenDatabases method will return one record.

using (EntitiesModel dbContext = new EntitiesModel())
{
   var categories = dbContext.Categories;

   dbContext.Dispose();

   string[] databases = History.OpenDatabases();
   Debug.Assert(databases.Length == 1);
}
Using dbContext As New EntitiesModel()
 Dim categories = dbContext.Categories

 dbContext.Dispose()

 Dim databases() As String = History.OpenDatabases()
 Debug.Assert(databases.Length = 1)
End Using

Retrieving ContextMetrics

The GetContextMetrics method returns the metric counters for the specified context and optionally resets the internal values. The context metric counters can be used to obtain runtime behavior statistics in order to aid application monitoring. The counters returned by the method are affected by the actions that this context performs. The ContextMetrics class provides the following context specific metric values:

  • Sql_Query - gets the number of the executed SQL queries.
  • Sql_Delete - gets the number of the executed SQL delete statements.
  • Sql_Insert - gets the number of the executed SQL insert statements.
  • Sql_Update - gets the number of the executed SQL update statements.
  • Sql_FetchedRows - gets the number of the fetched rows.
  • Sql_Commit - gets the number of the executed SQL commit statements.
  • Sql_Rollback - gets the number of the executed SQL rollback statements.
  • Sql_Flush - gets the number of the flush operations to the relational server.
  • Sql_CommitError - gets the number of errors during SQL commit operations.
  • Sql_FlushError - gets the number of errors during flush operations.
  • Sql_QueryError - gets the number of errors during SQL query execution.
  • Sql_FetchError - gets the number of errors during fetch of the query result sets.
  • Sql_Enlist - gets the number of enlistments into system transactions.
  • Length - gets the number of metric values for the current ContextMetrics instance.

The following example demonstrates you how to get the DatabaseEvents for the current database.

BackendConfiguration backend = EntitiesModel.GetBackendConfiguration();
backend.Runtime.MetricCapacity = 100;
backend.Runtime.MetricInterval = 1000;

using (EntitiesModel dbContext = new EntitiesModel(backend))
{
   var items = dbContext.Categories.ToList();
   History history = new History(dbContext);
   DatabaseEventsCollection events = history.GetDatabaseEvents(null, null, null);

   if (events != null && events.Count > 0)
   {
       foreach (ITraceEvent item in events)
       {
       }
   }
}
Dim backend As BackendConfiguration = EntitiesModel.GetBackendConfiguration()
backend.Runtime.MetricCapacity = 100
backend.Runtime.MetricInterval = 1000

Using dbContext As New EntitiesModel(backend)
 Dim items = dbContext.Categories.ToList()
 Dim _history As New History(dbContext)
 Dim events As DatabaseEventsCollection = _history.GetDatabaseEvents(Nothing, Nothing, Nothing)

 If events IsNot Nothing AndAlso events.Count > 0 Then
  For Each item As ITraceEvent In events
  Next item
 End If
End Using

The DatabaseEvents are tightly connected with the Log Level setting of the domain model.

For example, if you set the Log Level setting to errors (logs only serious errors such as connections time out - this is the default value) and no errors occur during the execution of the program, then the GetDatabaseEvents method will return null.