10 changed files with 351 additions and 0 deletions
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
/* |
||||
* Copyright 2012-2022 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.export.otlp; |
||||
|
||||
import io.micrometer.core.instrument.Clock; |
||||
import io.micrometer.registry.otlp.OtlpConfig; |
||||
import io.micrometer.registry.otlp.OtlpMeterRegistry; |
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration; |
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; |
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.ConditionalOnEnabledMetricsExport; |
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration; |
||||
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.context.annotation.Bean; |
||||
|
||||
/** |
||||
* {@link EnableAutoConfiguration Auto-configuration} for exporting metrics to OTLP. |
||||
* |
||||
* @author Eddú Meléndez |
||||
* @since 3.0.0 |
||||
*/ |
||||
@AutoConfiguration( |
||||
before = { CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class }, |
||||
after = MetricsAutoConfiguration.class) |
||||
@ConditionalOnBean(Clock.class) |
||||
@ConditionalOnClass(OtlpMeterRegistry.class) |
||||
@ConditionalOnEnabledMetricsExport("otlp") |
||||
@EnableConfigurationProperties(OtlpProperties.class) |
||||
public class OtlpMetricsExportAutoConfiguration { |
||||
|
||||
private final OtlpProperties properties; |
||||
|
||||
public OtlpMetricsExportAutoConfiguration(OtlpProperties properties) { |
||||
this.properties = properties; |
||||
} |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
public OtlpConfig otlpConfig() { |
||||
return new OtlpPropertiesConfigAdapter(this.properties); |
||||
} |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
public OtlpMeterRegistry otlpMeterRegistry(OtlpConfig otlpConfig, Clock clock) { |
||||
return new OtlpMeterRegistry(otlpConfig, clock); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,45 @@
@@ -0,0 +1,45 @@
|
||||
/* |
||||
* Copyright 2012-2022 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.export.otlp; |
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
|
||||
/** |
||||
* {@link ConfigurationProperties @ConfigurationProperties} for configuring OTLP metrics |
||||
* export. |
||||
* |
||||
* @author Eddú Meléndez |
||||
* @since 3.0.0 |
||||
*/ |
||||
@ConfigurationProperties(prefix = "management.otlp.metrics.export") |
||||
public class OtlpProperties extends StepRegistryProperties { |
||||
|
||||
/** |
||||
* URI of the OLTP server. |
||||
*/ |
||||
private String url = "http://localhost:4318/v1/metrics"; |
||||
|
||||
public String getUrl() { |
||||
return this.url; |
||||
} |
||||
|
||||
public void setUrl(String url) { |
||||
this.url = url; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
/* |
||||
* Copyright 2012-2022 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.export.otlp; |
||||
|
||||
import io.micrometer.registry.otlp.OtlpConfig; |
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter; |
||||
|
||||
/** |
||||
* Adapter to convert {@link OtlpProperties} to an {@link OtlpConfig}. |
||||
* |
||||
* @author Eddú Meléndez |
||||
*/ |
||||
class OtlpPropertiesConfigAdapter extends StepRegistryPropertiesConfigAdapter<OtlpProperties> implements OtlpConfig { |
||||
|
||||
OtlpPropertiesConfigAdapter(OtlpProperties properties) { |
||||
super(properties); |
||||
} |
||||
|
||||
@Override |
||||
public String prefix() { |
||||
return "management.otlp.metrics.export"; |
||||
} |
||||
|
||||
@Override |
||||
public String url() { |
||||
return get(OtlpProperties::getUrl, OtlpConfig.super::url); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
/* |
||||
* Copyright 2012-2022 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 OTLP. |
||||
*/ |
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.otlp; |
||||
@ -0,0 +1,112 @@
@@ -0,0 +1,112 @@
|
||||
/* |
||||
* Copyright 2012-2022 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.export.otlp; |
||||
|
||||
import io.micrometer.core.instrument.Clock; |
||||
import io.micrometer.registry.otlp.OtlpConfig; |
||||
import io.micrometer.registry.otlp.OtlpMeterRegistry; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
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 OtlpMetricsExportAutoConfiguration}. |
||||
* |
||||
* @author Eddú Meléndez |
||||
*/ |
||||
class OtlpMetricsExportAutoConfigurationTests { |
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
||||
.withConfiguration(AutoConfigurations.of(OtlpMetricsExportAutoConfiguration.class)); |
||||
|
||||
@Test |
||||
void backsOffWithoutAClock() { |
||||
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(OtlpMeterRegistry.class)); |
||||
} |
||||
|
||||
@Test |
||||
void autoConfiguresConfigAndMeterRegistry() { |
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class).run((context) -> assertThat(context) |
||||
.hasSingleBean(OtlpMeterRegistry.class).hasSingleBean(OtlpConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
void autoConfigurationCanBeDisabledWithDefaultsEnabledProperty() { |
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class) |
||||
.withPropertyValues("management.defaults.metrics.export.enabled=false") |
||||
.run((context) -> assertThat(context).doesNotHaveBean(OtlpMeterRegistry.class) |
||||
.doesNotHaveBean(OtlpConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
void autoConfigurationCanBeDisabledWithSpecificEnabledProperty() { |
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class) |
||||
.withPropertyValues("management.otlp.metrics.export.enabled=false").run((context) -> assertThat(context) |
||||
.doesNotHaveBean(OtlpMeterRegistry.class).doesNotHaveBean(OtlpConfig.class)); |
||||
} |
||||
|
||||
@Test |
||||
void allowsCustomConfigToBeUsed() { |
||||
this.contextRunner.withUserConfiguration(CustomConfigConfiguration.class).run((context) -> assertThat(context) |
||||
.hasSingleBean(OtlpMeterRegistry.class).hasSingleBean(OtlpConfig.class).hasBean("customConfig")); |
||||
} |
||||
|
||||
@Test |
||||
void allowsRegistryToBeCustomized() { |
||||
this.contextRunner.withUserConfiguration(CustomRegistryConfiguration.class).run((context) -> assertThat(context) |
||||
.hasSingleBean(OtlpMeterRegistry.class).hasSingleBean(OtlpConfig.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 |
||||
OtlpConfig customConfig() { |
||||
return (key) -> null; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
@Import(BaseConfiguration.class) |
||||
static class CustomRegistryConfiguration { |
||||
|
||||
@Bean |
||||
OtlpMeterRegistry customRegistry(OtlpConfig config, Clock clock) { |
||||
return new OtlpMeterRegistry(config, clock); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2012-2022 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.export.otlp; |
||||
|
||||
import io.micrometer.registry.otlp.OtlpConfig; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesTests; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link OtlpProperties}. |
||||
* |
||||
* @author Eddú Meléndez |
||||
*/ |
||||
class OtlpPropertiesTests extends StepRegistryPropertiesTests { |
||||
|
||||
@Test |
||||
void defaultValuesAreConsistent() { |
||||
OtlpProperties properties = new OtlpProperties(); |
||||
OtlpConfig config = OtlpConfig.DEFAULT; |
||||
assertStepRegistryDefaultValues(properties, config); |
||||
assertThat(properties.getUrl()).isEqualTo(config.url()); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue