14 February 2007

DMS code sample

The EAR file with the src is here.

As I mentioned, here's an example of a DMS instrumented servlet I used as an demo recently.
The simple servlet processes the request and depending on the type of request received, sleeps for a specific amount of time. Basically, if you request a vegetable it takes 5000 msecs, if you request some fruit it takes 2000 msecs.

Where it is a little more interesting is where the DMS API is used to measure the different timing points.
The DMS PhaseEvent class is used to create different items to be measured and exposed in specified ways.

For example, to set up a metric to measure some stats related to processing of a vegetable request, the following is used:

Noun base = Noun.create(SERIES_NAME);
Noun root = Noun.create(base, "eating", "eatingseries");

vegetableComp = PhaseEvent.create(root, VEGETABLE_NAME, "Time taken to eat veggies");
vegetableComp.deriveMetric(Sensor.time);
vegetableComp.deriveMetric(Sensor.average);
vegetableComp.deriveMetric(Sensor.completed);
vegetableComp.deriveMetric(Sensor.maxActive);

fruitComp = PhaseEvent.create(root, FRUIT_NAME, "Time taken to eat fruit");
fruitComp.deriveMetric(Sensor.time);
fruitComp.deriveMetric(Sensor.average);
fruitComp.deriveMetric(Sensor.completed);
fruitComp.deriveMetric(Sensor.maxActive);

This will expose an event struicture that measures the specified attributes: total time, average time, complete and max time taken for the case where a vegetable or a piece of fruit is eaten.

In the servlet, the corresponding PhasEvent is used to measure the requested action type:
if (action.equalsIgnoreCase(VEGETABLE_NAME)) {
try {
out.println("<p>");
out.println(VEGETABLE_NAME + " start ... ");
out.flush();
long now = System.currentTimeMillis();
dmsToken = vegetableComp.start();
Thread.sleep(VEGETABLE_PAUSE);
vegetableComp.stop(dmsToken);
stopped = true;
out.println("stop (" + (System.currentTimeMillis() - now) + ")");
out.println("</p>");
out.flush();
} catch (Exception e) {
// nada
} finally {
if (!stopped) {
vegetableComp.abort(dmsToken);
}
}
} else if (action.equalsIgnoreCase(FRUIT_NAME)) {
...
}
The servlet finishes off by using the DMSConsole class to render the current set of DMS stats available to the runtime:
out.println("<h3>DMSConsole</h3>");
DMSConsole.getConsole().dump(out, SERIES_NAME);

When the servlet is accessed it looks like this:



Note how the DMS metrics for the different types of tasks are shown, and expose the relevant details to show how the task is performing.

The DMS stats can also be viewed with the Spy application that ships as part of OC4J -- http://localhost:8888/dms0/Spy

Observe how the eatingseries metrics are listed amongst the rest of the OC4J metrics and correspond to what we are seeing from the direct use of DMSConsole when viewed in text mode.



And nicely, see how the metric definitions we provided are shown on the help page.



No comments: