<commit_before>/*
 * 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.riemann.Riemann;
import com.codahale.metrics.riemann.RiemannReporter;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;
import com.codahale.metrics.jvm.BufferPoolMetricSet;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;
import com.codahale.metrics.log4j.InstrumentedAppender;
import com.rackspacecloud.blueflood.service.Configuration;
import com.rackspacecloud.blueflood.service.CoreConfig;
import org.apache.log4j.LogManager;

import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class Metrics {
    private static final MetricRegistry registry = new MetricRegistry();
    private static final GraphiteReporter reporter;
    private static final RiemannReporter reporter1;
    private static final JmxReporter reporter2;
    private static final String JVM_PREFIX = "jvm";

    static {
        Configuration config = Configuration.getInstance();

        // register jvm metrics
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        if (!System.getProperty("java.version").split("\\.")[1].equals("6")) {
            // if not running 1.6
            registry.registerAll(new PrefixedMetricSet(new BufferPoolMetricSet(mbs), JVM_PREFIX, "buffer-pool"));
        }
        registry.registerAll(new PrefixedMetricSet(new GarbageCollectorMetricSet(), JVM_PREFIX, "gc"));
        registry.registerAll(new PrefixedMetricSet(new MemoryUsageGaugeSet(), JVM_PREFIX, "memory"));
        registry.registerAll(new PrefixedMetricSet(new ThreadStatesGaugeSet(), JVM_PREFIX, "thread-states"));

        // instrument log4j
        InstrumentedAppender appender = new InstrumentedAppender(registry);
        appender.activateOptions();
        LogManager.getRootLogger().addAppender(appender);

        if (!config.getStringProperty(CoreConfig.RIEMANN_HOST).equals("")) {
            Riemann riemann = new Riemann(config.getStringProperty(CoreConfig.RIEMANN_HOST), config.getIntegerProperty(CoreConfig.RIEMANN_PORT));

            reporter1 = RiemannReporter
                    .forRegistry(registry)
                    .convertDurationsTo(TimeUnit.MILLISECONDS)
                    .convertRatesTo(TimeUnit.SECONDS);
            if (!config.getStringProperty(CoreConfig.RIEMANN_SEPARATOR).isEmpty()) {
                reporter1.useSeparatored(config.getStringProperty(CoreConfig.RIEMANN_SEPARATOR));
            }
            if (!config.getStringProperty(CoreConfig.RIEMANN_TTL).isEmpty()) {
                reporter1.withTtl(config.getFloatProperty(CoreConfig.RIEMANN_TTL));
            }
            if (!config.getStringProperty(CoreConfig.RIEMANN_LOCALHOST).isEmpty()) {
                reporter1.localHosted(config.getStringProperty(CoreConfig.RIEMANN_LOCALHOST));
            }
            if (!config.getStringProperty(CoreConfig.RIEMANN_TAGS).isEmpty()) {
                reporter1.tags(config.getListProperty(CoreConfig.RIEMANN_TAGS));
            }
            reporter1.build(riemann);

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

        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.MILLISECONDS)
                    .convertRatesTo(TimeUnit.SECONDS)
                    .prefixedWith(config.getStringProperty(CoreConfig.GRAPHITE_PREFIX))
                    .build(graphite);
<commit_msg>fix typos and catch IOException<commit_after>/*
 * 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.riemann.Riemann;
import com.codahale.metrics.riemann.RiemannReporter;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;
import com.codahale.metrics.jvm.BufferPoolMetricSet;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;
import com.codahale.metrics.log4j.InstrumentedAppender;
import com.rackspacecloud.blueflood.service.Configuration;
import com.rackspacecloud.blueflood.service.CoreConfig;
import org.apache.log4j.LogManager;

import javax.management.MBeanServer;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class Metrics {
    private static final MetricRegistry registry = new MetricRegistry();
    private static final GraphiteReporter reporter;
    private static final RiemannReporter reporter1;
    private static final JmxReporter reporter2;
    private static final String JVM_PREFIX = "jvm";

    static {
        Configuration config = Configuration.getInstance();

        // register jvm metrics
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        if (!System.getProperty("java.version").split("\\.")[1].equals("6")) {
            // if not running 1.6
            registry.registerAll(new PrefixedMetricSet(new BufferPoolMetricSet(mbs), JVM_PREFIX, "buffer-pool"));
        }
        registry.registerAll(new PrefixedMetricSet(new GarbageCollectorMetricSet(), JVM_PREFIX, "gc"));
        registry.registerAll(new PrefixedMetricSet(new MemoryUsageGaugeSet(), JVM_PREFIX, "memory"));
        registry.registerAll(new PrefixedMetricSet(new ThreadStatesGaugeSet(), JVM_PREFIX, "thread-states"));

        // instrument log4j
        InstrumentedAppender appender = new InstrumentedAppender(registry);
        appender.activateOptions();
        LogManager.getRootLogger().addAppender(appender);

        if (!config.getStringProperty(CoreConfig.RIEMANN_HOST).equals("")) {
            RiemannReporter tmpreporter;
            try {
                Riemann riemann = new Riemann(config.getStringProperty(CoreConfig.RIEMANN_HOST), config.getIntegerProperty(CoreConfig.RIEMANN_PORT));

                RiemannReporter.Builder builder = RiemannReporter
                        .forRegistry(registry)
                        .convertDurationsTo(TimeUnit.MILLISECONDS)
                        .convertRatesTo(TimeUnit.SECONDS);
                if (!config.getStringProperty(CoreConfig.RIEMANN_SEPARATOR).isEmpty()) {
                    builder.useSeparator(config.getStringProperty(CoreConfig.RIEMANN_SEPARATOR));
                }
                if (!config.getStringProperty(CoreConfig.RIEMANN_TTL).isEmpty()) {
                    builder.withTtl(config.getFloatProperty(CoreConfig.RIEMANN_TTL));
                }
                if (!config.getStringProperty(CoreConfig.RIEMANN_LOCALHOST).isEmpty()) {
                    builder.localHost(config.getStringProperty(CoreConfig.RIEMANN_LOCALHOST));
                }
                if (!config.getStringProperty(CoreConfig.RIEMANN_TAGS).isEmpty()) {
                    builder.tags(config.getListProperty(CoreConfig.RIEMANN_TAGS));
                }
                tmpreporter = builder.build(riemann);

                tmpreporter.start(30l, TimeUnit.SECONDS);
            } catch (IOException e) {
                tmpreporter = null;
            }
            reporter1 = tmpreporter;
        } else {
            reporter1 = null;
        }

        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.MILLISECONDS)
                    .convertRatesTo(TimeUnit.SECONDS)
                    .prefixedWith(config.getStringProperty(CoreConfig.GRAPHITE_PREFIX))
                    .build(graphite);

{'fraction_non_alphanumeric': 0.09202705472432876, 'fraction_numerical': 0.0049190407870465256, 'mean_word_length': 3.1369224247562526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1696926', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2621, 'n_words': 506}