The Statistics API is the direct interface to ObjectGrid's internal statistics tree. Statistics are disabled by default, but can be enabled by setting a StatsSpec
. A StatsSpec is an interface for defining how ObjectGrid should monitor statistics. Once statistics are enabled, the user can query performance data using any of the methods described in monitoring applications. In this task, the focus will be on using the local StatsAccessor API to query data.
Before you begin
For more information about the statistics and module structure that ObjectGrid provides, see ObjectGrid StatsModule Overview.
About this task
The StatsAccessor is meant to be used when local to the ObjectGrid core. This means that a user can access statistics on any ObjectGrid instance that is in the same JVM as the running code. StatsAccessor may be applied through the following steps:
- Retrieving the StatsAccessor

- Setting the grid StastSpec

- Driving transactions to force data capture
- Querying a StatsFact
using the StatsAccessor API
- Interacting with the StatsModule

Retrieving the StatsAccessor
The StatsAccessor follows the singleton pattern. So, apart from problems related to classloader, there should only be one StatsAccessor per JVM. This class will serve as the main interface for all local statistics operations. The code below is an example of how to retrieve the accessor class. This operation should be called prior to any other ObjectGrid calls.
public class LocalClient {
public static void main(String[] args) {
StatsAccessor accessor = StatsAccessorFactory.getStatsAccessor();
}
}
Setting the grid StatsSpec
In this case the user will be setting this JVM to collect all statistics at the ObjectGrid level only. It is important to ensure that an application enables all statistics that might be needed prior to beginning any transactions. This example will set the StatsSpec using both a static constant field and using a spec String. The former is simpler since the field has already defined the spec, but the latter will allow the user to enable any combination of statistics required.
public static void main(String[] args) {
StatsAccessor accessor = StatsAccessorFactory.getStatsAccessor();
StatsSpec spec = new StatsSpec(StatsSpec.OG_ALL);
accessor.setStatsSpec(spec);
StatsSpec spec = new StatsSpec("og.all=enabled");
accessor.setStatsSpec(spec);
}
Driving transactions to force data capture
Statistics collection has been enabled at the ObjectGrid level, but in order to show useful data, the user will need to start driving transactions against the grid. The code excerpt below inserts a record into MapA, which is in ObjectGridA. Since the statistics are at the ObjectGrid level, any map within the ObjectGrid would yield the same results.
public static void main(String[] args) {
StatsAccessor accessor = StatsAccessorFactory.getStatsAccessor();
StatsSpec spec = new StatsSpec(StatsSpec.OG_ALL);
accessor.setStatsSpec(spec);
ObjectGridManager manager = ObjectGridmanagerFactory.getObjectGridManager();
ObjectGrid grid = manager.getObjectGrid("ObjectGridA");
Session session = grid.getSession();
Map map = session.getMap("MapA");
session.begin();
map.insert("SomeKey", "SomeValue");
session.commit();
}
Querying a StatsFact using the StatsAccessor API
As mentioned in the statistics overview above, every statistics path is associated with a StatsFact. The StatsFact is a generic placeholder used to organize and contain a StatsModule. Before a user can access the actual statistics module, the StatsFact will have to be retrieved.
public static void main(String[] args) {
StatsAccessor accessor = StatsAccessorFactory.getStatsAccessor();
StatsSpec spec = new StatsSpec(StatsSpec.OG_ALL);
accessor.setStatsSpec(spec);
ObjectGridManager manager = ObjectGridManagerFactory.getObjectGridManager();
ObjectGrid grid = manager.getObjectGrid("ObjectGridA");
Session session = grid.getSession();
Map map = session.getMap("MapA");
session.begin();
map.insert("SomeKey", "SomeValue");
session.commit();
StatsFact fact = accessor.getStatsFact(new String[] {"EmployeeGrid"}, StatsModule.MODULE_TYPE_OBJECT_GRID);
}
Interacting with the StatsModule
The StatsModule object is contained within the StatsFact. A user can easily obtain a reference to the module using the StatsFact interface. Since the StatsFact is a generic interface, the user must cast the returned module to the expected StatsModule
type. Since this task collects ObjectGrid statistics, the returned StatsModule will be cast to an OGStatsModule. Once the module is cast, the user will have access to all of the available statistics.
public static void main(String[] args) {
StatsAccessor accessor = StatsAccessorFactory.getStatsAccessor();
StatsSpec spec = new StatsSpec(StatsSpec.OG_ALL);
accessor.setStatsSpec(spec);
ObjectGridManager manager = ObjectGridmanagerFactory.getObjectGridManager();
ObjectGrid grid = manager.getObjectGrid("ObjectGridA");
Session session = grid.getSession();
Map map = session.getMap("MapA");
session.begin();
map.insert("SomeKey", "SomeValue");
session.commit();
StatsFact fact = accessor.getStatsFact(new String[] {"EmployeeGrid"}, StatsModule.MODULE_TYPE_OBJECT_GRID);
OGStatsModule module = (OGStatsModule)fact.getStatsModule();
ActiveTimeStatistic timeStat = module.getTransactionTime("Default", true);
double time = timeStat.getMeanTime();
}
Additional Information