/*
 * Copyright 2013 Rackspace
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.rackspacecloud.blueflood.utils;


import com.codahale.metrics.*;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;
import com.rackspacecloud.blueflood.service.Configuration;
import com.rackspacecloud.blueflood.service.CoreConfig;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

public class Metrics {
    private static final MetricRegistry registry = new MetricRegistry();
    private static final GraphiteReporter reporter;

    static {
        Configuration config = Configuration.getInstance();

        if (!config.getStringProperty(CoreConfig.GRAPHITE_HOST).equals("")) {
            Graphite graphite = new Graphite(new InetSocketAddress(config.getStringProperty(CoreConfig.GRAPHITE_HOST), config.getIntegerProperty(CoreConfig.GRAPHITE_PORT)));

            reporter = GraphiteReporter
                    .forRegistry(registry)
                    .convertDurationsTo(TimeUnit.SECONDS)
                    .convertRatesTo(TimeUnit.MILLISECONDS)
                    .prefixedWith(config.getStringProperty(CoreConfig.GRAPHITE_PREFIX))
                    .build(graphite);

            reporter.start(30l, TimeUnit.SECONDS);
        } else {
            reporter = null;
        }
    }

    public static MetricRegistry getRegistry() {
        return registry;
    }

    public static Meter meter(Class kls, String... names) {
        return getRegistry().meter(MetricRegistry.name(kls, names));
    }

    public static Timer timer(Class kls, String... names) {
        return getRegistry().timer(MetricRegistry.name(kls, names));
    }

    public static Histogram histogram(Class kls, String... names) {
        return getRegistry().histogram(MetricRegistry.name(kls, names));
    }

    public static Counter counter(Class kls, String... names) {
        return getRegistry().counter(MetricRegistry.name(kls, names));
    }
}

@@ -793,16 +793,235 @@
 porter;%0A
+import com.codahale.metrics.jvm.BufferPoolMetricSet;%0Aimport com.codahale.metrics.jvm.GarbageCollectorMetricSet;%0Aimport com.codahale.metrics.jvm.MemoryUsageGaugeSet;%0Aimport com.codahale.metrics.jvm.ThreadStatesGaugeSet;%0A
 import c
@@ -1128,16 +1128,100 @@
 onfig;%0A%0A
+import javax.management.MBeanServer;%0Aimport java.lang.management.ManagementFactory;%0A
 import j
@@ -2085,24 +2085,527 @@
 graphite);%0A%0A
+            // register jvm metrics%0A            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();%0A            if (!System.getProperty(%22java.version%22).split(%22%5C%5C.%22)%5B1%5D.equals(%226%22)) %7B%0A                // if not running 1.6%0A                registry.registerAll(new BufferPoolMetricSet(mbs));%0A            %7D%0A            registry.registerAll(new GarbageCollectorMetricSet());%0A            registry.registerAll(new MemoryUsageGaugeSet());%0A            registry.registerAll(new ThreadStatesGaugeSet());%0A
             

add jvm metrics