【问题标题】:Kafka Consumer metrics gone upon upgrade from Spring Boot 2.2.2 to 2.3.0Kafka Consumer 指标在从 Spring Boot 2.2.2 升级到 2.3.0 后消失
【发布时间】:2020-09-29 05:42:22
【问题描述】:

问题:

我们将 Spring Boot 版本从 2.2.2 升级到 2.3.0,在 2.2.2 的 Prometheus 端点中看到的所有 kafka_consumer_* 指标在 2.3.0 中都不可见。

例如,以下所有内容均缺失:

  • kafka_consumer_records_consumed_total_records_total
  • kafka_consumer_records_lag_records
  • kafka_consumer_fetch_latency_max_seconds
  • kafka_consumer_bytes_consumed_total_bytes_total

不确定我们是否遗漏了某种配置或隐藏在文档中的内容...

已经尝试过的:

  • 梳理 Spring Boot 2.3.0 发行说明、更新的 micrometer 文档和更新的 spring-kafka 文档,了解为什么会发生这种情况
  • 用 Google 搜索感觉就像是天涯海角
  • 尝试升级到 Spring Boot 2.2.7 并且 kafka 指标仍然存在,似乎只有升级到 2.3.0 会导致问题
  • 删除了我们项目代码中的所有不需要的依赖项/自定义项,并且只连接到本地主机上的 kafka 容器,但指标仍然没有出现

相关代码/详情:

  • 我们将 Red Hat AMQ Streams 用于我们的 kafka 代理(kafka 版本 2.1.1)
  • 我们在环境中唯一更改的是 Spring Boot 版本(以及自动引入/更新的依赖项)以重新创建此问题

以下是我们更改前的build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.2.2.RELEASE"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
    kotlin("jvm") version "1.3.72"
    kotlin("plugin.spring") version "1.3.72"
}

group = "ourGroup"
version = "0.0.1"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

extra["springCloudVersion"] = "Hoxton.RELEASE"

dependencyManagement {
    imports {
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
    }
}

dependencies {
    implementation("org.springframework.cloud:spring-cloud-starter-stream-kafka")
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("io.micrometer:micrometer-registry-prometheus")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
    testImplementation("io.projectreactor:reactor-test")
    testImplementation("org.springframework.security:spring-security-test")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

现在,如果我们只是用新的 Spring Boot 版本更新我们的 build.gradle.kts,如下行所示,我们的 kafka 指标就会消失:

    id("org.springframework.boot") version "2.3.0.RELEASE"

以下是我们在更改前后看到的 prometheus 指标的屏幕截图:

Before 2.3.0 upgrade

After 2.3.0 upgrade

提前感谢您的帮助!如果您需要任何其他详细信息,请告诉我!

【问题讨论】:

  • 我正在使用spring-kafka 并且刚刚测试了@AlanYeung 的问题。我可以确认在 Spring Boot 2.3.0.RELEASE 中,kafka_consumer_* 指标在 /actuator/prometheus 中消失了。我仍然可以在2.2.7.RELEASE 中看到它们。
  • Kafka 计量器已从抓取 JMX MBean 迁移到使用 Boot 2.0 中的本机指标。请参阅 this committhis one。最后一个使用this。明天我看看能不能弄清楚是什么坏了。
  • 真棒@GaryRussell,非常感谢您的指点!我在2.3.0.RELEASE 中有kafka_consumer_* 指标。干杯!
  • 更正 &gt;in Boot 2.0 应该在 Boot 2.3 中。
  • 我希望看到更多的新贡献者在他们的第一个问题上投入这么多精力。干得好,艾伦!

标签: spring-boot prometheus spring-kafka spring-cloud-stream spring-boot-actuator


【解决方案1】:

它适用于普通的 spring-kafka 消费者

@SpringBootApplication
public class So62292889Application {

    public static void main(String[] args) {
        SpringApplication.run(So62292889Application.class, args);
    }

    @KafkaListener(id = "so62292889", topics = "so62292889")
    public void listen(String in) {
        System.out.println(in);
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("so62292889").partitions(1).replicas(1).build();
    }

    @Bean
    public ApplicationRunner runner(MeterRegistry registry) {
        return args -> {
            registry.getMeters().forEach(meter -> System.out.println(meter.getId()));
        };
    }

}
MeterId{name='kafka.consumer.outgoing.byte.total'...

我看到你正在使用 spring-cloud-stream。问题是这个项目创建了自己的生产者/消费者工厂并且没有添加 Micrometer 监听器。

opened an issue against the binder

【讨论】:

    【解决方案2】:

    类似于@jumping_monkey,我有一个使用Spring Boot 2.3.0.RELEASE 的spring-kafka 应用程序,kafka_consumer_* 指标没有显示在/actuator/prometheus 中。当我降级到 Spring Boot 2.2.7.RELEASE 时,它们确实出现了。

    已确定使用 spring-cloud-stream 的应用存在问题,并且已计划解决该问题。但是,我的应用程序不使用 spring-cloud-stream;它使用spring-kafka

    解决方案是按照spring-kafka reference 上的指导将MicrometerConsumerListener 添加到消费者工厂。执行此操作后,指标会按预期显示。

    【讨论】:

    • 欢迎来到 Stack Overflow。以后,请确保在您真正找到解决方案之前不要发布答案;不应使用答案部分来重申您面临同样的问题。由于您编辑了答案以包含解决方案,因此我对其进行了改写,以删除您最初的后续问题,并直接进入您的解决方案。
    • 杰里米 - 感谢您的反馈。我最初的偏好是在上面添加评论,但因为我是新用户而被阻止添加评论。因此,我求助于在答案中发布问题。我认为最好采用这种方法,而不是为同一问题创建一个全新的问题/线程——起初我真的没有答案——在进行了大量调查之后。再次感谢!
    • @BrentBishop Boot 2.3 将自动添加侦听器,只要执行器启动器存在 - 请参阅 github.com/spring-projects/spring-boot/commit/… - 我刚刚对其进行了测试,它对我来说效果很好。
    • @GaryRussell:感谢您的测试。您的代码是否使用自动配置的 ConsumerFactory?我的应用程序确实存在执行器启动器。因为我需要动态分配引导服务器,所以我有自定义代码来创建 consumerFactory。我通过您共享的提交中的 Spring 代码进行了调试 - 并观察到添加侦听器的代码没有运行,因此需要在应用程序代码中添加它们。我说的对吗?
    • 如果是这样,这点给我造成了一点困惑。在预引导 2.3.0 中,我们只需要添加依赖项和可用的指标。要继续使用 2.3.0 获取指标,需要添加侦听器的代码。最后,如果是这样的话——这是一个微小而合理的改变。阅读文档时我没有注意到它。
    猜你喜欢
    • 2020-04-17
    • 2020-09-09
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 2020-04-14
    • 2020-04-24
    • 2020-10-26
    • 1970-01-01
    相关资源
    最近更新 更多