首页 > 解决方案 > 在 Spring Boot Camel 应用程序公开的 Micrometer / Prometheus 信息中包含其他 JMX 指标

问题描述

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

# HELP CamelExchangesFailed_total  
# TYPE CamelExchangesFailed_total counter
CamelExchangesFailed_total{camelContext="camel-1",routeId="route3",serviceName="MicrometerRoutePolicyService",} 0.0
CamelExchangesFailed_total{camelContext="camel-1",routeId="route2",serviceName="MicrometerRoutePolicyService",} 0.0
CamelExchangesFailed_total{camelContext="camel-1",routeId="route1",serviceName="MicrometerRoutePolicyService",} 0.0
# HELP CamelExchangesTotal_total  
# TYPE CamelExchangesTotal_total counter
CamelExchangesTotal_total{camelContext="camel-1",routeId="route3",serviceName="MicrometerRoutePolicyService",} 0.0
CamelExchangesTotal_total{camelContext="camel-1",routeId="route2",serviceName="MicrometerRoutePolicyService",} 0.0
CamelExchangesTotal_total{camelContext="camel-1",routeId="route1",serviceName="MicrometerRoutePolicyService",} 0.0
# HELP CamelExchangesExternalRedeliveries_total  
# TYPE CamelExchangesExternalRedeliveries_total counter
CamelExchangesExternalRedeliveries_total{camelContext="camel-1",routeId="route3",serviceName="MicrometerRoutePolicyService",} 0.0
CamelExchangesExternalRedeliveries_total{camelContext="camel-1",routeId="route2",serviceName="MicrometerRoutePolicyService",} 0.0
CamelExchangesExternalRedeliveries_total{camelContext="camel-1",routeId="route1",serviceName="MicrometerRoutePolicyService",} 0.0
# HELP CamelExchangesSucceeded_total  
# TYPE CamelExchangesSucceeded_total counter
CamelExchangesSucceeded_total{camelContext="camel-1",routeId="route3",serviceName="MicrometerRoutePolicyService",} 0.0
CamelExchangesSucceeded_total{camelContext="camel-1",routeId="route2",serviceName="MicrometerRoutePolicyService",} 0.0
CamelExchangesSucceeded_total{camelContext="camel-1",routeId="route1",serviceName="MicrometerRoutePolicyService",} 0.0
# HELP CamelExchangesFailuresHandled_total  
# TYPE CamelExchangesFailuresHandled_total counter
CamelExchangesFailuresHandled_total{camelContext="camel-1",routeId="route3",serviceName="MicrometerRoutePolicyService",} 0.0
CamelExchangesFailuresHandled_total{camelContext="camel-1",routeId="route2",serviceName="MicrometerRoutePolicyService",} 0.0
CamelExchangesFailuresHandled_total{camelContext="camel-1",routeId="route1",serviceName="MicrometerRoutePolicyService",} 0.0

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

在此处输入图像描述

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

以下文章描述了一种使用 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"
}

骆驼上下文配置:


    @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) {

            }
        };
    }

应用程序.yml

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

      metric-registry: prometheusMeterRegistry


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

标签: spring-bootapache-camelspring-boot-actuatormicrometerspring-micrometer

解决方案


如果有可以启用的东西,请先检查骆驼指标配置。如果 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 有一些是Meter使用 JMX 的数据注册 s 的,例如:KafkaConsumerMetricsCommonsObjectPool2Metrics,你可以看看。

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


推荐阅读