Skip to content

Commit

Permalink
Merge pull request #27 from logchange/tomcat-version
Browse files Browse the repository at this point in the history
added metric with tomcat version
  • Loading branch information
marwin1991 committed Feb 21, 2024
2 parents e8cb116 + 6666871 commit f614a8f
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 12 deletions.
10 changes: 10 additions & 0 deletions changelog/unreleased/000003-tomcat-version.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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();
}



}

Original file line number Diff line number Diff line change
@@ -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<Tag> tags() {
List<Tag> tags = new LinkedList<>();

tags.add(Tag.of("name", info.getName()));
tags.add(Tag.of("version", info.getVersion()));

return tags;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.logchange.hofund.web;

public interface HofundWebServerInfoProvider {

HofundWebServerInfo get();

}

22 changes: 16 additions & 6 deletions hofund-spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>dev.logchange.hofund</groupId>
<artifactId>hofund-spring</artifactId>
<version>${project.version}</version>
<optional>true</optional> <!-- Optional, because it's defined in -starter module -->
</dependency>

<!-- https://www.baeldung.com/maven-optional-dependency#what-is-ltoptionalgt -->

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat-embed-core.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
Expand All @@ -37,12 +53,6 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>dev.logchange.hofund</groupId>
<artifactId>hofund-spring</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@
<lombok.version>1.18.30</lombok.version>

<!-- Provided, project with spring has to have spring, so we should not create jar hell with other spring version -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot/2.2.0.RELEASE -->
<spring-boot.version>[2.2.0.RELEASE,)</spring-boot.version>
<micrometer-io.version>[1.3.0,)</micrometer-io.version>
<spring-framework.version>[5.2.12.RELEASE,6.0.0)</spring-framework.version>
<spring-boot.version>[2.2.0.RELEASE,3.0.0)</spring-boot.version>
<spring-framework.version>[5.2.12.RELEASE,)</spring-framework.version>
<tomcat-embed-core.version>[9.0.27,)</tomcat-embed-core.version>
<micrometer-registry-prometheus.version>[1.3.16,)</micrometer-registry-prometheus.version>
<slf4j.version>[1.7.28,)</slf4j.version>

Expand Down

0 comments on commit f614a8f

Please sign in to comment.