All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.amient.kafka.metrics.GroupMetrics Maven / Gradle / Ivy

package io.amient.kafka.metrics;

import com.yammer.metrics.core.Gauge;
import com.yammer.metrics.core.Metric;
import com.yammer.metrics.core.MetricName;
import com.yammer.metrics.core.MetricsRegistry;
import org.apache.kafka.common.TopicPartition;

import java.util.HashMap;
import java.util.Map;

public class GroupMetrics {

    private final Class cls;
    private final MetricsRegistry registry;
    private final String name;
    private final Map> data = new HashMap<>();

    public GroupMetrics(String metricName, Class cls, MetricsRegistry registry) {
        this.registry = registry;
        this.name = metricName;
        this.cls = cls;
    }

    public T get(String group, TopicPartition tp) {
        Map metrics = data.get(group);
        if (metrics == null) {
            metrics = new HashMap<>();
            data.put(group, metrics);
        }
        T metric = metrics.get(tp);
        if (metric == null) {
            try {
                metric = cls.newInstance();
                if (metric instanceof Gauge) {
                    registry.newGauge(NewName(group, tp), (Gauge)metric);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            metrics.put(tp, metric);
        }
        return metric;
    }

    private MetricName NewName(String group, TopicPartition tp) {
        return new MetricName(
                "kafka.groups",
                "Group",
                name,
                "",
                "kafka.consumer:type=Group,name=" + name
                        + ",group=" + group
                        + ",topic=" + tp.topic()
                        + ",partition=" + tp.partition());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy