在 Spring Boot Camel 应用程序公开的 Micrometer / Prometheus 信息中包含其他 JMX 指标

Posted

技术标签:

【中文标题】在 Spring Boot Camel 应用程序公开的 Micrometer / Prometheus 信息中包含其他 JMX 指标【英文标题】:Include additional JMX metrics in Micrometer / Prometheus info exposed by a Spring Boot Camel application 【发布时间】:2021-07-22 17:43:29 【问题描述】:

我已经使用 Camel 3.9 配置了 Spring Boot 2 应用程序,以使用 Micrometer 通过 actuator/prometheus 端点公开指标,正确返回一些 Camel 指标:

# HELP CamelExchangesFailed_total  
# TYPE CamelExchangesFailed_total counter
CamelExchangesFailed_totalcamelContext="camel-1",routeId="route3",serviceName="MicrometerRoutePolicyService", 0.0
CamelExchangesFailed_totalcamelContext="camel-1",routeId="route2",serviceName="MicrometerRoutePolicyService", 0.0
CamelExchangesFailed_totalcamelContext="camel-1",routeId="route1",serviceName="MicrometerRoutePolicyService", 0.0
# HELP CamelExchangesTotal_total  
# TYPE CamelExchangesTotal_total counter
CamelExchangesTotal_totalcamelContext="camel-1",routeId="route3",serviceName="MicrometerRoutePolicyService", 0.0
CamelExchangesTotal_totalcamelContext="camel-1",routeId="route2",serviceName="MicrometerRoutePolicyService", 0.0
CamelExchangesTotal_totalcamelContext="camel-1",routeId="route1",serviceName="MicrometerRoutePolicyService", 0.0
# HELP CamelExchangesExternalRedeliveries_total  
# TYPE CamelExchangesExternalRedeliveries_total counter
CamelExchangesExternalRedeliveries_totalcamelContext="camel-1",routeId="route3",serviceName="MicrometerRoutePolicyService", 0.0
CamelExchangesExternalRedeliveries_totalcamelContext="camel-1",routeId="route2",serviceName="MicrometerRoutePolicyService", 0.0
CamelExchangesExternalRedeliveries_totalcamelContext="camel-1",routeId="route1",serviceName="MicrometerRoutePolicyService", 0.0
# HELP CamelExchangesSucceeded_total  
# TYPE CamelExchangesSucceeded_total counter
CamelExchangesSucceeded_totalcamelContext="camel-1",routeId="route3",serviceName="MicrometerRoutePolicyService", 0.0
CamelExchangesSucceeded_totalcamelContext="camel-1",routeId="route2",serviceName="MicrometerRoutePolicyService", 0.0
CamelExchangesSucceeded_totalcamelContext="camel-1",routeId="route1",serviceName="MicrometerRoutePolicyService", 0.0
# HELP CamelExchangesFailuresHandled_total  
# TYPE CamelExchangesFailuresHandled_total counter
CamelExchangesFailuresHandled_totalcamelContext="camel-1",routeId="route3",serviceName="MicrometerRoutePolicyService", 0.0
CamelExchangesFailuresHandled_totalcamelContext="camel-1",routeId="route2",serviceName="MicrometerRoutePolicyService", 0.0
CamelExchangesFailuresHandled_totalcamelContext="camel-1",routeId="route1",serviceName="MicrometerRoutePolicyService", 0.0

探索 mbean,我可以看到我也感兴趣的其他一些指标(例如路由平均处理时间)

问题是,我怎样才能让 Spring Boot Actuator + Micrometer + Prometheus 包含这些额外的指标?

following article 描述了一种使用 JMX 代理通过 config.yml 文件在 Prometheus 上发布的方法:

src/main/resources/config.yml

rules:
  - pattern: 'fis.metrics<name=os.(.*)><>(.+):'
    name: os_$1
    help: some help

  - pattern: 'org.apache.camel<context=camel, type=routes, name=\"(.*)\"><>LastProcessingTime'
    name: camel_last_processing_time
    help: Last Processing Time [milliseconds]
    type: GAUGE
    labels:
      route: $1

但是,我无法使用 Spring Boot Actuator + Micrometer/Prometheus 的当前基础架构找到一种方法。

这是我的配置:

依赖关系:

plugins 
    id "org.springframework.boot" version "2.4.4"
    id "com.github.lkishalmi.gatling" version "3.3.4"


apply plugin: 'eclipse'
apply plugin: 'com.github.lkishalmi.gatling'

description = """sle-sync"""

ext 
    springCloudVersion = '2020.0.2'
    orikaVersion = '1.5.2'
    junitVersion = '5.2.0'
    junitPlatformVersion = '1.2.0'
    camelVersion = '3.9.0'


