为了账号安全,请及时绑定邮箱和手机立即绑定

如何从 SpringBoot 应用程序发出自定义指标并在 PCF Autoscaler 中使用它

如何从 SpringBoot 应用程序发出自定义指标并在 PCF Autoscaler 中使用它

青春有我 2023-03-31 15:36:20
我按照这个示例发出自定义指标,并按照这些详细信息在 PCF 中注册指标。这是代码:@RestControllerpublic class CustomMetricsController {    @Autowired    private MeterRegistry registry;         @GetMapping("/high_latency")    public ResponseEntity<Integer> highLatency() throws InterruptedException {        int queueLength=0;        Random random = new Random();        int number = random.nextInt(50);        System.out.println("Generate number is : "+number);        if(number % 2 == 0) {            queueLength=99;        } else {            queueLength=200;        }        return new ResponseEntity<>(queueLength, null, HttpStatus.OK);    }}应用程序.yml:management:  endpoints:    web:      exposure:        include: "metrics,prometheus"  endpoint:    metrics:      enabled: true    prometheus:      enabled: true安全配置类:build.gradle 依赖部分:在将应用程序部署到 PCF 后,我按照以下步骤注册自定义指标:安装metric-registrar在我的本地机器上发出一些整数的注册端点(我将其用于自动缩放器规则) cf register-metrics-endpoint api high_latency 在此步骤之后,我可以看到一个自定义用户提供的服务与我在 PCF 中的应用程序绑定在我的本地安装log-cache插件以验证指标端点,这是日志最后,我在 Autoscaler 中为自定义指标添加了规则。这是我在 Autoscaler 事件历史记录中遇到的错误。事件历史最近:2019 年 7 月 17 日,星期三,上午 11:20,Autoscaler 在扩展窗口期间没有收到任何 high_latency 指标。在这些指标可用之前,将推迟缩减规模。
查看完整描述

1 回答

?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

我看着这个很大。这里有几件事...


我相信您的注册命令是错误的,至少对于您引用的示例应用程序而言是这样。


您正在使用cf register-metrics-endpoint api high_latency,这意味着您有一个名为的应用程序api和该应用程序上的一个端点,high_latency该端点使用 Prometheus 格式导出指标。对于这个示例应用程序,路径应该是/actuator/prometheus,根据自述文件和我的简短测试。


当我使用命令时cf register-metrics-endpoint app-name /actuator/prometheus,我能够在输出中看到自定义指标cf tail,但在包含的输出中看不到它们。


例如:(未显示在您的屏幕截图中)


2019-07-17T22:54:37.83-0400 [custom-metrics-demo/0] GAUGE tomcat_global_request_max_seconds:2.006000

2019-07-17T22:54:37.83-0400 [custom-metrics-demo/0] GAUGE system_cpu_count:4.000000

2019-07-17T22:54:37.83-0400 [custom-metrics-demo/0] COUNTER tomcat_sessions_created_sessions_total:12

2019-07-17T22:54:37.83-0400 [custom-metrics-demo/0] COUNTER custom_metric_total:15

2019-07-17T22:54:37.83-0400 [custom-metrics-demo/0] GAUGE jvm_gc_pause_seconds_sum:0.138000

2019-07-17T22:54:37.83-0400 [custom-metrics-demo/0] COUNTER jvm_gc_pause_seconds_count:25

在引用的示例应用程序中,没有名为 的指标high_latency,因此它不能用作您的指标名称,因为它永远不会由应用程序生成。/actuator/prometheus如果您在浏览器中访问端点,则可以看到所有指标。Prometheus 格式是基于文本的,非常容易阅读。


最后一个很棘手,没有记录,但我可以在 Autoscaler 的代码中看到它(在撰写本文时)。当 Autoscaler 从 LogCache 轮询指标时,它只提取 GAUGE 和 TIMER 事件,而不是 COUNTER 事件。如果您尝试使用custom_metric演示应用程序,那将无法正常工作,因为它是一个 COUNTER 指标。您只会看到有关 Autoscaler 在缩放窗口期间看不到任何指标事件的消息。确保您选择的是用于缩放的 GAUGE 指标。


Autoscaler 似乎也不支持使用带有关联标签的指标。例如,如果您想使用tomcat_servlet_request_seconds_sum{name="dispatcherServlet",},我认为没有办法告诉 Autoscaler 标签name必须是某个值。该信息在 LogCache 中,但我认为 Autoscaler 目前不会使用它。在我写这篇文章的时候,我快速浏览了一下代码,似乎表明它只是在查看指标名称。如果您要创建自定义指标,这无关紧要。只是不要为指标使用任何标签。


查看完整回答
反对 回复 2023-03-31
  • 1 回答
  • 0 关注
  • 77 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信