diff --git a/changelog/unreleased/000003-tomcat-version.yml b/changelog/unreleased/000003-tomcat-version.yml new file mode 100644 index 0000000..ab3759f --- /dev/null +++ b/changelog/unreleased/000003-tomcat-version.yml @@ -0,0 +1,10 @@ +title: Added new metric `hofund_web_server_info` with information about type and version of web server. +authors: + - name: Peter Zmilczak + nick: marwin1991 + url: https://github.com/marwin1991 +type: added #[added/changed/deprecated/removed/fixed/security/other] +issues: + - 26 +merge_requests: + - 27 \ No newline at end of file diff --git a/hofund-core/src/main/java/dev/logchange/hofund/web/HofundWebServerInfo.java b/hofund-core/src/main/java/dev/logchange/hofund/web/HofundWebServerInfo.java new file mode 100644 index 0000000..5a525de --- /dev/null +++ b/hofund-core/src/main/java/dev/logchange/hofund/web/HofundWebServerInfo.java @@ -0,0 +1,31 @@ +package dev.logchange.hofund.web; + +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Getter +@Builder(access = AccessLevel.PRIVATE) +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public class HofundWebServerInfo { + + private final String name; + + private final String version; + + public static HofundWebServerInfo create(String name, String version) { + log.info("Server name: " + name + " version: " + version); + + return HofundWebServerInfo.builder() + .name(name) + .version(version) + .build(); + } + + + +} + diff --git a/hofund-core/src/main/java/dev/logchange/hofund/web/HofundWebServerInfoMeter.java b/hofund-core/src/main/java/dev/logchange/hofund/web/HofundWebServerInfoMeter.java new file mode 100644 index 0000000..36306af --- /dev/null +++ b/hofund-core/src/main/java/dev/logchange/hofund/web/HofundWebServerInfoMeter.java @@ -0,0 +1,42 @@ +package dev.logchange.hofund.web; + +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.binder.MeterBinder; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +public class HofundWebServerInfoMeter implements MeterBinder { + + private static final String NAME = "hofund_web_server_info"; + private static final String DESCRIPTION = "Basic information about web server that is running this application"; + + private final HofundWebServerInfo info; + private final AtomicInteger atomicInteger; + + public HofundWebServerInfoMeter(HofundWebServerInfoProvider provider) { + this.info = provider.get(); + this.atomicInteger = new AtomicInteger(1); + } + + @Override + public void bindTo(MeterRegistry meterRegistry) { + Gauge.builder(NAME, atomicInteger, AtomicInteger::doubleValue) + .description(DESCRIPTION) + .tags(tags()) + .register(meterRegistry); + } + + private List tags() { + List tags = new LinkedList<>(); + + tags.add(Tag.of("name", info.getName())); + tags.add(Tag.of("version", info.getVersion())); + + return tags; + } + +} diff --git a/hofund-core/src/main/java/dev/logchange/hofund/web/HofundWebServerInfoProvider.java b/hofund-core/src/main/java/dev/logchange/hofund/web/HofundWebServerInfoProvider.java new file mode 100644 index 0000000..4ddc8c1 --- /dev/null +++ b/hofund-core/src/main/java/dev/logchange/hofund/web/HofundWebServerInfoProvider.java @@ -0,0 +1,8 @@ +package dev.logchange.hofund.web; + +public interface HofundWebServerInfoProvider { + + HofundWebServerInfo get(); + +} + diff --git a/hofund-spring-boot-autoconfigure/pom.xml b/hofund-spring-boot-autoconfigure/pom.xml index c1951ff..0e2fc68 100644 --- a/hofund-spring-boot-autoconfigure/pom.xml +++ b/hofund-spring-boot-autoconfigure/pom.xml @@ -23,6 +23,22 @@ provided + + dev.logchange.hofund + hofund-spring + ${project.version} + true + + + + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat-embed-core.version} + true + + org.springframework.boot spring-boot-configuration-processor @@ -37,12 +53,6 @@ true - - dev.logchange.hofund - hofund-spring - ${project.version} - true - diff --git a/hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/web/springboot/autoconfigure/HofundWebServerInfoAutoConfiguration.java b/hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/web/springboot/autoconfigure/HofundWebServerInfoAutoConfiguration.java new file mode 100644 index 0000000..4806573 --- /dev/null +++ b/hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/web/springboot/autoconfigure/HofundWebServerInfoAutoConfiguration.java @@ -0,0 +1,34 @@ +package dev.logchange.hofund.web.springboot.autoconfigure; + +import dev.logchange.hofund.web.HofundWebServerInfo; +import dev.logchange.hofund.web.HofundWebServerInfoMeter; +import dev.logchange.hofund.web.HofundWebServerInfoProvider; +import io.micrometer.prometheus.PrometheusMeterRegistry; +import lombok.RequiredArgsConstructor; +import org.apache.catalina.util.ServerInfo; +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; + +@RequiredArgsConstructor +@Configuration(proxyBeanMethods = false) +//available since spring boot 2.4.0 +//@ConditionalOnEnabledMetricsExport(value="prometheus") +@ConditionalOnClass(PrometheusMeterRegistry.class) +public class HofundWebServerInfoAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public HofundWebServerInfoMeter hofundWebServerInfoMeter(HofundWebServerInfoProvider provider) { + return new HofundWebServerInfoMeter(provider); + } + + + @Bean + @ConditionalOnClass(ServerInfo.class) + public HofundWebServerInfoProvider tomcatHofundWebServerInfoProvider(){ + return () -> HofundWebServerInfo.create("Apache Tomcat", ServerInfo.getServerNumber()); + } + +} diff --git a/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index d748854..238c2ae 100644 --- a/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -2,4 +2,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ dev.logchange.hofund.info.springboot.autoconfigure.HofundInfoAutoConfiguration,\ dev.logchange.hofund.git.springboot.autoconfigure.HofundGitInfoAutoConfiguration,\ dev.logchange.hofund.connection.springboot.autoconfigure.HofundConnectionAutoConfiguration,\ -dev.logchange.hofund.graph.springboot.autoconfigure.HofundGraphAutoConfiguration +dev.logchange.hofund.graph.springboot.autoconfigure.HofundGraphAutoConfiguration,\ +dev.logchange.hofund.java.springboot.autoconfigure.HofundJavaInfoAutoConfiguration,\ +dev.logchange.hofund.os.springboot.autoconfigure.HofundOsInfoAutoConfiguration,\ +dev.logchange.hofund.web.springboot.autoconfigure.HofundWebServerInfoAutoConfiguration \ No newline at end of file diff --git a/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index b0345ec..3baed4e 100644 --- a/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,4 +1,7 @@ dev.logchange.hofund.info.springboot.autoconfigure.HofundInfoAutoConfiguration dev.logchange.hofund.git.springboot.autoconfigure.HofundGitInfoAutoConfiguration dev.logchange.hofund.connection.springboot.autoconfigure.HofundConnectionAutoConfiguration -dev.logchange.hofund.graph.springboot.autoconfigure.HofundGraphAutoConfiguration \ No newline at end of file +dev.logchange.hofund.graph.springboot.autoconfigure.HofundGraphAutoConfiguration +dev.logchange.hofund.java.springboot.autoconfigure.HofundJavaInfoAutoConfiguration +dev.logchange.hofund.os.springboot.autoconfigure.HofundOsInfoAutoConfiguration +dev.logchange.hofund.web.springboot.autoconfigure.HofundWebServerInfoAutoConfiguration \ No newline at end of file diff --git a/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/java/HofundJavaInfoE2ETest.java b/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/java/HofundJavaInfoE2ETest.java index ecb7855..d9fa190 100644 --- a/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/java/HofundJavaInfoE2ETest.java +++ b/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/java/HofundJavaInfoE2ETest.java @@ -53,7 +53,7 @@ void shouldContainsHofundJavaInfo() { String response = template.getForObject(path, String.class); //then: - log.info("Expecting: \n{}", expected); + log.info("Expecting: \n{}\nResponse: \n{}", expected, response); Assertions.assertTrue(response.contains(expected)); } } diff --git a/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/os/HofundOsInfoE2ETest.java b/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/os/HofundOsInfoE2ETest.java index 5f5e986..8f131ef 100644 --- a/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/os/HofundOsInfoE2ETest.java +++ b/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/os/HofundOsInfoE2ETest.java @@ -40,7 +40,7 @@ void shouldContainsHofundOsInfo() { String response = template.getForObject(path, String.class); //then: - log.info("Expecting: \n{}", expected); + log.info("Expecting: \n{}\nResponse: \n{}", expected, response); Assertions.assertTrue(response.contains(expected)); } } diff --git a/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/web/HofundWebServerInfoE2ETest.java b/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/web/HofundWebServerInfoE2ETest.java new file mode 100644 index 0000000..291dc53 --- /dev/null +++ b/hofund-spring-boot-e2e/src/test/java/dev/logchange/hofund/web/HofundWebServerInfoE2ETest.java @@ -0,0 +1,43 @@ +package dev.logchange.hofund.web; + + +import lombok.extern.slf4j.Slf4j; +import org.apache.catalina.util.ServerInfo; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; + +@Slf4j +@AutoConfigureObservability +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class HofundWebServerInfoE2ETest { + + private final TestRestTemplate template = new TestRestTemplate(); + + @LocalServerPort + private int port; + + @Test + void shouldContainsHofundOsInfo() { + //given: + String path = "http://localhost:" + port + "/actuator/prometheus"; + + String version = ServerInfo.getServerNumber(); + + String expected = + "# HELP hofund_web_server_info Basic information about web server that is running this application\n" + + "# TYPE hofund_web_server_info gauge\n" + + "hofund_web_server_info{name=\"Apache Tomcat\",version=\"{version}\",} 1.0" + .replace("{version}", version); + + //when: + String response = template.getForObject(path, String.class); + + //then: + log.info("Expecting: \n{}\nResponse: \n{}", expected, response); + Assertions.assertTrue(response.contains(expected)); + } +} diff --git a/pom.xml b/pom.xml index e8ff2c1..e3f89a4 100644 --- a/pom.xml +++ b/pom.xml @@ -64,9 +64,11 @@ 1.18.30 + + [2.2.0.RELEASE,) [1.3.0,) - [5.2.12.RELEASE,6.0.0) - [2.2.0.RELEASE,3.0.0) + [5.2.12.RELEASE,) + [9.0.27,) [1.3.16,) [1.7.28,)