8 changed files with 403 additions and 0 deletions
@ -0,0 +1,63 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-present 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.micrometer.metrics.autoconfigure.export.logging; |
||||||
|
|
||||||
|
import io.micrometer.core.instrument.Clock; |
||||||
|
import io.micrometer.core.instrument.logging.LoggingMeterRegistry; |
||||||
|
import io.micrometer.core.instrument.logging.LoggingRegistryConfig; |
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration; |
||||||
|
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.boot.context.properties.EnableConfigurationProperties; |
||||||
|
import org.springframework.boot.micrometer.metrics.autoconfigure.CompositeMeterRegistryAutoConfiguration; |
||||||
|
import org.springframework.boot.micrometer.metrics.autoconfigure.MetricsAutoConfiguration; |
||||||
|
import org.springframework.boot.micrometer.metrics.autoconfigure.export.ConditionalOnEnabledMetricsExport; |
||||||
|
import org.springframework.boot.micrometer.metrics.autoconfigure.export.simple.SimpleMetricsExportAutoConfiguration; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@link EnableAutoConfiguration Auto-configuration} for exporting metrics to a |
||||||
|
* {@link LoggingMeterRegistry}. |
||||||
|
* |
||||||
|
* @author Vasily Pelikh |
||||||
|
* @since 4.0.0 |
||||||
|
*/ |
||||||
|
@AutoConfiguration( |
||||||
|
before = { CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class }, |
||||||
|
after = MetricsAutoConfiguration.class) |
||||||
|
@ConditionalOnBean(Clock.class) |
||||||
|
@ConditionalOnClass(LoggingMeterRegistry.class) |
||||||
|
@ConditionalOnEnabledMetricsExport("logging") |
||||||
|
@EnableConfigurationProperties(LoggingMetricsExportProperties.class) |
||||||
|
public final class LoggingMetricsExportAutoConfiguration { |
||||||
|
|
||||||
|
@Bean |
||||||
|
@ConditionalOnMissingBean |
||||||
|
LoggingRegistryConfig loggingRegistryConfig(LoggingMetricsExportProperties loggingMetricsExportProperties) { |
||||||
|
return new LoggingMetricsExportPropertiesConfigAdapter(loggingMetricsExportProperties); |
||||||
|
} |
||||||
|
|
||||||
|
@Bean |
||||||
|
@ConditionalOnMissingBean |
||||||
|
LoggingMeterRegistry loggingMeterRegistry(LoggingRegistryConfig config, Clock clock) { |
||||||
|
return new LoggingMeterRegistry(config, clock); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-present 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.micrometer.metrics.autoconfigure.export.logging; |
||||||
|
|
||||||
|
import io.micrometer.core.instrument.logging.LoggingMeterRegistry; |
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||||
|
import org.springframework.boot.micrometer.metrics.autoconfigure.export.properties.StepRegistryProperties; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@link ConfigurationProperties @ConfigurationProperties} for configuring metrics export |
||||||
|
* to a {@link LoggingMeterRegistry}. |
||||||
|
* |
||||||
|
* @author Vasily Pelikh |
||||||
|
* @since 4.0.0 |
||||||
|
*/ |
||||||
|
@ConfigurationProperties("management.logging.metrics.export") |
||||||
|
public class LoggingMetricsExportProperties extends StepRegistryProperties { |
||||||
|
|
||||||
|
/** |
||||||
|
* Whether counters and timers that have no activity in an interval are still logged. |
||||||
|
*/ |
||||||
|
private boolean logInactive = false; |
||||||
|
|
||||||
|
public boolean isLogInactive() { |
||||||
|
return this.logInactive; |
||||||
|
} |
||||||
|
|
||||||
|
public void setLogInactive(boolean logInactive) { |
||||||
|
this.logInactive = logInactive; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-present 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.micrometer.metrics.autoconfigure.export.logging; |
||||||
|
|
||||||
|
import io.micrometer.core.instrument.logging.LoggingRegistryConfig; |
||||||
|
|
||||||
|
import org.springframework.boot.micrometer.metrics.autoconfigure.export.properties.StepRegistryPropertiesConfigAdapter; |
||||||
|
|
||||||
|
/** |
||||||
|
* Adapter to convert {@link LoggingMetricsExportProperties} to a |
||||||
|
* {@link LoggingRegistryConfig}. |
||||||
|
* |
||||||
|
* @author Vasily Pelikh |
||||||
|
* @since 4.0.0 |
||||||
|
*/ |
||||||
|
public class LoggingMetricsExportPropertiesConfigAdapter |
||||||
|
extends StepRegistryPropertiesConfigAdapter<LoggingMetricsExportProperties> implements LoggingRegistryConfig { |
||||||
|
|
||||||
|
public LoggingMetricsExportPropertiesConfigAdapter(LoggingMetricsExportProperties properties) { |
||||||
|
super(properties); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String prefix() { |
||||||
|
return "management.logging.metrics.export"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean logInactive() { |
||||||
|
return obtain(LoggingMetricsExportProperties::isLogInactive, LoggingRegistryConfig.super::logInactive); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-present 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. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* Support for exporting actuator metrics to a log. |
||||||
|
*/ |
||||||
|
@NullMarked |
||||||
|
package org.springframework.boot.micrometer.metrics.autoconfigure.export.logging; |
||||||
|
|
||||||
|
import org.jspecify.annotations.NullMarked; |
||||||
@ -0,0 +1,128 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-present 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.micrometer.metrics.autoconfigure.export.logging; |
||||||
|
|
||||||
|
import io.micrometer.core.instrument.Clock; |
||||||
|
import io.micrometer.core.instrument.logging.LoggingMeterRegistry; |
||||||
|
import io.micrometer.core.instrument.logging.LoggingRegistryConfig; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||||
|
import org.springframework.boot.test.context.FilteredClassLoader; |
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.context.annotation.Import; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link LoggingMetricsExportAutoConfiguration}. |
||||||
|
* |
||||||
|
* @author Vasily Pelikh |
||||||
|
*/ |
||||||
|
class LoggingMetricsExportAutoConfigurationTests { |
||||||
|
|
||||||
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
||||||
|
.withConfiguration(AutoConfigurations.of(LoggingMetricsExportAutoConfiguration.class)); |
||||||
|
|
||||||
|
@Test |
||||||
|
void backsOffWithoutAClock() { |
||||||
|
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(LoggingMeterRegistry.class) |
||||||
|
.doesNotHaveBean(LoggingRegistryConfig.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void backOffWhenLoggingMeterRegistryIsMissing() { |
||||||
|
this.contextRunner.withUserConfiguration(BaseConfiguration.class) |
||||||
|
.withClassLoader(new FilteredClassLoader(LoggingMeterRegistry.class)) |
||||||
|
.run((context) -> assertThat(context).doesNotHaveBean(LoggingMeterRegistry.class) |
||||||
|
.doesNotHaveBean(LoggingRegistryConfig.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void autoConfiguresConfigAndMeterRegistry() { |
||||||
|
this.contextRunner.withUserConfiguration(BaseConfiguration.class) |
||||||
|
.run((context) -> assertThat(context).hasSingleBean(LoggingMeterRegistry.class) |
||||||
|
.hasSingleBean(LoggingRegistryConfig.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void autoConfigurationCanBeDisabledWithDefaultsEnabledProperty() { |
||||||
|
this.contextRunner.withUserConfiguration(BaseConfiguration.class) |
||||||
|
.withPropertyValues("management.defaults.metrics.export.enabled=false") |
||||||
|
.run((context) -> assertThat(context).doesNotHaveBean(LoggingMeterRegistry.class) |
||||||
|
.doesNotHaveBean(LoggingRegistryConfig.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void autoConfigurationCanBeDisabledWithSpecificEnabledProperty() { |
||||||
|
this.contextRunner.withUserConfiguration(BaseConfiguration.class) |
||||||
|
.withPropertyValues("management.logging.metrics.export.enabled=false") |
||||||
|
.run((context) -> assertThat(context).doesNotHaveBean(LoggingMeterRegistry.class) |
||||||
|
.doesNotHaveBean(LoggingRegistryConfig.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void allowsCustomConfigToBeUsed() { |
||||||
|
this.contextRunner.withUserConfiguration(CustomConfigConfiguration.class) |
||||||
|
.run((context) -> assertThat(context).hasSingleBean(LoggingMeterRegistry.class) |
||||||
|
.hasSingleBean(LoggingRegistryConfig.class) |
||||||
|
.hasBean("customConfig")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void allowsRegistryToBeCustomized() { |
||||||
|
this.contextRunner.withUserConfiguration(CustomRegistryConfiguration.class) |
||||||
|
.run((context) -> assertThat(context).hasSingleBean(LoggingMeterRegistry.class) |
||||||
|
.hasSingleBean(LoggingRegistryConfig.class) |
||||||
|
.hasBean("customRegistry")); |
||||||
|
} |
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false) |
||||||
|
static class BaseConfiguration { |
||||||
|
|
||||||
|
@Bean |
||||||
|
Clock customClock() { |
||||||
|
return Clock.SYSTEM; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false) |
||||||
|
@Import(BaseConfiguration.class) |
||||||
|
static class CustomConfigConfiguration { |
||||||
|
|
||||||
|
@Bean |
||||||
|
LoggingRegistryConfig customConfig() { |
||||||
|
return (key) -> null; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false) |
||||||
|
@Import(BaseConfiguration.class) |
||||||
|
static class CustomRegistryConfiguration { |
||||||
|
|
||||||
|
@Bean |
||||||
|
LoggingMeterRegistry customRegistry(LoggingRegistryConfig config, Clock clock) { |
||||||
|
return new LoggingMeterRegistry(config, clock); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,53 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-present 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.micrometer.metrics.autoconfigure.export.logging; |
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link LoggingMetricsExportPropertiesConfigAdapter}. |
||||||
|
* |
||||||
|
* @author Vasily Pelikh |
||||||
|
*/ |
||||||
|
class LoggingMetricsExportPropertiesConfigAdapterTests { |
||||||
|
|
||||||
|
private LoggingMetricsExportProperties properties; |
||||||
|
|
||||||
|
@BeforeEach |
||||||
|
void setUp() { |
||||||
|
this.properties = new LoggingMetricsExportProperties(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void whenPropertiesLogInactiveIsNotSetAdapterLogInactiveReturnsFalse() { |
||||||
|
assertThat(createAdapter().logInactive()).isFalse(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void whenPropertiesLogInactiveIsSetAdapterLogInactiveReturnsIt() { |
||||||
|
this.properties.setLogInactive(true); |
||||||
|
assertThat(createAdapter().logInactive()).isTrue(); |
||||||
|
} |
||||||
|
|
||||||
|
private LoggingMetricsExportPropertiesConfigAdapter createAdapter() { |
||||||
|
return new LoggingMetricsExportPropertiesConfigAdapter(this.properties); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,41 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-present 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.micrometer.metrics.autoconfigure.export.logging; |
||||||
|
|
||||||
|
import io.micrometer.core.instrument.logging.LoggingRegistryConfig; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import org.springframework.boot.micrometer.metrics.autoconfigure.export.properties.StepRegistryPropertiesTests; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link LoggingMetricsExportProperties}. |
||||||
|
* |
||||||
|
* @author Vasily Pelikh |
||||||
|
*/ |
||||||
|
class LoggingMetricsExportPropertiesTests extends StepRegistryPropertiesTests { |
||||||
|
|
||||||
|
@Test |
||||||
|
void defaultValuesAreConsistent() { |
||||||
|
LoggingMetricsExportProperties properties = new LoggingMetricsExportProperties(); |
||||||
|
LoggingRegistryConfig config = LoggingRegistryConfig.DEFAULT; |
||||||
|
assertStepRegistryDefaultValues(properties, config); |
||||||
|
assertThat(properties.isLogInactive()).isSameAs(config.logInactive()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue