001/*
002 * (C) Copyright 2010 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
020package org.nuxeo.runtime.management.metrics;
021
022import java.io.IOException;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.javasimon.CallbackSkeleton;
027import org.javasimon.Counter;
028import org.javasimon.Sample;
029import org.javasimon.Split;
030
031public class MetricSerializingCallback extends CallbackSkeleton {
032
033    protected static final Log log = LogFactory.getLog(MetricSerializingCallback.class);
034
035    protected final MetricSerializer serializer;
036
037    public MetricSerializingCallback(MetricSerializer serializer) {
038        this.serializer = serializer;
039    }
040
041    @Override
042    public void stopwatchStop(Split split) {
043        toStream(split.getStopwatch().sample());
044    }
045
046    @Override
047    public void counterSet(Counter counter, long val) {
048        toStream(counter.sample());
049    }
050
051    protected void toStream(Sample sample) {
052        try {
053            serializer.toStream(sample);
054        } catch (IOException e) {
055            log.info("not streamed " + sample, e);
056        }
057    }
058
059}