prometheus學習筆記(2)-利用java client寫入數據


繼續學習prometheus,上一節演示了用http方式使用curl向pushgateway發送數據,本節將研究如何利用client jar包,以java代碼的方式寫入數據。

一、依賴的jar包

 1 <dependency>
 2     <groupId>io.prometheus</groupId>
 3     <artifactId>simpleclient</artifactId>
 4     <version>0.9.0</version>
 5 </dependency>
 6 
 7 <dependency>
 8     <groupId>io.prometheus</groupId>
 9     <artifactId>simpleclient_pushgateway</artifactId>
10     <version>0.9.0</version>
11 </dependency>
View Code

主要就是上面2個(這是最小配置),考慮到我們通常是在spring環境中使用,一般還要加1個spring依賴,完整pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cnblogs.yjmyzz</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- spring應用最小依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>

        <!-- The client -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient</artifactId>
            <version>0.9.0</version>
        </dependency>

        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_pushgateway</artifactId>
            <version>0.9.0</version>
        </dependency>

        <!--        下面2個也常用,但在本例中用不到-->
        <!--        <dependency>-->
        <!--            <groupId>io.prometheus</groupId>-->
        <!--            <artifactId>simpleclient_hotspot</artifactId>-->
        <!--            <version>0.9.0</version>-->
        <!--        </dependency>-->
        <!--        <dependency>-->
        <!--            <groupId>io.prometheus</groupId>-->
        <!--            <artifactId>simpleclient_httpserver</artifactId>-->
        <!--            <version>0.9.0</version>-->
        <!--        </dependency>-->

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>
View Code

 

二、示例代碼

package com.cnblogs.yjmyzz.springbootdemo;

import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.PushGateway;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import java.io.IOException;
import java.util.Random;


/**
 * @author 菩提樹下的楊過(http : / / yjmyzz.cnblogs.com)
 * 利用client寫入prometheus示例
 */
@ComponentScan("com.cnblogs.yjmyzz")
public class SampleApplication {

    /**
     * push網關
     *
     * @return
     */
    @Bean
    public PushGateway getPushGateway() {
        return new PushGateway("localhost:9091");
    }

    /**
     * counter實例
     *
     * @return
     */
    @Bean
    public Counter getCounter() {
        return Counter.build()
                .name("blog_visit") //這里模擬博客訪問量
                .labelNames("blog_id") //博客id
                .help("counter_blog_visit") //這個名字隨便起
                .register(); //注:通常只能注冊1次,1個實例中重復注冊會報錯
    }

    @Bean
    public Gauge getGauge() {
        return Gauge.build()
                .name("blog_fans") //這里模擬粉絲數(注:這里我們沒設置label)
                .help("gauge_blog_fans")
                .register();
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SampleApplication.class);

        //從spring上下文中取出這些實例
        Counter counter = context.getBean(Counter.class);
        Gauge gauge = context.getBean(Gauge.class);
        PushGateway gateway = context.getBean(PushGateway.class);
        Random rnd = new Random();

        //粉絲數先預設50
        gauge.inc(50);
        while (true) {
            //隨機生成1個blogId
            int blogId = rnd.nextInt(100000);
            //該blogId的訪問量+1
            counter.labels(blogId + "").inc();
            //模擬粉絲數的變化
            if (blogId % 2 == 0) {
                gauge.inc();
            } else {
                gauge.dec();
            }
            //利用網關采集數據
            gateway.push(counter, "job-counter-test");
            gateway.push(gauge, "job-gauge-test");

            //輔助輸出日志
            System.out.println("blogId:" + blogId);
            Thread.sleep(5000);
        }
    }
}

代碼運行起來后,可以通過http://localhost:9091,確認job是否執行成功

 

三、配置grafana圖表

寫入成功后,grafana里就能識別出這2個指標了:

 

參考文章:

https://github.com/prometheus/client_java


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM