kafka采用http协议 report 出 influxline 格式的metrics测量数据

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值