<commit_before>package ar.com.threelegs.newrelic.cassandra;

import ar.com.threelegs.newrelic.cassandra.jmx.JMXHelper;
import ar.com.threelegs.newrelic.cassandra.jmx.JMXTemplate;
import com.newrelic.metrics.publish.Agent;
import com.typesafe.config.Config;

import javax.management.MBeanServerConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author juanformoso
 * @author germanklf
 */
public class CassandraRing extends Agent {

    private String name = "Default";
    private Config config;

    public CassandraRing(Config config) {
        super("ar.com.3legs.newrelic.cassandra", "0.0.1");
        this.name = config.getString("name");
        this.config = config;
    }

    @Override
    public String getComponentHumanLabel() {
        return name;
    }

    @Override
    public void pollCycle() {
        System.out.println("starting poll cycle");
        try {
            System.out.println("getting ring hosts from discovery_host " + config.getString("discovery_host"));
            List<String> ringHosts = getRingHosts();

            List<Metric> allMetrics = new ArrayList<Metric>();

            System.out.println("getting metrics for hosts [" + ringHosts + "]...");

            for (final String host : ringHosts) {
                System.out.println("getting metrics for host [" + host + "]...");

                try {
                    allMetrics.addAll(JMXHelper.run(host, config.getString("jmx_port"), new JMXTemplate<List<Metric>>() {
                        @Override
                        public List<Metric> execute(MBeanServerConnection connection) throws Exception {

                            ArrayList<Metric> metrics = new ArrayList<Metric>();

                            Double rsl = JMXHelper.queryAndGetAttribute(connection, "org.apache.cassandra.metrics", "Latency", "ClientRequest", "RangeSlice", "OneMinuteRate");
                            Double rl = JMXHelper.queryAndGetAttribute(connection, "org.apache.cassandra.metrics", "Latency", "ClientRequest", "Read", "OneMinuteRate");
                            Double wl = JMXHelper.queryAndGetAttribute(connection, "org.apache.cassandra.metrics", "Latency", "ClientRequest", "Write", "OneMinuteRate");

                            //System.out.println("test: " + JMXHelper.queryAndGetAttribute(connection, JMXHelper.getObjectNameByKeys("org.apache.cassandra.db", "type=ColumnFamilies", "keyspace=system", "columnfamily=peers"), "WriteCount"));

                            metrics.add(new Metric("Cassandra/hosts/" + host + "/Latency/Reads", "microseconds", rsl + rl));
                            metrics.add(new Metric("Cassandra/hosts/" + host + "/Latency/Writes", "microseconds", wl));

                            metrics.add(new Metric("Cassandra/global/Latency/Reads", "microseconds", rsl + rl));
                            metrics.add(new Metric("Cassandra/global/Latency/Writes", "microseconds", wl));

                            return metrics;
                        }
                    }));
                } catch (Exception e) {
                    System.out.println("exception processing host: " + host);
                    e.printStackTrace();
                }

            }

            System.out.println("pushing " + allMetrics.size() + " metrics...");<commit_msg>Added compaction and memtable flush metrics. Reads/Writes are now using "mu" as unit.<commit_after>package ar.com.threelegs.newrelic.cassandra;

import ar.com.threelegs.newrelic.cassandra.jmx.JMXHelper;
import ar.com.threelegs.newrelic.cassandra.jmx.JMXTemplate;
import com.newrelic.metrics.publish.Agent;
import com.typesafe.config.Config;

import javax.management.MBeanServerConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author juanformoso
 * @author germanklf
 */
public class CassandraRing extends Agent {

    private String name = "Default";
    private Config config;

    public CassandraRing(Config config) {
        super("ar.com.3legs.newrelic.cassandra", "0.0.1");
        this.name = config.getString("name");
        this.config = config;
    }

    @Override
    public String getComponentHumanLabel() {
        return name;
    }

    @Override
    public void pollCycle() {
        System.out.println("starting poll cycle");
        try {
            System.out.println("getting ring hosts from discovery_host " + config.getString("discovery_host"));
            List<String> ringHosts = getRingHosts();

            List<Metric> allMetrics = new ArrayList<Metric>();

            System.out.println("getting metrics for hosts [" + ringHosts + "]...");

            for (final String host : ringHosts) {
                System.out.println("getting metrics for host [" + host + "]...");

                try {
                    allMetrics.addAll(JMXHelper.run(host, config.getString("jmx_port"), new JMXTemplate<List<Metric>>() {
                        @Override
                        public List<Metric> execute(MBeanServerConnection connection) throws Exception {

                            ArrayList<Metric> metrics = new ArrayList<Metric>();

                            Double rsl = JMXHelper.queryAndGetAttribute(connection, "org.apache.cassandra.metrics", "Latency", "ClientRequest", "RangeSlice", "OneMinuteRate");
                            Double rl = JMXHelper.queryAndGetAttribute(connection, "org.apache.cassandra.metrics", "Latency", "ClientRequest", "Read", "OneMinuteRate");
                            Double wl = JMXHelper.queryAndGetAttribute(connection, "org.apache.cassandra.metrics", "Latency", "ClientRequest", "Write", "OneMinuteRate");

                            Integer cpt = JMXHelper.queryAndGetAttribute(connection, JMXHelper.getObjectNameByKeys("org.apache.cassandra.metrics", "type=Compaction", "name=PendingTasks"), "Value");
                            Integer mpt = JMXHelper.queryAndGetAttribute(connection, JMXHelper.getObjectNameByKeys("org.apache.cassandra.metrics", "path=internal", "scope=MemtablePostFlusher", "name=PendingTasks"), "Value");
                            //org.apache.cassandra.metrics:type=,path=,scope=,name=

                            //System.out.println("test: " + JMXHelper.queryAndGetAttribute(connection, JMXHelper.getObjectNameByKeys("org.apache.cassandra.db", "type=ColumnFamilies", "keyspace=system", "columnfamily=peers"), "WriteCount"));

                            metrics.add(new Metric("Cassandra/hosts/" + host + "/Compaction/PendingTasks", "count", cpt));
                            metrics.add(new Metric("Cassandra/hosts/" + host + "/MemtableFlush/PendingTasks", "count", mpt));

                            metrics.add(new Metric("Cassandra/hosts/" + host + "/Latency/Reads", "mu", rsl));
                            metrics.add(new Metric("Cassandra/hosts/" + host + "/Latency/Reads", "mu", rl));
                            metrics.add(new Metric("Cassandra/hosts/" + host + "/Latency/Writes", "mu", wl));

                            metrics.add(new Metric("Cassandra/global/Latency/Reads", "mu", rsl));
                            metrics.add(new Metric("Cassandra/global/Latency/Reads", "mu", rl));
                            metrics.add(new Metric("Cassandra/global/Latency/Writes", "mu", wl));

                            return metrics;
                        }
                    }));
                } catch (Exception e) {
                    System.out.println("exception processing host: " + host);
                    e.printStackTrace();
                }

            }

            System.out.println("pushing " + allMetrics.size() + " metrics...");
{'fraction_non_alphanumeric': 0.12904488517745302, 'fraction_numerical': 0.0010438413361169101, 'mean_word_length': 2.3038793103448274, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3017038', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2057, 'n_words': 381}