kafka 自带测量数据,为进行监控,将其测量数据采集并report出去,由于团队内部使用时序数据库 influxdb 作为测量数据库,所有吐出格式为 influxline,
样例代码如下,以飨读者
KafkaInfluxMetricsReporter
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.bestv.bdp.influxmetrics;
import com.yammer.metrics.Metrics;
import kafka.metrics.KafkaMetricsConfig;
import kafka.metrics.KafkaMetricsReporter;
import kafka.utils.VerifiableProperties;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
public class KafkaInfluxMetricsReporter implements KafkaMetricsReporter, KafkaInfluxMetricsReporterMBean {
private static final Logger LOG = LoggerFactory.getLogger(KafkaInfluxMetricsReporter.class);
private static final String DEFAULT_NAME = "influxdb-reporter";
private boolean initialized = false;
private boolean running = false;
private InfluxReporter reporter;
@Override
public void init(VerifiableProperties props) {
if(!initialized) {
KafkaMetricsConfig metricsConfig = new KafkaMetricsConfig(props);
InfluxDBMetricsConfig config = new InfluxDBMetricsConfig(props);
config.addTag("brokerId", props.getString("broker.id"));
//设置http客户端
LOG.info("config.getConnectString():" + config.getConnectString());
InfluxDB influxDBclient = InfluxDBFactory.connect(config.getConnectString());
this.reporter = new InfluxReporter(Metrics.defaultRegistry(), DEFAULT_NAME
,influxDBclient, new MetricsPredicate(config.getPredicates()),config);
if (props.getBoolean(InfluxDBMetricsConfig.KAFKA_INFLUX_METRICS_ENABLE, false)) {
initialized = true;
startReporter(metricsConfig.pollingIntervalSecs());
LOG.info("KafkaInfluxMetricsReporter initialized.");
}
}
}
@Override
public void startReporter(long pollingPeriodInSeconds) {
if (initialized && !running) {
reporter.start(pollingPeriodInSeconds, TimeUnit.SECONDS);
running = true;
LOG.info(String.format("Started KafkaInfluxMetricsReporter with polling period %d seconds", pollingPeriodInSeconds));
}
}
@Override
public void stopReporter() {
}
@Override
public String getMBeanName() {
return "kafka:type=" + KafkaInfluxMetricsReporter.class.getName();
}
}
pom.xml 文件,需添加依赖
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.5</version>
</dependency>
所使用的的 InfluxReporter ,参阅 《实现Flume多维度metrics测量的解决方案》: https://blog.csdn.net/zhixingheyi_tian/article/details/80455443