gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:40:52 25 4
gpt4 key购买 nike

我正在关注这个example用于发出自定义指标并遵循 these details用于在 PCF 中注册指标。

这是代码:

@RestController
public 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);
}
}

application.yml:

management:
endpoints:
web:
exposure:
include: "metrics,prometheus"
endpoint:
metrics:
enabled: true
prometheus:
enabled: true

安全配置类: enter image description here

build.gradle依赖部分: enter image description here

将应用程序部署到 PCF 后注册自定义指标所遵循的步骤:

  1. 在我的本地计算机上安装了 metric-registrar
  2. 发出一些整数的注册端点(我将其用于自动缩放器规则)cf register-metrics-endpoint api high_latency在此步骤之后,我可以看到一项自定义用户提供的服务与 PCF 中的我的应用程序绑定(bind)。 enter image description here
  3. 在本地安装了 log-cache 插件来验证指标端点,以下是日志 enter image description here
  4. 最后,我在 Autoscaler 中添加了自定义指标的规则。 enter image description here

这是我在自动缩放器事件历史记录中遇到的错误。

EVENT HISTORY Most Recent: Wed July 17, 2019 at 11:20 AM Autoscaler did not receive any metrics for high_latency during the scaling window. Scaling down will be deferred until these metrics are available.

最佳答案

我对此进行了很大的研究。这里有一些事情...

  1. 我认为您的注册命令是错误的,至少对于您引用的示例应用程序是错误的。

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

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

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

希望有帮助!

关于java - 如何从 SpringBoot 应用程序发出自定义指标并在 PCF Autoscaler 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57079356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com