前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud Sleuth与Prometheus集成-示例

Spring Cloud Sleuth与Prometheus集成-示例

原创
作者头像
堕落飞鸟
发布2023-04-12 07:37:01
6310
发布2023-04-12 07:37:01
举报
文章被收录于专栏:飞鸟的专栏

现在,假设我们有一个简单的Spring Boot应用程序,其中有一个REST端点返回Hello World消息。我们将添加Sleuth跟踪和Prometheus指标,以记录应用性能并导出到Prometheus。

第一步:添加依赖项

在pom.xml中添加以下依赖项:

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

第二步:添加Prometheus配置

在application.yml中添加以下配置:

代码语言:javascript
复制
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    prometheus:
      enabled: true
  metrics:
    tags:
      application: ${spring.application.name}
  export:
    prometheus:
      enabled: true

第三步:启用Sleuth跟踪

在主类上添加@EnableSleuth注释:

代码语言:javascript
复制
@SpringBootApplication
@EnableSleuth
public class SleuthPrometheusDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SleuthPrometheusDemoApplication.class, args);
    }

}

第四步:添加指标注释

在HelloController类中添加@Timed注释:

代码语言:javascript
复制
@RestController
public class HelloController {

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

    @GetMapping("/")
    @Timed(value = "hello.request.time", description = "Time taken to return Hello World message")
    public String hello() {
        LOGGER.info("Saying hello...");
        return "Hello World!";
    }
}

上面的@Timed注释将记录请求处理时间,并将其导出为名为hello.request.time的Prometheus指标。

第五步:启动应用程序

在启动应用程序之后,可以使用以下URL访问Prometheus端点:

http://localhost:8080/actuator/prometheus

Prometheus端点将返回当前应用程序的所有指标数据。可以使用Prometheus查询语言(PromQL)来查询和可视化指标数据。例如,要查看hello.request.time指标的平均值:

代码语言:javascript
复制
avg(hello_request_time)

可以使用Prometheus的Grafana插件将指标数据可视化。在Grafana中创建一个新的仪表板,并添加一个新的“Prometheus”数据源,然后使用PromQL查询语言构建图表和仪表板面板。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第二步:添加Prometheus配置
  • 第三步:启用Sleuth跟踪
  • 第四步:添加指标注释
  • 第五步:启动应用程序
相关产品与服务
Prometheus 监控服务
Prometheus 监控服务(TencentCloud Managed Service for Prometheus,TMP)是基于开源 Prometheus 构建的高可用、全托管的服务,与腾讯云容器服务(TKE)高度集成,兼容开源生态丰富多样的应用组件,结合腾讯云可观测平台-告警管理和 Prometheus Alertmanager 能力,为您提供免搭建的高效运维能力,减少开发及运维成本。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档