Add startup time metrics

See gh-27878
This commit is contained in:
bono007
2021-09-05 21:05:50 -05:00
committed by Stephane Nicoll
parent 32cfde074f
commit 2e67963bfe
20 changed files with 601 additions and 30 deletions

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.metrics.startup;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.metrics.startup.StartupTimeMetrics;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* {@link EnableAutoConfiguration Auto-configuration} for the {@link StartupTimeMetrics}.
*
* @author Chris Bono
* @since 2.6.0
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter({ MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class })
@ConditionalOnClass(MeterRegistry.class)
@ConditionalOnBean(MeterRegistry.class)
public class StartupTimeMetricsAutoConfiguration {
@Bean
@ConditionalOnMissingBean
StartupTimeMetrics startupTimeMetrics(MeterRegistry meterRegistry) {
return new StartupTimeMetrics(meterRegistry);
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Auto-configuration for actuator startup time metrics.
*/
package org.springframework.boot.actuate.autoconfigure.metrics.startup;

View File

@@ -75,6 +75,7 @@ org.springframework.boot.actuate.autoconfigure.metrics.mongo.MongoMetricsAutoCon
org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.r2dbc.ConnectionPoolMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.redis.LettuceMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.startup.StartupTimeMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.task.TaskExecutorMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.web.client.HttpClientMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.web.jetty.JettyMetricsAutoConfiguration,\

View File

@@ -0,0 +1,92 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.metrics.startup;
import java.time.Duration;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
import org.springframework.boot.actuate.metrics.startup.StartupTimeMetrics;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link StartupTimeMetricsAutoConfiguration}.
*
* @author Chris Bono
*/
class StartupTimeMetricsAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().with(MetricsRun.simple())
.withConfiguration(AutoConfigurations.of(StartupTimeMetricsAutoConfiguration.class));
@Test
void startupTimeMetricsAreRecorded() {
this.contextRunner.run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext(), Duration.ofMillis(2500)));
context.publishEvent(new ApplicationReadyEvent(new SpringApplication(), null,
context.getSourceApplicationContext(), Duration.ofMillis(3000)));
assertThat(context).hasSingleBean(StartupTimeMetrics.class);
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
assertThat(registry.find("application.started.time").timeGauge()).isNotNull();
assertThat(registry.find("application.ready.time").timeGauge()).isNotNull();
});
}
@Test
void startupTimeMetricsCanBeDisabled() {
this.contextRunner.withPropertyValues("management.metrics.enable.application.started.time:false",
"management.metrics.enable.application.ready.time:false").run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext(), Duration.ofMillis(2500)));
context.publishEvent(new ApplicationReadyEvent(new SpringApplication(), null,
context.getSourceApplicationContext(), Duration.ofMillis(3000)));
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
assertThat(registry.find("application.started.time").timeGauge()).isNull();
assertThat(registry.find("application.ready.time").timeGauge()).isNull();
});
}
@Test
void customStartupTimeMetricsAreRespected() {
this.contextRunner.withUserConfiguration(CustomStartupTimeMetricsConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(StartupTimeMetrics.class)
.hasBean("customStartTimeMetrics"));
}
@Configuration(proxyBeanMethods = false)
static class CustomStartupTimeMetricsConfiguration {
@Bean
StartupTimeMetrics customStartTimeMetrics() {
return new StartupTimeMetrics(new SimpleMeterRegistry(), Tags.empty(), "myapp.started", "myapp.ready");
}
}
}

View File