repositories 
    mavenLocal()


dependencyManagement 
    imports 
        mavenBom "org.apache.camel.springboot:camel-spring-boot-bom:$camelVersion"
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:$springCloudVersion"
    


dependencies 
    implementation 'org.apache.camel.springboot:camel-spring-boot-starter',
            'org.apache.camel.springboot:camel-servlet-starter',
            'org.apache.camel.springboot:camel-http-starter',
            'org.apache.camel.springboot:camel-metrics-starter',
            "org.apache.camel.springboot:camel-micrometer-starter",
            'com.playtika.sleuth:sleuth-camel-core:2.1.0',
            "org.springframework.boot:spring-boot-starter-web",
            "org.springframework.boot:spring-boot-starter-actuator",
            "org.springframework.cloud:spring-cloud-starter-config",
            "org.springframework.cloud:spring-cloud-starter-kubernetes-fabric8-config",
            "org.springframework.cloud:spring-cloud-starter-sleuth",
            "io.micrometer:micrometer-registry-prometheus",
            "ma.glasnost.orika:orika-core:$orikaVersion",
            'org.projectlombok:lombok',
            "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2",
            "org.glassfish.jaxb:jaxb-runtime:2.3.2",
            'org.apache.camel:camel-management'
    testCompile 'org.apache.camel:camel-test-spring',
            "org.springframework.boot:spring-boot-starter-test",
            'com.github.sbrannen:spring-test-junit5:1.0.2',
            "org.junit.jupiter:junit-jupiter-api:$junitVersion"
    testRuntimeOnly "org.junit.platform:junit-platform-launcher:$junitPlatformVersion",
            "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
    annotationProcessor "org.projectlombok:lombok:1.18.10"
    testAnnotationProcessor "org.projectlombok:lombok:1.18.10"


CamelContext配置:


    @Bean
    public CamelContextConfiguration camelContextConfiguration() 

        return new CamelContextConfiguration() 
            @Override
            public void beforeApplicationStart(CamelContext camelContext) 
                camelContext.addRoutePolicyFactory(new MicrometerRoutePolicyFactory());
                camelContext.setMessageHistoryFactory(new MicrometerMessageHistoryFactory());
            

            @Override
            public void afterApplicationStart(CamelContext camelContext) 

            
        ;
    

application.yml

camel:
  component:
    servlet:
      mapping:
        context-path: /api/*
    metrics:

      metric-registry: prometheusMeterRegistry


management:
  endpoints:
    web:
      exposure:
        include: info, health, prometheus

【问题讨论】:

【参考方案1】:

如果有可以启用的功能,请先检查骆驼指标配置。如果 Camel 创建分布或计时器,您可以在千分尺侧启用报告百分位数。使用 Spring Boot,检查 management.metrics 属性,例如:

management.metrics.distribution.percentiles.all=0.95, 0.98, 0.99, 0.999, 0.9999, 0.99999
management.metrics.distribution.percentiles-histogram.all=true

Prometheus JMX Exporter 可能是解决这个问题的快速而肮脏的解决方案,但我不确定我是否会全心全意地推荐它:

JVM 代理使事情变得复杂,因为您需要将它添加到您的部署解决方案中 JMX 是分层的,Prometheus 是多维的,所以你需要编写和维护正则表达式来进行转换(去过那里,这不是一个很愉快的练习) 如果您想使用其他后端,Prometheus JMX Exporter 将无法使用它们

作为替代方案,您可以编写自己的MeterBinder。它是一个可以将Meters 注册到您的注册表的组件(正是您所需要的)。 Micrometer 有几个正在使用 JMX 的数据注册Meters,例如:KafkaConsumerMetrics、CommonsObjectPool2Metrics,你可以看看。

此外,由于 Camel 工具由 Camel 提供,您可以为 camel-metricscamel-micrometer 打开问题以添加缺少的仪表。如果您为他们创建问题 (https://github.com/jonatan-ivanov),请随时抄送我。

【讨论】:

以上是关于在 Spring Boot Camel 应用程序公开的 Micrometer / Prometheus 信息中包含其他 JMX 指标的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot + Apache Camel + Freemarker 自定义模板加载器

在 Spring Boot 应用程序中,IBM MQ 的集成工具(Apache camel 或 Spring 集成)容易或更好[关闭]

Camel:使用 spring-boot 配置的数据源

Camel ActiveMQ + Spring boot 不读取 spring activemq 配置

Apache Camel 与 Spring Boot 集成,通过FTP定时采集处理文件

spring boot + apache camel + mongodb 集成问题