如何从SpringBoot应用程序发出自定义指标并在PCF Autoscaler中使用它 [英] How to emit custom metrics from SpringBoot application and use it in PCF Autoscaler

查看:230
本文介绍了如何从SpringBoot应用程序发出自定义指标并在PCF Autoscaler中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注以下示例发出自定义指标,并按照这些详细信息用于在PCF中注册指标.

I am following this example for emitting the custom metrics and followed these details for registering the metrics in 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:

application.yml:

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

SecurityConfiguration类:

SecurityConfiguration class:

build.gradle依赖项部分:

build.gradle dependencies part:

在将应用程序部署到PCF之后,我按照以下步骤注册自定义指标:

Steps I followed to register the custom metrics after app deployment to PCF:

  1. 在我的本地计算机上安装了metric-registrar
  2. 发出一些整数的注册端点(我正在将其用于自动缩放规则) cf register-metrics-endpoint api high_latency 完成此步骤后,我可以看到PC上我的应用程序绑定了一个自定义用户提供"服务.
  3. 在我的本地计算机上安装了log-cache插件以验证指标端点,这是日志
  4. 最后,我在Autoscaler中为自定义指标添加了规则.
  1. Installed metric-registrar on my local machine
  2. Registered endpoint that emitting some Integer(which I am using for Autoscaler Rule) cf register-metrics-endpoint api high_latency After this step I can see one Custom-User-Provided service is bounded with my app in PCF.
  3. Installed log-cache plugin on my local to verify the metrics endpoint and here are the logs
  4. At last I added the rule in Autoscaler for custom metric.

这是我在Autoscaler事件历史记录中遇到的错误.

This is the error I am getting in Autoscaler event history.

事件历史记录最近一次:2019年7月17日,星期三,上​​午11:20 Autoscaler 缩放期间未收到有关high_latency的任何指标 窗户.缩小比例将推迟到这些指标达到 可用.

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.

推荐答案

我对此做了很大的研究.这里有几件事...

I looked into this a big. A few things here...

  1. 我认为您的注册命令至少在您引用的示例应用程序中是错误的.

  1. I believe your registration command is wrong, at least for the sample app you referenced.

您正在使用cf register-metrics-endpoint api high_latency,这意味着您有一个名为api的应用程序,并且该应用程序上的一个端点位于high_latency,该端点使用Prometheus格式导出​​指标.对于此示例应用程序,根据README&的路径应为/actuator/prometheus.我的简短测试.

You're using cf register-metrics-endpoint api high_latency, which means you have an app named api and an endpoint on that app at high_latency which exports metrics using the Prometheus format. For this sample app, the path should be /actuator/prometheus, according to the README & my brief test.

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

When I used the command cf register-metrics-endpoint app-name /actuator/prometheus, I was able to see the custom metrics in the output from cf tail & I do not see them in your included output.

Ex :(未显示在屏幕截图中)

Ex: (not showing up in your screenshot)

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格式基于文本,非常易于阅读.

  • In the referenced sample app, there is no metric called high_latency, so that won't work as the name of your metric, as it will never get generated by the app. You can see all the metrics if you access the /actuator/prometheus endpoint in your browser. The Prometheus format is text based and pretty easy to read.

    最后一个是棘手的,没有记录在案,但我可以在Autoscaler的代码中看到它(在撰写本文时).当Autoscaler从LogCache轮询指标时,只会拉出GAUGE& TIMER事件,而不是COUNTER事件.如果您尝试从演示应用程序中使用custom_metric,则该操作将不起作用,因为它是COUNTER指标.您将仅看到有关在缩放窗口期间Autoscaler没有看到任何度量标准事件的消息.确保您正在选择用于缩放的GAUGE指标.

    The last one is tricky and not documented but I can see it in the code for Autoscaler (at the time of writing this). When Autoscaler polls for metrics from LogCache, it only pulls GAUGE & TIMER events, not COUNTER events. If you were to try and use custom_metric from the demo app that would not work because it is a COUNTER metric. You'll just see the message about Autoscaler not seeing any metric events during the scaling window. Make sure that you are picking a GAUGE metric to use for scaling.

    Autoscaler也似乎不支持将指标与关联的标签一起使用.例如,如果您想使用tomcat_servlet_request_seconds_sum{name="dispatcherServlet",},我认为没有办法告诉Autoscaler必须将name标记设为某个值.该信息位于LogCache中,但是我不认为Autoscaler目前正在使用它.在撰写本文时,我快速浏览了一下代码,似乎表明它只是在查看指标名称.如果您要创建自定义指标,则没关系.只是不要为该指标使用任何标签.

    It also does not appear that Autoscaler supports using metrics with associated tags. If for example, you wanted to use tomcat_servlet_request_seconds_sum{name="dispatcherServlet",}, I don't think there's a way to tell Autoscaler that the name tag is required to be a certain value. That info is in LogCache, but I don't think Autoscaler uses it at this time. My quick glance through the code, at the time I write this, seems to indicate it's only looking at the metric name. If you're creating a custom metric, this won't matter. Just don't use any tags for the metric.

    希望有帮助!

    这篇关于如何从SpringBoot应用程序发出自定义指标并在PCF Autoscaler中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