001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021package org.nuxeo.runtime.management.counters;
022
023import java.util.Map;
024import java.util.concurrent.ConcurrentHashMap;
025
026import org.javasimon.CallbackSkeleton;
027import org.javasimon.Counter;
028
029/**
030 * Listen to Simon events to store past values of the counters History is kept in memory using
031 * {@link CounterHistoryStack}
032 *
033 * @author Tiry (tdelprat@nuxeo.com)
034 * @deprecated since 11.4: use dropwizard metrics instead
035 */
036@Deprecated(since = "11.4")
037public class CounterHistoryRecorder extends CallbackSkeleton {
038
039    protected Map<String, CounterHistoryStack> counterHistory = new ConcurrentHashMap<>();
040
041    protected int historyLength = 100;
042
043    public CounterHistoryRecorder(int size) {
044        historyLength = size;
045    }
046
047    protected CounterHistoryStack getCounterHistoryStack(Counter counter) {
048        CounterHistoryStack stack = counterHistory.get(counter.getName());
049        if (stack == null) {
050            stack = new CounterHistoryStack(historyLength);
051            counterHistory.put(counter.getName(), stack);
052        }
053        return stack;
054    }
055
056    protected void storeCounter(Counter counter) {
057        getCounterHistoryStack(counter).push(new long[] { System.currentTimeMillis(), counter.getCounter() });
058    }
059
060    @Override
061    public void counterDecrease(Counter counter, long dec) {
062        storeCounter(counter);
063    }
064
065    @Override
066    public void counterSet(Counter counter, long val) {
067        storeCounter(counter);
068    }
069
070    @Override
071    public void counterIncrease(Counter counter, long inc) {
072        storeCounter(counter);
073    }
074
075    public CounterHistoryStack getCounterHistory(String counterName) {
076        return counterHistory.get(counterName);
077    }
078}