自定义 exporter with java client

背景:

需要自定义 时间戳,目前查阅,只能通过 底层的操作才支持 带 自定义的时间戳。

前提:

下载安装 prometheus,见

1、新建springboot项目

<!-- The client -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient</artifactId>
            <version>0.16.0</version>
        </dependency>
        <!-- Hotspot JVM metrics-->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_hotspot</artifactId>
            <version>0.16.0</version>
        </dependency>
        <!-- Exposition HTTPServer-->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_httpserver</artifactId>
            <version>0.16.0</version>
        </dependency>

        <!-- Pushgateway exposition-->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_pushgateway</artifactId>
            <version>0.16.0</version>
        </dependency>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2、自定义 collecotr

public class GuageCollector2  extends Collector {

    private static final Logger LOGGER = LoggerFactory.getLogger(GuageCollector2.class);

        public static List<SendData> datas = new ArrayList<>();

        @Override
        public List<MetricFamilySamples> collect() {
          
          //这里就是 暴露指标

            if (CollectionUtils.isEmpty(datas)) {
                return new ArrayList<>();
            }

            LOGGER.info(">>>>>>>>> load data");
            List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
            List<String> labels = Arrays.asList("timestamp", "snmp_id", "index", "ip", "flow_type", "flow_id");
            List<MetricFamilySamples.Sample> samples = new ArrayList<>();
            for (SendData data : datas) {
                List<String> labelValues = Arrays.asList(data.getTimestamp() + "", data.getSnmpId() + "", data.getIndex() + "", data.getIp() + "" , data.getFlowType() + "", data.getFlowId() + "");
                samples.add(new MetricFamilySamples.Sample("test_bmc_bandwith_in1", labels, labelValues, data.getIn().doubleValue(), data.getTimestamp()));
                samples.add(new MetricFamilySamples.Sample("test_bmc_bandwith_out1", labels, labelValues, data.getOut().doubleValue(), data.getTimestamp()));
            }

            // With no labels.
            mfs.add(new MetricFamilySamples("test_bmc_bandwitdh",  Type.GAUGE, "help", samples));

            return mfs;
        }

}

3、定义/ metrics endpoint

 @GetMapping("/metrics")
    public void metricsTrue(HttpServletResponse response) throws IOException {

        LOGGER.info("<<<<<<< load metrics");
        response.setContentType("text/plain; version=0.0.4; charset=utf-8");
        List<String> metricsNames = new ArrayList<>();
        //实时的数据
        TextFormat.write004(response.getWriter(), ExportServer.INSTANCE.collect());
    }

4、配置 prometheus.yml

global:
  # 采集间隔
  scrape_interval:     15s # By default, scrape targets every 15 seconds.
  # 计算报警和预聚合 间隔
  evaluation_interval: 10s

  # 采集超时 时间
  scrape_timeout: 10s


scrape_configs:
  

  - job_name: "myexportor"
    # honor_labels: true
    # honor_timestamps: false
    static_configs:
      - targets: ['192.168.31.45:8082']
        labels:
          instance: exportor
  
posted @ 2022-07-22 19:03  小烽  阅读(385)  评论(0编辑  收藏  举报