Adhoc Monitoring with Scripting-Client in GlassFish v3

Monitoring with Scripting-Client Adhoc monitoring is a powerful feature which will allow the user to monitor the parts of App Server at a very granular level. Scripting-Client and DTrace are the new monitoring features which would allow the Adhoc monitoring of GlassFish v3. In this blog I am going to go over the Scripting-Client feature of Monitoring in GlassFish v3.

\* - Note that the Scripting-Client and DTrace are value-add features and available with paid subscription.

We will see how to write a simple javascript to see how many requests are received for a given App. And later in this blog we will see more advanced functionality where you can even see how much time to serve an HTTP request.

Following is the Javascript to calculate number of requests coming in for an App.

//Script to count the total number of requests received
request_params = java.lang.reflect.Array.newInstance(java.lang.String, 6);
request_params[0]="appName";
request_params[1]="hostName";
request_params[2]="serverName";
request_params[3]="serverPort";
request_params[4]="contextPath";
request_params[5]="servletPath";

nrequests = 0;
function requestStartEvent(appName,hostName,serverName,serverPort,contextPath,
    servletPath){
        nrequests=nrequests+1;
        client.print("js> New request received. Total count = " + nrequests + "\\n");
}

scriptContainer.registerListener('glassfish:web:http-service:requestStartEvent',
    request_params , 'requestStartEvent');


Note that in the above script, we are using the Scripting-Client API to do print a message which will be displayed on your asadmin console where you run the 'run-script' command. We are also using another Scripting-Client API, scriptContainer-registerListener to register a listener for a (requestStartEvent) probe. In this API, the first parameter we pass is the probe id (do a list-probes to see what probes are available), the second is the list of parameter (names) that we are interested in, the third is the callback method name in this javascript.


Once you have the script, you would deploy and run this script using a single asadmin run-script command. As you start accessing an App, you would start seeing the request counter getting incremented.

Requests Received


Now I am going to show you a more powerful feature, the call tracing. Here we are trying to see how much time is spent in serving the request. We listen to both the requestStart and requestEnd probes and using the thread id, we will calculate the time between these two events, which gives us the total time taken by this request. Following is how the Javascript looks like.

//This script will calculate the time taken by each request for an App
//requestStartEvent
request_params = java.lang.reflect.Array.newInstance(java.lang.String, 6);
request_params[0]="appName";
request_params[1]="hostName";
request_params[2]="serverName";
request_params[3]="serverPort";
request_params[4]="contextPath";
request_params[5]="servletPath";

var startTime;
var object = new Object();
var nrequestStartEvent=0;

function requestStartEvent(appName,hostName,serverName,serverPort,contextPath,
    servletPath){

    nrequestStartEvent=nrequestStartEvent+1;
        startTime = (new Date()).getTime();
    //insert the request time in Map
        key = java.lang.Thread.currentThread().getId();
        object[key] = startTime;
        client.print(
            'Count: '+ nrequestStartEvent +'\\n'+
            'Event: glassfish:web:http-service:requestStartEvent' +'\\n'+
            'Application: '+appName+'\\n'+
            'Host: ' + hostName +'\\n'+
            'Server: ' + serverName +'\\n'+
            'HTTP Port: ' + serverPort +'\\n'+
            'Context Path: ' + contextPath +'\\n'+
            'Servlet Path: ' + servletPath + '\\n' +
            'Current Thread: ' + java.lang.Thread.currentThread().getId() +
        '\\n\\n');
}

scriptContainer.registerListener('glassfish:web:http-service:requestStartEvent',
    request_params , 'requestStartEvent');

//requestEndEvent
request1_params = java.lang.reflect.Array.newInstance(java.lang.String, 7);
request1_params[0]="appName";
request1_params[1]="hostName";
request1_params[2]="serverName";
request1_params[3]="serverPort";
request1_params[4]="contextPath";
request1_params[5]="servletPath";
request1_params[6]="statusCode";


var nrequestEndEvent=0;

function requestEndEvent(appName,hostName,serverName,serverPort,contextPath,
    servletPath,statusCode){

    nrequestEndEvent=nrequestEndEvent+1;
        key = java.lang.Thread.currentThread().getId();
        startTime = object[key];
        if (startTime == null)
            client.print("Error getting the startTime for thread = " + key);
        else
            delete[key];
        totalTime = (new Date()).getTime() - startTime;
    //insert the request time in Map
        object[key] = startTime;


        client.print(
            'Time Taken: ' + ((new Date()).getTime()-startTime) + ' ms\\n' +
            'Count: '+nrequestEndEvent+'\\n'+
            'Event: glassfish:web:http-service:requestEndEvent' +'\\n'+
            'Application: '+appName+'\\n'+
            'Host: ' + hostName +'\\n'+
            'Server: ' + serverName +'\\n'+
            'HTTP Port: ' + serverPort +'\\n'+
            'Context Path: ' + contextPath +'\\n'+
            'Servlet Path: ' + servletPath +'\\n'+
            'Status Code: ' + statusCode + '\\n' +
            'Current Thread: ' + java.lang.Thread.currentThread().getId() + '\\n' +
         '\\n\\n');
}
scriptContainer.registerListener('glassfish:web:http-service:requestEndEvent',
    request1_params, 'requestEndEvent');



Once you have the script, you would deploy and run this script using a single asadmin run-script command. As you start accessing an App, you would start seeing the time taken by each request to your App.

Application request timer

This will be quite a powerful feature where you can start doing the call tracing of a request into each of the GlassFish components. See here on how to do the same with Java code in your App (or a module).
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

bloggerprashanth

Search

Categories
Archives
« May 2015
SunMonTueWedThuFriSat
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      
Today