Data Access has been discontinued. Please refer to this page for more information.

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 (FluentModel dbContext = new FluentModel())
{
   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 FluentModel()
 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 (FluentModel dbContext = new FluentModel())
{
   var categories = dbContext.Categories;

   dbContext.Dispose();

   string[] databases = History.OpenDatabases();
   Debug.Assert(databases.Length == 1);
}
Using dbContext As New FluentModel()
 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 = FluentModel.GetBackendConfiguration();
backend.Runtime.MetricCapacity = 100;
backend.Runtime.MetricInterval = 1000;

using (FluentModel dbContext = new FluentModel(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 = FluentModel.GetBackendConfiguration()
backend.Runtime.MetricCapacity = 100
backend.Runtime.MetricInterval = 1000

Using dbContext As New FluentModel(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 value of the LogEvents property of the model.

public static BackendConfiguration GetBackendConfiguration()
{
    BackendConfiguration backend = new BackendConfiguration();
    backend.Backend = "MsSql";
    backend.ProviderName = "System.Data.SqlClient";

    backend.Logging.LogEvents = LoggingLevel.Errors;

    CustomizeBackendConfiguration(ref backend);

    return backend;
}
Public Shared Function GetBackendConfiguration() As BackendConfiguration
    Dim backend As BackendConfiguration = New BackendConfiguration()
    backend.Backend = "MsSql"
    backend.ProviderName = "System.Data.SqlClient"

    backend.Logging.LogEvents = LoggingLevel.Errors

    CustomizeBackendConfiguration(backend)

    Return backend
End Function

For example, if you set the log level 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.