Add prometheus to actuator-webmvc sample

Closes gh-86
This commit is contained in:
Moritz Halbritter
2022-08-10 15:39:46 +02:00
parent 7980166f08
commit acc650c08c
3 changed files with 32 additions and 4 deletions

View File

@@ -9,7 +9,8 @@ dependencies {
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("io.micrometer:micrometer-registry-prometheus")
runtimeOnly("io.micrometer:micrometer-registry-prometheus")
testImplementation("org.springframework.boot:spring-boot-starter-test")

View File

@@ -66,11 +66,14 @@ class ActuatorWebMvcApplicationAotTests {
}
@Test
void shouldHavePrometheusMetrics(WebTestClient client) {
void prometheusWorks(WebTestClient client) {
client.get().uri("/actuator/prometheus").exchange().expectStatus().isOk().expectBody()
.consumeWith((result) -> assertThat(new String(result.getResponseBodyContent()))
.contains("jvm_classes_loaded_classes "));
// Check custom timer
.contains("custom_timer_seconds_max 5.0").contains("custom_timer_seconds_count 1.0")
.contains("custom_timer_seconds_sum 5.0")
// Check JVM metric
.contains("# TYPE jvm_threads_peak_threads gauge"));
}
}

View File

@@ -0,0 +1,24 @@
package com.example.actuator.webmvc;
import java.time.Duration;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
class RegisterCustomTimer implements CommandLineRunner {
private final MeterRegistry meterRegistry;
RegisterCustomTimer(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
@Override
public void run(String... args) {
this.meterRegistry.timer("custom.timer").record(Duration.ofSeconds(5));
}
}