Prometheus采集Java程序指标信息
采集Java程序JVM信息
本文实例代码#
https://github.com/yoyofx/java-metric-prometheus
创建 Spring Boot Application 应用程序#
进行 https://start.spring.io 使用版本 Spring Boot v2.7.11和JDK 17,并创建一个具有以下依赖项的简单JAVA应用程序。
- Spring Boot Actuator (Ops)
- Prometheus (Observability)
- Spring Web (Optional: only to create a simple REST controller.)
Maven POM 会生成以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
接下来,我们需要公开一个执行器端点,Prometheus将通过该端点以Prometheus能够理解的格式收集指标数据。为此,我们需要添加以下属性。
management.endpoints.web.exposure.include=prometheus
接下来,让我们添加一个简单的控制器和一个简单的接口端点。
@RestController
@SpringBootApplication
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello World!";
}
}
现在,让我们启动应用程序并打开以下URL。
http://localhost:8080/actuator/prometheus
打开上述端点后,您将发现以下格式的一些指标数据,例如:
jvm_memory_used_bytes{area="heap",id="G1 Survivor Space",} 1005592.0
作者: YOYOFx
出处:https://www.cnblogs.com/maxzhang1985/p/12673160.html
版权:本文采用「署名-非商业性使用-相同方式共享 4.0 国际」知识共享许可协议进行许可。

标签:
Prometheus
, Spring Boot
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性