En Ru
Easy to use
GS Monitoring Framework

GS MONITORING FRAMEWORK TUTORIAL


Here you can find a step-by-step example of using GS Monitoring Framework.

Let’s imagine we have some application that collects statistical data and writes it to the DB fields. Sometimes collection of data stops for unknown reason. The task is to find out when and why it happens.


Step 1


Include GS Monitoring Framework jar file into project. If you still do not have GS Monitoring Framework please just fill in the field below and push the button. Our specialists will contact you.

My e-mail or phone:
 
 
Example: e-mail: [you@your.domain.name] or phone number with country code: [+375 017 259 19 11]


Privacy: Contact me form requires your e-mail or phone. We do not disclose, sell, rent or transfer your information to any third party. We use this information for getting in touch with you only.

Step 2


Add monitor initializer class. In this example we added a list of field names. In case of exception index of the field is replaced with the field name.
It is also possible to get etalon values from its storage with the help of initializer. For example getting of etalon time performance values from file.

package com.gersis-software.example.statistics.monitoring;

import com.gersis_software.gsmf.initializers.IMonitorInitializer;
import com.gersis_software.gsmf.monitors.IMonitor;

import java.util.ArrayList;

/**
 * Initializer for StatisticsServiceMonitor
 *
 * Copyright (c) GERSIS SOFTWARE 2009
 */

public class StatServiceMonInit
ializer implements IMonitorInitializer {

    /**
     * Initialize StatisticsServiceMonitor
     *
     * Add field names to monitor
     *
     * @param aMonitor
     */


    public void initializeMonitor(IMonitor aMonitor) {
        ArrayList<String> fields = new ArrayList<String>();
       

        fields.add("StatDate
");
        fields.add("CreatedModifications");

        StatServiceMonitor serviceMonitor = (StatServiceMonitor)aMonitor;
        serviceMonitor.setFieldNames(fields);
    }
}



 

Step 3


Add additional info class. In current example this class is used to store information about exception.

package com.gersis-software.example.statistics.monitoring;

import com.gersis_software.gsmf.monitors.IInfoForMonitor;

/**
 * Additinal information for StatisticsService
 *
 * Copyright (c) GERSIS SOFTWARE 2009
 */

public class StatServiceInfo implements IInfoForMonitor {

    //index of field that throw exception on update
    private int _field;
    //exception that has been thrown
    private Exception _exception;

    /*
        Getters and Setters for fields
     */


    public int getField() {
        return _field;
    }

    public void setField(int field) {
        this._field = field;
    }

    public Exception getException() {
        return _exception;
    }

    public void setException(Exception exception) {
        this._exception = exception;
    }

}




Step 4


Add class of monitor that analyzes received information. In case of abnormal situation, data will be written to the desired destination (can be selected as adapter class in monitoring-settings.xml).
In this example abnormal situation is when exception parameter from StatisticsServiceInfo is not null.

package com.gersis-software.example.statistics.monitoring;

import com.gersis_software.gsmf.monitors.AbstractMonitor;
import com.gersis_software.gsmf.monitors.IMonitor;
import com.gersis_software.gsmf.adapters.IMonitorAdapter;
import com.gersis-software.example.statistics.StatisticsService;

import java.util.Set;
import java.util.ArrayList;
import java.util.Date;

/**
 * Monitor that will process information about StatisticsService
 *
 * Copyright (c) GERSIS SOFTWARE 2009
 */

public class StatServiceMonitor extends AbstractMonitor<StatisticsService, StatisticsService, StatServiceInfo> {

    private StatServiceInfo _serviceInfo;
    private StatisticsService _service;

    private ArrayList<String> _fieldNames;

    public StatServiceMonitor(Set<IMonitorAdapter> aAdapters, String aName) {
        super(aAdapters, aName);
    }

    /**
     * Creates Description if there was abnormal situation
     *
     */


    protected void createAbnormalStateDescription() {
        StringBuffer description = new StringBuffer(new Date(System.currentTimeMillis()) + " : StatisticsService fails.\n");

        //if service not null than StatisticsDAO field is null
        if (_service != null) {
            description.append("Reason: StatisticsDAO is null\n");
        }

        //if serviceInfo not null, than there was exception while updating field
        if (_serviceInfo != null) {
            description.append("Reason: Exception on updating field StatisticsListener.").append(_fieldNames.get(_serviceInfo.getField())).append(".");
            description.append("\nException: ").append(_serviceInfo.getException().toString());
        }

        setAbnormalStateDescription(description.toString());
    }

    /**
     * Returns copy of Monitor
     *
     * @return copy of this Monitor
     */


    protected synchronized IMonitor<StatisticsService, StatisticsService, StatServiceInfo> getCopy() {
        StatServiceMonitor copy = new StatServiceMonitor(getMonitorAdapters(), getName());
        copy._service = _service;
        copy._serviceInfo = _serviceInfo;
        copy._fieldNames = _fieldNames;
        copy.setAbnormalStateDescription(getAbnormalStateDescription());

        return copy;
    }

    /**
     * Process monitored value
     *
     * @param statisticsService monitored value
     */


    public void processMonitoredValue(StatisticsService statisticsService) {
        processMonitoredValue(statisticsService, null);
    }

    /**
     * Process monitored value with additional information
     *
     * @param statisticsService monitored value
     * @param statisticsServiceInfo additional information
     */


    public void processMonitoredValue(StatisticsService statisticsService, StatServiceInfo statisticsServiceInfo) {
        executeParallelProcessing(statisticsService, statisticsServiceInfo);
    }

    /**
     * Returns current monitored value
     *
     * @return current monitored value
     */


    public StatisticsService getCurrentMonitoredValue() {
        return _service;
    }

    /**
     * Returns EtalonValue (for this monitor we has no etalon values)
     *
     * @return
     */


    public StatisticsService getEtalonValue() {
        return null;
    }

    /**
     * Set EtalonValue (for this monitor we has no etalon values)
     *
     * @param statisticsService
     */


    public void setEtalonValue(StatisticsService statisticsService) {
       
    }

    /**
     * Analyze MonitoredValue
     *
     * @param statisticsService monitored value
     * @param statisticsServiceInfo additional information
     */


    public void analyzeMonitoredValue(StatisticsService statisticsService, StatServiceInfo statisticsServiceInfo) {

        //if StatisticsDAO is null than this is abnormal situation
        if (statisticsService.getStatisticsDAO() == null) {
            _service =  statisticsService;
            onAbnormalState();
        }

        if (statisticsServiceInfo != null) {
            Exception exception = statisticsServiceInfo.getException();
            //if there is exception than this is abnormal situation
            if (exception != null) {
                _serviceInfo = statisticsServiceInfo;
                onAbnormalState();
            }
        }
    }

    public StatisticsServiceInfo getInfo() {
        return _serviceInfo
    }
   
    public void setFieldNames(ArrayList<String> fieldNames) {
        this._fieldNames = fieldNames;
    }

    /**
     * Execute analyzing of monitored value in parallel thread
     *
     * @param statisticsService    monitored value
     * @param statisticsServiceInfo additional information
     */


    @Override
    protected void executeParallelProcessing(final StatisticsService statisticsService, final StatServiceInfo statisticsServiceInfo) {
        Thread executor = new Thread(new Runnable() {
            public void run() {
                analyzeMonitoredValue(statisticsService, statisticsServiceInfo);
            }
        });
        executor.start();  
    }
}



 

Step 5


Add configuration files to the application root folder.

monitoring.cfg
factory_class            com.gersis_software.gsmf.MonitorSettingsXmlFactory
xmlconfig_filename        monitoring-settings.xml


monitoring-settings.xml
<allMonitors>
    <monitorType class=" com.gersis-software.example.statistics.monitoring.StatServiceMonitor">
        <monitorInstance name="StatServiceMonitor">
            <adapters>
                <adapter class="com.gersis_software.gsmf.adapters.FileLogMonitorAdapter"/>
            </adapters>
            <initializer class="com.gersis-software.example.statistics.monitoring.StatServiceMonInitializer"/>
        </monitorInstance>
    </monitorType>
</allMonitors>




Step 6


Add code, that collects information, to the potentially problematic points. Code inserted for monitoring purposes is marked yellow.

//creates a new monitor instance
    private IMonitor monitor = MonitorManager.getInstance().getMonitor("StatServiceMonitor", StatServiceMonitor.class);

    /**
     * Collects statistics and updates the database.
     */

    public void run() {
        LOG.log("Regular ststistics collection.");
        Statistics statistics = new Statistics();

        int field = 0;
        Exception exception = null;


        //Add try/catch block to catch Exception (if it will be thrown)
        try
{
            statistics.setStatDate(new Date(System.currentTimeMillis()));
            field++;
            statistics.setCreatedModifications(StatisticsListener.getModifications().count());
            field++;       
        }
        catch (Exception e) {
            exception = e;
        }
        finally {
            //Process information about exception to the monitor
            StatServiceInfo info = new StatServiceInfo();
            //exception (can be null if there was no exceptions)
            info.setException(exception);
            //index of field that was cause of exception
            info.setField(field);
            monitor.processMonitoredValue(this, info);
        }


        statisticsDAO.update(statistics);
    }

Step 7


Messages from the monitor will be stored to the file “monitoring.log”. It is located in the root folder of application, like monitoring.cfg and monitoring-settings.xml files.