Skip to content

Commit 820396f

Browse files
jonatan-ivanovmhalbritter
authored andcommitted
Add ProcessInfoContributor
This InfoContributor exposes information about the process of the application. See gh-38371
1 parent 2d12fa0 commit 820396f

File tree

8 files changed

+257
-2
lines changed

8 files changed

+257
-2
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.actuate.info.InfoContributor;
2323
import org.springframework.boot.actuate.info.JavaInfoContributor;
2424
import org.springframework.boot.actuate.info.OsInfoContributor;
25+
import org.springframework.boot.actuate.info.ProcessInfoContributor;
2526
import org.springframework.boot.autoconfigure.AutoConfiguration;
2627
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2728
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -92,4 +93,11 @@ public OsInfoContributor osInfoContributor() {
9293
return new OsInfoContributor();
9394
}
9495

96+
@Bean
97+
@ConditionalOnEnabledInfoContributor(value = "process", fallback = InfoContributorFallback.DISABLE)
98+
@Order(DEFAULT_ORDER)
99+
public ProcessInfoContributor processInfoContributor() {
100+
return new ProcessInfoContributor();
101+
}
102+
95103
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

+6
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@
309309
"description": "Whether to enable Operating System info.",
310310
"defaultValue": false
311311
},
312+
{
313+
"name": "management.info.process.enabled",
314+
"type": "java.lang.Boolean",
315+
"description": "Whether to enable process info.",
316+
"defaultValue": false
317+
},
312318
{
313319
"name": "management.metrics.binders.files.enabled",
314320
"type": "java.lang.Boolean",

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
import org.springframework.boot.actuate.info.InfoContributor;
2929
import org.springframework.boot.actuate.info.JavaInfoContributor;
3030
import org.springframework.boot.actuate.info.OsInfoContributor;
31+
import org.springframework.boot.actuate.info.ProcessInfoContributor;
3132
import org.springframework.boot.autoconfigure.AutoConfigurations;
3233
import org.springframework.boot.info.BuildProperties;
3334
import org.springframework.boot.info.GitProperties;
3435
import org.springframework.boot.info.JavaInfo;
3536
import org.springframework.boot.info.OsInfo;
37+
import org.springframework.boot.info.ProcessInfo;
3638
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3739
import org.springframework.context.annotation.Bean;
3840
import org.springframework.context.annotation.Configuration;
@@ -164,6 +166,16 @@ void osInfoContributor() {
164166
});
165167
}
166168

169+
@Test
170+
void processInfoContributor() {
171+
this.contextRunner.withPropertyValues("management.info.process.enabled=true").run((context) -> {
172+
assertThat(context).hasSingleBean(ProcessInfoContributor.class);
173+
Map<String, Object> content = invokeContributor(context.getBean(ProcessInfoContributor.class));
174+
assertThat(content).containsKey("process");
175+
assertThat(content.get("process")).isInstanceOf(ProcessInfo.class);
176+
});
177+
}
178+
167179
private Map<String, Object> invokeContributor(InfoContributor contributor) {
168180
Info.Builder builder = new Info.Builder();
169181
contributor.contribute(builder);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.info;
18+
19+
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
20+
import org.springframework.aot.hint.RuntimeHints;
21+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
22+
import org.springframework.boot.actuate.info.Info.Builder;
23+
import org.springframework.boot.actuate.info.ProcessInfoContributor.ProcessInfoContributorRuntimeHints;
24+
import org.springframework.boot.info.ProcessInfo;
25+
import org.springframework.context.annotation.ImportRuntimeHints;
26+
27+
/**
28+
* An {@link InfoContributor} that exposes {@link ProcessInfo}.
29+
*
30+
* @author Jonatan Ivanov
31+
* @since 3.3.0
32+
*/
33+
@ImportRuntimeHints(ProcessInfoContributorRuntimeHints.class)
34+
public class ProcessInfoContributor implements InfoContributor {
35+
36+
private final ProcessInfo processInfo;
37+
38+
public ProcessInfoContributor() {
39+
this.processInfo = new ProcessInfo();
40+
}
41+
42+
@Override
43+
public void contribute(Builder builder) {
44+
builder.withDetail("process", this.processInfo);
45+
}
46+
47+
static class ProcessInfoContributorRuntimeHints implements RuntimeHintsRegistrar {
48+
49+
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
50+
51+
@Override
52+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
53+
this.bindingRegistrar.registerReflectionHints(hints.reflection(), ProcessInfo.class);
54+
}
55+
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.info;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aot.hint.MemberCategory;
22+
import org.springframework.aot.hint.RuntimeHints;
23+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
24+
import org.springframework.boot.actuate.info.ProcessInfoContributor.ProcessInfoContributorRuntimeHints;
25+
import org.springframework.boot.info.ProcessInfo;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link ProcessInfoContributor}.
31+
*
32+
* @author Jonatan Ivanov
33+
*/
34+
class ProcessInfoContributorTests {
35+
36+
@Test
37+
void processInfoShouldBeAdded() {
38+
ProcessInfoContributor processInfoContributor = new ProcessInfoContributor();
39+
Info.Builder builder = new Info.Builder();
40+
processInfoContributor.contribute(builder);
41+
Info info = builder.build();
42+
assertThat(info.get("process")).isInstanceOf(ProcessInfo.class);
43+
}
44+
45+
@Test
46+
void shouldRegisterHints() {
47+
RuntimeHints runtimeHints = new RuntimeHints();
48+
new ProcessInfoContributorRuntimeHints().registerHints(runtimeHints, getClass().getClassLoader());
49+
assertThat(RuntimeHintsPredicates.reflection()
50+
.onType(ProcessInfo.class)
51+
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS))
52+
.accepts(runtimeHints);
53+
}
54+
55+
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc

+12-1
Original file line numberDiff line numberDiff line change
@@ -1087,12 +1087,17 @@ When appropriate, Spring auto-configures the following `InfoContributor` beans:
10871087
| Exposes Operating System information.
10881088
| None.
10891089

1090+
| `process`
1091+
| {spring-boot-actuator-module-code}/info/ProcessInfoContributor.java[`ProcessInfoContributor`]
1092+
| Exposes process information.
1093+
| None.
1094+
10901095
|===
10911096

10921097
Whether an individual contributor is enabled is controlled by its `management.info.<id>.enabled` property.
10931098
Different contributors have different defaults for this property, depending on their prerequisites and the nature of the information that they expose.
10941099

1095-
With no prerequisites to indicate that they should be enabled, the `env`, `java`, and `os` contributors are disabled by default.
1100+
With no prerequisites to indicate that they should be enabled, the `env`, `java`, `os`, and `process` contributors are disabled by default.
10961101
Each can be enabled by setting its `management.info.<id>.enabled` property to `true`.
10971102

10981103
The `build` and `git` info contributors are enabled by default.
@@ -1190,6 +1195,12 @@ The `info` endpoint publishes information about your Operating System, see {spri
11901195

11911196

11921197

1198+
[[actuator.endpoints.info.process-information]]
1199+
==== Process Information
1200+
The `info` endpoint publishes information about your process, see {spring-boot-module-api}/info/ProcessInfo.html[`Process`] for more details.
1201+
1202+
1203+
11931204
[[actuator.endpoints.info.writing-custom-info-contributors]]
11941205
==== Writing Custom InfoContributors
11951206
To provide custom application information, you can register Spring beans that implement the {spring-boot-actuator-module-code}/info/InfoContributor.java[`InfoContributor`] interface.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.info;
18+
19+
/**
20+
* Information about the process of the application.
21+
*
22+
* @author Jonatan Ivanov
23+
* @since 3.3.0
24+
*/
25+
public class ProcessInfo {
26+
27+
private static final Runtime runtime = Runtime.getRuntime();
28+
29+
private final long pid;
30+
31+
private final long parentPid;
32+
33+
private final String owner;
34+
35+
public ProcessInfo() {
36+
ProcessHandle process = ProcessHandle.current();
37+
this.pid = process.pid();
38+
this.parentPid = process.parent().map(ProcessHandle::pid).orElse(-1L);
39+
this.owner = process.info().user().orElse(null);
40+
}
41+
42+
/**
43+
* Number of processors available to the process. This value may change between
44+
* invocations especially in (containerized) environments where resource usage can be
45+
* isolated (for example using control groups).
46+
* @return result of {@link Runtime#availableProcessors()}
47+
* @see Runtime#availableProcessors()
48+
*/
49+
public int getCpus() {
50+
return runtime.availableProcessors();
51+
}
52+
53+
public long getPid() {
54+
return this.pid;
55+
}
56+
57+
public long getParentPid() {
58+
return this.parentPid;
59+
}
60+
61+
public String getOwner() {
62+
return this.owner;
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.info;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests for {@link ProcessInfo}.
25+
*
26+
* @author Jonatan Ivanov
27+
*/
28+
class ProcessInfoTests {
29+
30+
@Test
31+
void processInfoIsAvailable() {
32+
ProcessInfo processInfo = new ProcessInfo();
33+
assertThat(processInfo.getCpus()).isEqualTo(Runtime.getRuntime().availableProcessors());
34+
assertThat(processInfo.getOwner()).isEqualTo(ProcessHandle.current().info().user().orElse(null));
35+
assertThat(processInfo.getPid()).isEqualTo(ProcessHandle.current().pid());
36+
assertThat(processInfo.getParentPid())
37+
.isEqualTo(ProcessHandle.current().parent().map(ProcessHandle::pid).orElse(null));
38+
}
39+
40+
}

0 commit comments

Comments
 (0)