@@ -16,6 +16,8 @@
package org.springframework.boot.actuate.autoconfigure.metrics.web.jetty;
import java.time.Duration;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
@@ -58,7 +60,7 @@ class JettyMetricsAutoConfigurationTests {
.withUserConfiguration(ServletWebServerConfiguration.class, MeterRegistryConfiguration.class)
.run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext()));
context.getSourceApplicationContext(), Duration.ZERO));
assertThat(context).hasSingleBean(JettyServerThreadPoolMetricsBinder.class);
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
assertThat(registry.find("jetty.threads.config.min").meter()).isNotNull();
@@ -73,7 +75,7 @@ class JettyMetricsAutoConfigurationTests {
.withUserConfiguration(ReactiveWebServerConfiguration.class, MeterRegistryConfiguration.class)
.run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext()));
context.getSourceApplicationContext(), Duration.ZERO));
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
assertThat(registry.find("jetty.threads.config.min").meter()).isNotNull();
});
@@ -95,7 +97,7 @@ class JettyMetricsAutoConfigurationTests {
.withUserConfiguration(ServletWebServerConfiguration.class, MeterRegistryConfiguration.class)
.run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext()));
context.getSourceApplicationContext(), Duration.ZERO));
assertThat(context).hasSingleBean(JettyConnectionMetricsBinder.class);
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
assertThat(registry.find("jetty.connections.messages.in").meter()).isNotNull();
@@ -110,7 +112,7 @@ class JettyMetricsAutoConfigurationTests {
.withUserConfiguration(ReactiveWebServerConfiguration.class, MeterRegistryConfiguration.class)
.run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext()));
context.getSourceApplicationContext(), Duration.ZERO));
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
assertThat(registry.find("jetty.connections.messages.in").meter()).isNotNull();
});
@@ -125,7 +127,7 @@ class JettyMetricsAutoConfigurationTests {
MeterRegistryConfiguration.class)
.run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext()));
context.getSourceApplicationContext(), Duration.ZERO));
assertThat(context).hasSingleBean(JettyConnectionMetricsBinder.class)
.hasBean("customJettyConnectionMetricsBinder");
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
@@ -144,7 +146,7 @@ class JettyMetricsAutoConfigurationTests {
"server.ssl.key-store-password: secret", "server.ssl.key-password: password")
.run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext()));
context.getSourceApplicationContext(), Duration.ZERO));
assertThat(context).hasSingleBean(JettySslHandshakeMetricsBinder.class);
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
assertThat(registry.find("jetty.ssl.handshakes").meter()).isNotNull();
@@ -161,7 +163,7 @@ class JettyMetricsAutoConfigurationTests {
"server.ssl.key-store-password: secret", "server.ssl.key-password: password")
.run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext()));
context.getSourceApplicationContext(), Duration.ZERO));
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
assertThat(registry.find("jetty.ssl.handshakes").meter()).isNotNull();
});
@@ -178,7 +180,7 @@ class JettyMetricsAutoConfigurationTests {
"server.ssl.key-store-password: secret", "server.ssl.key-password: password")
.run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext()));
context.getSourceApplicationContext(), Duration.ZERO));
assertThat(context).hasSingleBean(JettySslHandshakeMetricsBinder.class)
.hasBean("customJettySslHandshakeMetricsBinder");
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.autoconfigure.metrics.web.tomcat;
import java.time.Duration;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicInteger;
@@ -62,7 +63,7 @@ class TomcatMetricsAutoConfigurationTests {
.withUserConfiguration(ServletWebServerConfiguration.class, MeterRegistryConfiguration.class)
.withPropertyValues("server.tomcat.mbeanregistry.enabled=true").run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext()));
context.getSourceApplicationContext(), Duration.ZERO));
assertThat(context).hasSingleBean(TomcatMetricsBinder.class);
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
assertThat(registry.find("tomcat.sessions.active.max").meter()).isNotNull();
@@ -79,7 +80,7 @@ class TomcatMetricsAutoConfigurationTests {
.withUserConfiguration(ReactiveWebServerConfiguration.class, MeterRegistryConfiguration.class)
.withPropertyValues("server.tomcat.mbeanregistry.enabled=true").run((context) -> {
context.publishEvent(new ApplicationStartedEvent(new SpringApplication(), null,
context.getSourceApplicationContext()));
context.getSourceApplicationContext(), Duration.ZERO));
SimpleMeterRegistry registry = context.getBean(SimpleMeterRegistry.class);
assertThat(registry.find("tomcat.sessions.active.max").meter()).isNotNull();
assertThat(registry.find("tomcat.threads.current").meter()).isNotNull();