Skip to content

Commit

Permalink
Add Java InfoContributor
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov authored and snicoll committed Oct 5, 2021
1 parent 0b58d48 commit fc87da7
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.boot.actuate.info.EnvironmentInfoContributor;
import org.springframework.boot.actuate.info.GitInfoContributor;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.boot.actuate.info.java.JavaInfoContributor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -40,6 +41,7 @@
*
* @author Meang Akira Tanaka
* @author Stephane Nicoll
* @author Jonatan Ivanov
* @since 2.0.0
*/
@Configuration(proxyBeanMethods = false)
Expand Down Expand Up @@ -77,4 +79,11 @@ public InfoContributor buildInfoContributor(BuildProperties buildProperties) {
return new BuildInfoContributor(buildProperties);
}

@Bean
@ConditionalOnEnabledInfoContributor("java")
@Order(DEFAULT_ORDER)
public InfoContributor javaInfoContributor() {
return new JavaInfoContributor();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.info.BuildInfoContributor;
import org.springframework.boot.actuate.info.EnvironmentInfoContributor;
import org.springframework.boot.actuate.info.GitInfoContributor;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.boot.actuate.info.java.JavaInfo;
import org.springframework.boot.actuate.info.java.JavaInfoContributor;
import org.springframework.boot.info.BuildProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.boot.test.util.TestPropertyValues;
Expand All @@ -39,6 +42,7 @@
* Tests for {@link InfoContributorAutoConfiguration}.
*
* @author Stephane Nicoll
* @author Jonatan Ivanov
*/
class InfoContributorAutoConfigurationTests {

Expand All @@ -55,7 +59,26 @@ void close() {
void disableEnvContributor() {
load("management.info.env.enabled:false");
Map<String, InfoContributor> beans = this.context.getBeansOfType(InfoContributor.class);
assertThat(beans).hasSize(0);
assertThat(beans).extractingFromEntries(Map.Entry::getValue).allSatisfy(
(infoContributor) -> assertThat(infoContributor).isNotInstanceOf(EnvironmentInfoContributor.class));
}

@Test
void disableJavaContributor() {
load("management.info.java.enabled:false");
Map<String, InfoContributor> beans = this.context.getBeansOfType(InfoContributor.class);
assertThat(beans).extractingFromEntries(Map.Entry::getValue).allSatisfy(
(infoContributor) -> assertThat(infoContributor).isNotInstanceOf(JavaInfoContributor.class));
}

@Test
void defaultInfoContributorsEnabled() {
load();
Map<String, InfoContributor> beans = this.context.getBeansOfType(InfoContributor.class);
assertThat(beans).hasSize(2).extractingFromEntries(Map.Entry::getValue)
.anySatisfy(
(infoContributor) -> assertThat(infoContributor).isInstanceOf(EnvironmentInfoContributor.class))
.anySatisfy((infoContributor) -> assertThat(infoContributor).isInstanceOf(JavaInfoContributor.class));
}

@Test
Expand Down Expand Up @@ -129,6 +152,17 @@ void customBuildInfoContributor() {
.isSameAs(this.context.getBean("customBuildInfoContributor"));
}

@Test
void javaInfoContributor() {
load();
Map<String, InfoContributor> beans = this.context.getBeansOfType(InfoContributor.class);
assertThat(beans).containsKeys("javaInfoContributor");
Map<String, Object> content = invokeContributor(
this.context.getBean("javaInfoContributor", InfoContributor.class));
Object javaInfo = content.get("java");
assertThat(javaInfo).isInstanceOf(JavaInfo.class);
}

private Map<String, Object> invokeContributor(InfoContributor contributor) {
Info.Builder builder = new Info.Builder();
contributor.contribute(builder);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2021-2021 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.info.java;

/**
* A simple DTO that holds information about the Java environment the application is
* running in.
*
* @author Jonatan Ivanov
* @since 2.6.0
*/
public class JavaInfo {

private final String vendor;

private final String version;

private final JreInfo runtime;

private final VmInfo vm;

public JavaInfo() {
this.vendor = System.getProperty("java.vendor");
this.version = System.getProperty("java.version");
this.runtime = new JreInfo();
this.vm = new VmInfo();
}

public String getVendor() {
return this.vendor;
}

public String getVersion() {
return this.version;
}

public JreInfo getRuntime() {
return this.runtime;
}

public VmInfo getVm() {
return this.vm;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2021-2021 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.info.java;

import org.springframework.boot.actuate.info.Info.Builder;
import org.springframework.boot.actuate.info.InfoContributor;

/**
* An {@link InfoContributor} that exposes {@link JavaInfo}.
*
* @author Jonatan Ivanov
* @since 2.6.0
*/
public class JavaInfoContributor implements InfoContributor {

private final JavaInfo javaInfo;

public JavaInfoContributor() {
this.javaInfo = new JavaInfo();
}

@Override
public void contribute(Builder builder) {
builder.withDetail("java", this.javaInfo);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2021-2021 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.info.java;

/**
* A simple DTO that holds information about the JRE (Java Runtime Environment) the
* application is running in.
*
* @author Jonatan Ivanov
* @since 2.6.0
*/
public class JreInfo {

private final String name;

private final String version;

public JreInfo() {
this.name = System.getProperty("java.runtime.name");
this.version = System.getProperty("java.runtime.version");
}

public String getName() {
return this.name;
}

public String getVersion() {
return this.version;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2021-2021 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.info.java;

/**
* A simple DTO that holds information about the JVM (Java Virtual Machine) the
* application is running in.
*
* @author Jonatan Ivanov
* @since 2.6.0
*/
public class VmInfo {

private final String name;

private final String vendor;

private final String version;

public VmInfo() {
this.name = System.getProperty("java.vm.name");
this.vendor = System.getProperty("java.vm.vendor");
this.version = System.getProperty("java.vm.version");
}

public String getName() {
return this.name;
}

public String getVendor() {
return this.vendor;
}

public String getVersion() {
return this.version;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2021-2021 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.
*/

/**
* Classes for java info.
*/
package org.springframework.boot.actuate.info.java;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2021-2021 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.info.java;

import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.info.Info;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link JavaInfoContributor}
*
* @author Jonatan Ivanov
*/
class JavaInfoContributorTests {

@Test
void javaInfoShouldBeAdded() {
JavaInfoContributor javaInfoContributor = new JavaInfoContributor();
Info.Builder builder = new Info.Builder();
javaInfoContributor.contribute(builder);

assertThat(builder.build().getDetails().get("java")).isInstanceOf(JavaInfo.class);
JavaInfo javaInfo = (JavaInfo) builder.build().getDetails().get("java");

assertThat(javaInfo.getVendor()).isEqualTo(System.getProperty("java.vendor"));
assertThat(javaInfo.getVersion()).isEqualTo(System.getProperty("java.version"));
assertThat(javaInfo.getRuntime().getName()).isEqualTo(System.getProperty("java.runtime.name"));
assertThat(javaInfo.getRuntime().getVersion()).isEqualTo(System.getProperty("java.runtime.version"));
assertThat(javaInfo.getVm().getName()).isEqualTo(System.getProperty("java.vm.name"));
assertThat(javaInfo.getVm().getVendor()).isEqualTo(System.getProperty("java.vm.vendor"));
assertThat(javaInfo.getVm().getVersion()).isEqualTo(System.getProperty("java.vm.version"));
}

}

0 comments on commit fc87da7

Please sign in to comment.