Kafka 监控实现机制学习

996 阅读3分钟

Kafka 监控实现机制学习

这篇文章概述了 kafka 的 Metrics 模块架构:blog.csdn.net/u010952362/…,建议看完本篇后食用。

监控实现思路

初始化 Metrics

第一步:在 kafkaServer 的 startup()方法中,metrics 第一次被初始化:

metrics = new Metrics(metricConfig, reporters, kafkaMetricsTime, true)

之后,这个 metircs 实例被传到 quotaManagers 的构造函数中。

quotaManagers = QuotaFactory.instantiate(config, metrics, time)

quotaManagers,这是 kafka 中用来限制 kafka.producer 的传输速度的,比如在 config 文件下配置 producer 不能以超过 5MB/s 的速度传输速度,name这个限制就是通过 quotaManager 来实现的。

Metrics 及其下的 Metric

第二步:我们向下查看 Metrics 类的源码

public class Metrics implements Closeable {
 ....
 ....
    private final ConcurrentMap<MetricName, KafkaMetric> metrics;
    private final ConcurrentMap<String, Sensor> sensors;

可以发现 metrics 与 sensors 这两个 concurrentMap 是 Metrics 中两个重要的成员属性,name我们再向下看看,什么事 KafkaMetric 以及 Sensor。

首先分析 KafkaMetric

KafkaMetric 实现了 Metric 接口,我们先来看看 Metric 接口,可以看到它的核心方法 value() 返回要监控的参数的值.

public interface Metric {
​
    /**
     * A name for this metric
     */
    public MetricName metricName();
​
    /**
     * The value of the metric
     */
    public double value();
​
}

接着我们来看看 KafkaMetric,它实现了 Metric 接口:

@Override
public double value() {
    synchronized (this.lock) {
        return value(time.milliseconds());
    }
}
​
double value(long timeMs) {
    return this.measurable.measure(config, timeMs);
}

这个 value()方法最终调用了 this.measurable.measure(config, timeMs) 方法,也就是 value() 是通过 kafkaMetric 中的另一个成员属性 measurable 完成。

Measurable 接口源码

public interface Measurable {
​
    /**
     * Measure this quantity and return the result as a double
     * @param config The configuration for this metric
     * @param now The POSIX time in milliseconds the measurement is being taken
     * @return The measured value
     */
    public double measure(MetricConfig config, long now);
​
}

总结一下 Metrics:

Metrics -> Metric -> Measurable

用形象的例子来举例子说明,Metrics 是汽车的仪表盘,而 Metric 是仪表盘中的一个仪表,而 Measurable 则是对真正要检测的组件的一个封装。

Sensor

接下来介绍一下 Sensor,即 Metrics 类下的 ConcurrentMap<String, Sensor> sensors;

以下是 Sensor 类的源码:

/**
 * A sensor applies a continuous sequence of numerical values to a set of associated metrics. 
 * For example a sensor on message size would record a sequence of message sizes using the 
 * {@link #record(double)} api and would maintain a set
 * of metrics about request sizes such as the average or max.
 * 翻译:传感器将连续的数值序列应用于一组相关的度量。
 * 例如,消息大小传感器将使用记录应用程序接口记录一系列消息大小,并维护一组关于请求大小的度量,
 * 例如平均值或最大值。
 */
public final class Sensor {
    //一个kafka就只有一个Metrics实例,这个registry就是对这个Metrics的引用
    private final Metrics registry;
    private final String name;
    private final Sensor[] parents;
    private final List<Stat> stats;
    private final List<KafkaMetric> metrics;

对 Sensor 的解释:从注释中可以看到 Sensor 的作用不同于 KafkaMetric,KafkaMetric 仅仅是返回某一个参数的值,而 Sensor 拥有对某一参数时间序列进行统计的功能,比如平均值、最小值,那这些统计又是如何实现的呢?答案是 List stats 这个成员属性。

Stat

那我们来看看 Stat 的源码:

public interface Stat {
​
    /**
     * Record the given value
     * @param config The configuration to use for this metric
     * @param value The value to record
     * @param timeMs The POSIX time in milliseconds this value occurred
     */
    public void record(MetricConfig config, double value, long timeMs);
​
}

可以看到 Stat 是一个接口,其中有一个 record 方法可以记录一个采样数值,下面看一个例子,max 这个功能如何用 Stat 来实现?

public final class Max extends SampledStat {
​
    public Max() {
        super(Double.NEGATIVE_INFINITY);
    }
​
    @Override
    protected void update(Sample sample, MetricConfig config, double value, long now) {
        sample.value = Math.max(sample.value, value);
    }
​
    @Override
    public double combine(List<Sample> samples, MetricConfig config, long now) {
        double max = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < samples.size(); i++)
            max = Math.max(max, samples.get(i).value);
        return max;
    }
​
}

首先 SampledStat 是 Stat 接口的一个实现类,而 Max 继承了 SampledStat,我们看看具体的方法,update 的作用就是将当前值与历史最大值作比较,更新最大值(冒泡),而 combine 方法相当于用一次完整的冒泡排序找出最大值。

接着我们再次回到 Sensor 类,查看其 record 源码:

public void record(double value, long timeMs) {
    this.lastRecordTime = timeMs;
    synchronized (this) {
        // increment all the stats
        for (int i = 0; i < this.stats.size(); i++)
            this.stats.get(i).record(config, value, timeMs);
        checkQuotas(timeMs);
    }
    for (int i = 0; i < parents.length; i++)
        parents[i].record(value, timeMs);
}

record方法,每个注册于其中的stats提交值,同时如果自己有父sensor的话,向父sensor提交. 这里 checkQuotas 方法是 kafka 用来检查注册在 sensor 上的每一个 KafkaMetric 的值是否超过了 config 文件中设置的配额。

总结

  • Metrics 仪表盘

    • Metric 仪表盘中的其中一个仪表

      • Measurable 仪表数据的来源--测量
    • Sensor 提供统计功能

      • Stat 统计功能的具体实现者

参考文献

blog.csdn.net/qq_21519863…