001/*
002 *  Copyright 2012 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.runtime.plugins.admin.jvmstatus.monitoring.sample;
017
018import java.io.IOException;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.rrd4j.DsType;
023import org.rrd4j.core.RrdDef;
024import org.rrd4j.core.Sample;
025
026import org.ametys.runtime.plugins.admin.jvmstatus.RequestCountListener;
027import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.SampleManager;
028
029/**
030 * {@link SampleManager} for collecting the throughput and the number
031 * of active HTTP requests .
032 */
033public class HttpRequestSampleManager extends AbstractSampleManager
034{
035    private long _lastCount;
036    
037    @Override
038    protected void _configureDatasources(RrdDef rrdDef)
039    {
040        _registerDatasources(rrdDef, "processed", DsType.GAUGE, 0, Double.NaN);
041    }
042
043    @Override
044    protected Map<String, Object> _internalCollect(Sample sample) throws IOException
045    {
046        Map<String, Object> result = new HashMap<>();
047        try
048        {
049            long processed = RequestCountListener.getTotalRequestCount() - _lastCount;
050            sample.setValue("processed", processed);
051            result.put("processed", processed);
052            _lastCount = RequestCountListener.getTotalRequestCount();
053        }
054        catch (IllegalStateException e)
055        {
056            // empty : no value means an error
057        }
058        return result;
059    }
060
061    @Override
062    protected String _getGraphTitle()
063    {
064        return "HTTP request";
065    }
066}