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
复制代码

 

二、示例代码

代码运行起来后,可以通过http://localhost:9091,确认job是否执行成功

 

三、配置grafana图表

写入成功后,grafana里就能识别出这2个指标了:

 

参考文章:

https://github.com/prometheus/client_java

posted @   菩提树下的杨过  阅读(6855)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
历史上的今天:
2015-09-20 rpc框架之 thrift 学习 2 - 基本概念
2015-09-20 rpc框架之 thrift 学习 1 - 安装 及 hello world
2008-09-20 用JS + WCF打造轻量级WebPart
点击右上角即可分享
微信分享提示