Skip to content

Commit

Permalink
Merge pull request #25 from logchange/24-git-info-default-values
Browse files Browse the repository at this point in the history
added git-info default values
  • Loading branch information
marwin1991 authored Feb 17, 2024
2 parents 5f5c912 + 4cdd037 commit 8b4645a
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 34 deletions.
61 changes: 34 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Your project has to contain:
<dependency>
<groupId>dev.logchange.hofund</groupId>
<artifactId>hofund-spring-boot-starter</artifactId>
<version>0.3.0</version>
<version>1.0.0</version>
</dependency>
...
</dependencies>
Expand All @@ -75,16 +75,27 @@ Your project has to contain:
...
<plugins>
...
<plugin>
<!-- For multi-module projects this plugin should be in module which produce final package (.jar/.war/.ear) -->
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>4.9.10</version> <!-- for java 11 you can use 5.0.0 (https://github.com/git-commit-id/git-commit-id-maven-plugin#relocation-of-the-project) -->
<configuration>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<injectAllReactorProjects>true</injectAllReactorProjects>
</configuration>
</plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>7.0.0</version>
<!--
For older version of java (f.e. 8) see https://github.com/git-commit-id/git-commit-id-maven-plugin
And https://github.com/logchange/hofund/tree/0.6.0?tab=readme-ov-file#1-add-to-your-pomxml
-->
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<injectAllReactorProjects>true</injectAllReactorProjects>
</configuration>
...
</plugins>
...
Expand Down Expand Up @@ -132,13 +143,6 @@ For maven project add to your `application.properties` following entries, but yo
```properties
hofund.info.application.name=@project.name@
hofund.info.application.version=@project.version@
hofund.git-info.commit.id=@git.commit.id@
hofund.git-info.commit.id-abbrev=@git.commit.id.abbrev@
hofund.git-info.dirty=@git.dirty@
hofund.git-info.branch=@git.branch@
hofund.git-info.build.host=@git.build.host@
hofund.git-info.build.time=@git.build.time@
```

or
Expand All @@ -149,17 +153,20 @@ hofund:
application:
name: @project.name@
version: @project.version@
git-info:
commit:
id: @git.commit.id@
id-abbrev: @git.commit.id.abbrev@
dirty: @git.dirty@
branch: @git.branch@
build:
host: @git.build.host@
time: @git.build.time@
```

You can also overrride custom values for `GitInfo` metrics
```properties
hofund.git-info.commit.id=someid # default value is equal to git.commit.id property from git.properties file generated by git-commit-id-maven-plugin
hofund.git-info.commit.id-abbrev=someAbbrevId # default value is equal to git.commit.id.abbrev
hofund.git-info.dirty=true # default value is equal to git.dirty
hofund.git-info.branch=feature-1 # default value is equal to git.branch
hofund.git-info.build.host=someHost # default value is equal to git.build.host
hofund.git-info.build.time=11:12:13T11-11-2023 # default value is equal to git.build.time
```



### 3. Now you can start your application and verify exposed prometheus metric:

```text
Expand Down
10 changes: 10 additions & 0 deletions changelog/unreleased/000003-default-git-properties.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: Simplified configuration of metric `hofund_git_info` by reading values from `git.properties` file instead of defining `hofund.git-info`. You can still use this to define custom values for this metric.
authors:
- name: Peter Zmilczak
nick: marwin1991
url: https://github.com/marwin1991
type: changed #[added/changed/deprecated/removed/fixed/security/other]
issues:
- 24
merge_requests:
- 25
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.logchange.hofund.git.springboot.autoconfigure;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Getter
@Setter
@Component
@PropertySource(value = "git.properties", ignoreResourceNotFound = true)
@RequiredArgsConstructor
public class HofundDefaultGitInfoProperties {

private final Environment env;

public String getCommitId(){
return get("git.commit.id");
}

public String getCommitIdAbbrev(){
return get("git.commit.id.abbrev");
}

public String getDirty(){
return get("git.dirty");
}

public String getBranch(){
return get("git.branch");
}

public String getBuildHost(){
return get("git.build.host");
}

public String getBuildTime(){
return get("git.build.time");
}

private String get(String key){
String value = env.getProperty(key);
if (value == null){
return "";
}

return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class HofundGitInfoAutoConfiguration {

private final HofundGitInfoProperties properties;

private final HofundDefaultGitInfoProperties defaultProperties;

@Bean
@ConditionalOnMissingBean
public HofundGitInfoMeter hofundGitInfoMeter(HofundGitInfoProvider infoProvider) {
Expand All @@ -33,30 +35,41 @@ public HofundGitInfoProvider hofundGitInfoProvider() {
return new HofundGitInfoProvider() {
@Override
public String getCommitId() {
return properties.getCommit().getIdAbbrev();
return defaultIfEmpty(properties.getCommit().getIdAbbrev(), defaultProperties.getCommitIdAbbrev());
}

@Override
public String dirty() {
return properties.getDirty();
return defaultIfEmpty(properties.getDirty(), defaultProperties.getDirty());
}

@Override
public String getBranch() {
return properties.getBranch();
return defaultIfEmpty(properties.getBranch(), defaultProperties.getBranch());
}

@Override
public String getBuildHost() {
return properties.getBuild().getHost();
return defaultIfEmpty(properties.getBuild().getHost(), defaultProperties.getBuildHost());
}

@Override
public String getBuildTime() {
return properties.getBuild().getTime();
return defaultIfEmpty(properties.getBuild().getTime(), defaultProperties.getBuildTime());
}
};
}

public static String defaultIfEmpty(String val, String defaultVal) {
if (isEmpty(val)) {
return defaultVal;
}

return val;
}

public static boolean isEmpty(String val) {
return val == null || val.trim().isEmpty();
}

}
29 changes: 29 additions & 0 deletions hofund-spring-boot-e2e/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<start-class>dev.logchange.hofund.HofundE2E</start-class>

<spring-boot-for-e2e.version>3.2.2</spring-boot-for-e2e.version>
<git-commit-id-maven-plugin.version>7.0.0</git-commit-id-maven-plugin.version>
</properties>


Expand Down Expand Up @@ -66,6 +68,33 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-for-e2e.version}</version>

<executions>
<execution>
<goals>
<goal>repackage</goal> <!-- https://stackoverflow.com/a/54867850/10346383 -->
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>${git-commit-id-maven-plugin.version}</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<injectAllReactorProjects>true</injectAllReactorProjects>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class HofundE2E {

public static void main(String[] args) {
SpringApplication.run(HofundE2E.class, args);
}


@GetMapping("/test")
public String test() {
return "test";
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
server.port=8626
management.endpoints.web.exposure.include=prometheus
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dev.logchange.hofund.git;


import lombok.extern.slf4j.Slf4j;
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;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@Slf4j
@AutoConfigureObservability
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HofundDefaultGitInfoE2ETest {

private final TestRestTemplate template = new TestRestTemplate();

@LocalServerPort
private int port;

@DynamicPropertySource
static void registerPgProperties(DynamicPropertyRegistry registry) {
registry.add("git.commit.id", () -> 123456);
registry.add("git.commit.id.abbrev", () -> "abcdef");
registry.add("git.dirty", () -> false);
registry.add("git.branch", () -> "alaMaKota");
registry.add("git.build.host", () -> "someHostName");
registry.add("git.build.time", () -> "11:12:33T17-02-2023");
}

@Test
void shouldContainsHofundDefaultGitInfoWithValuesFromGitProperties() {
//given:
String path = "http://localhost:" + port + "/actuator/prometheus";

String expected = "# HELP hofund_git_info Basic information about application based on git\n" +
"# TYPE hofund_git_info gauge\n" +
"hofund_git_info{branch=\"alaMaKota\",build_host=\"someHostName\",build_time=\"11:12:33T17-02-2023\",commit_id=\"abcdef\",dirty=\"false\",} 1.0";

//when:
String response = template.getForObject(path, String.class);

//then:
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,46 @@
package dev.logchange.hofund.git;


import lombok.extern.slf4j.Slf4j;
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, properties = {
"hofund.git-info.commit.id=SomeIdForTest",
"hofund.git-info.commit.id-abbrev=someAbbrevIdForE2eTest",
"hofund.git-info.dirty=true",
"hofund.git-info.branch=feature-1-for-test-e2e",
"hofund.git-info.build.host=someHostForTest",
"hofund.git-info.build.time=14:12:13T11-11-2023"
})
public class HofundGitInfoE2ETest {

private final TestRestTemplate template = new TestRestTemplate();

@LocalServerPort
private int port;

@Test
void shouldContainsHofundDefaultGitInfoWithValuesFromGitProperties() {
//given:
String path = "http://localhost:" + port + "/actuator/prometheus";

String expected =
"# HELP hofund_git_info Basic information about application based on git\n" +
"# TYPE hofund_git_info gauge\n" +
"hofund_git_info{branch=\"feature-1-for-test-e2e\",build_host=\"someHostForTest\",build_time=\"14:12:13T11-11-2023\",commit_id=\"someAbbrevIdForE2eTest\",dirty=\"true\",} 1.0";

//when:
String response = template.getForObject(path, String.class);

//then:
log.info("Expecting: \n{}\nResponse: \n{}", expected, response);
Assertions.assertTrue(response.contains(expected));
}
}
Loading

0 comments on commit 8b4645a

Please sign in to comment.