Skip to content

Commit 9426c23

Browse files
committed
Add libraries actuator endpoint
Adds a libraries actuator endpoint that displays details about the libraries used by the application. Provides out-of-the-box support for displaying details about Java libraries that are bundled within the application jar/war archive. The spring-boot-maven-plugin and spring-boot-gradle-plugin now generate a META-INF/bundled-libraries.yaml file when they package the application archive. The file is included in the packaged application archive. At runtime, libraries are contributed to the libraries endpoint via LibrariesContributor beans, similar to how InfoContributers contribute to the info endpoint. spring-boot-actuator-autoconfigure auto-configures a LibrariesContributor that adds the libraries from META-INF/bundled-libraries.yaml to the libraries endpoint. This auto-configuration can be disabled using environment properties similar to how other actuator endpoints behave. An application can provide other LibrariesContributor instances to contribute more libraries to the libraries endpoint. For example, an application might want to contribute libraries dynamically added at runtime. Or an application might want to contribute details about front-end libraries. Each Library is described as a simple set of key/value pairs. For example, Java libraries with coordinates in a Maven repository typically contain entries for `groupId`, `artifactId`, and `version`. Fixes #22924
1 parent 1bd603f commit 9426c23

File tree

36 files changed

+1665
-67
lines changed

36 files changed

+1665
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2021 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.autoconfigure.libraries;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.context.annotation.Conditional;
26+
27+
/**
28+
* {@link Conditional @Conditional} that checks whether or not a default libraries
29+
* contributor is enabled. Matches if the value of the
30+
* {@code management.libraries.<name>.enabled} property is {@code true}. Otherwise,
31+
* matches if the value of the {@code management.libraries.defaults.enabled} property is
32+
* {@code true} or if it is not configured.
33+
*
34+
* @author Phil Clay
35+
* @since 2.5.0
36+
*/
37+
@Retention(RetentionPolicy.RUNTIME)
38+
@Target({ ElementType.TYPE, ElementType.METHOD })
39+
@Documented
40+
@Conditional(OnEnabledLibrariesContributorCondition.class)
41+
public @interface ConditionalOnEnabledLibrariesContributor {
42+
43+
/**
44+
* The name of the libraries contributor.
45+
* @return the name of the libraries contributor
46+
*/
47+
String value();
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2021 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.autoconfigure.libraries;
18+
19+
import java.io.IOException;
20+
import java.util.stream.Collectors;
21+
22+
import org.springframework.beans.factory.ObjectProvider;
23+
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
24+
import org.springframework.boot.actuate.autoconfigure.libraries.LibrariesProperties.Bundled;
25+
import org.springframework.boot.actuate.libraries.BasicLibrariesContributor;
26+
import org.springframework.boot.actuate.libraries.LibrariesContributor;
27+
import org.springframework.boot.actuate.libraries.LibrariesEndpoint;
28+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
31+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.context.annotation.Configuration;
34+
import org.springframework.core.Ordered;
35+
import org.springframework.core.annotation.Order;
36+
37+
/**
38+
* {@link EnableAutoConfiguration Auto-configuration} for the {@link LibrariesEndpoint}.
39+
*
40+
* @author Phil Clay
41+
* @since 2.5.0
42+
*/
43+
@Configuration(proxyBeanMethods = false)
44+
@EnableConfigurationProperties(LibrariesProperties.class)
45+
@ConditionalOnAvailableEndpoint(endpoint = LibrariesEndpoint.class)
46+
public class LibrariesEndpointAutoConfiguration {
47+
48+
/**
49+
* The default order for the core {@link LibrariesContributor} beans.
50+
*/
51+
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
52+
53+
/**
54+
* A {@link LibrariesContributor} that contributes libraries that were bundled in the
55+
* spring boot application at build time, typically by the spring boot maven or gradle
56+
* plugins.
57+
* @param librariesProperties libraries configuration properties
58+
* @return a {@link LibrariesContributor} that contributes libraries that were bundled
59+
* in the spring boot application at build time, typically by the spring boot maven or
60+
* gradle plugins.
61+
* @throws IOException if an exception occurs reading the build libraries yaml file
62+
*/
63+
@Bean
64+
@ConditionalOnEnabledLibrariesContributor("bundled")
65+
@ConditionalOnResource(resources = "${" + LibrariesProperties.CONFIGURATION_PROPERTIES_PREFIX
66+
+ ".bundled.location:classpath:" + Bundled.DEFAULT_CLASSPATH_LOCATION + "}")
67+
@Order(DEFAULT_ORDER)
68+
public LibrariesContributor bundledLibrariesContributor(LibrariesProperties librariesProperties)
69+
throws IOException {
70+
return BasicLibrariesContributor.fromYamlResource("bundled", librariesProperties.getBundled().getLocation());
71+
}
72+
73+
@Bean
74+
@ConditionalOnMissingBean
75+
public LibrariesEndpoint librariesEndpoint(ObjectProvider<LibrariesContributor> librariesContributors) {
76+
return new LibrariesEndpoint(librariesContributors.orderedStream().collect(Collectors.toList()));
77+
}
78+
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2012-2021 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.autoconfigure.libraries;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.core.io.ClassPathResource;
21+
import org.springframework.core.io.Resource;
22+
23+
/**
24+
* Configuration properties for core libraries contributors.
25+
*
26+
* @author Phil Clay
27+
* @since 2.5.0
28+
*/
29+
@ConfigurationProperties(LibrariesProperties.CONFIGURATION_PROPERTIES_PREFIX)
30+
public class LibrariesProperties {
31+
32+
static final String CONFIGURATION_PROPERTIES_PREFIX = "management.libraries";
33+
34+
private final Bundled bundled = new Bundled();
35+
36+
/**
37+
* Gets the properties for exposing the libraries bundled in the application archive,
38+
* typically by the spring boot maven or gradle plugins.
39+
* @return the properties for exposing the libraries bundled in the application
40+
* archive
41+
*/
42+
public Bundled getBundled() {
43+
return this.bundled;
44+
}
45+
46+
/**
47+
* Properties for exposing the libraries bundled in the application archive, typically
48+
* by the spring boot maven or gradle plugins.
49+
*/
50+
public static class Bundled {
51+
52+
/**
53+
* The default classpath location of the {@code bundled-libraries.yaml} file. This
54+
* location is where the spring boot maven and gradle plugins will write the
55+
* {@code bundled-libraries.yaml} file within the jar/war.
56+
*/
57+
public static final String DEFAULT_CLASSPATH_LOCATION = "META-INF/bundled-libraries.yaml";
58+
59+
/**
60+
* Location of the generated {@code bundled-libraries.yaml} file.
61+
*/
62+
private Resource location = new ClassPathResource(DEFAULT_CLASSPATH_LOCATION);
63+
64+
/**
65+
* Gets the location of the generated {@code bundled-libraries.yaml} file.
66+
*
67+
* <p>
68+
* Defaults to {@value #DEFAULT_CLASSPATH_LOCATION}.
69+
* </p>
70+
* @return the location of the generated {@code bundled-libraries.yaml} file.
71+
*/
72+
public Resource getLocation() {
73+
return this.location;
74+
}
75+
76+
/**
77+
* Sets the location of the generated {@code bundled-libraries.yaml} file.
78+
* <p>
79+
* Defaults to {@value #DEFAULT_CLASSPATH_LOCATION}.
80+
* </p>
81+
* @param location the location of the generated {@code bundled-libraries.yaml}
82+
* file.
83+
*/
84+
public void setLocation(Resource location) {
85+
this.location = location;
86+
}
87+
88+
}
89+
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2021 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.autoconfigure.libraries;
18+
19+
import org.springframework.boot.actuate.autoconfigure.OnEndpointElementCondition;
20+
import org.springframework.context.annotation.Condition;
21+
22+
/**
23+
* {@link Condition} that checks if a libraries contributor is enabled.
24+
*
25+
* @author Phil Clay
26+
*/
27+
class OnEnabledLibrariesContributorCondition extends OnEndpointElementCondition {
28+
29+
OnEnabledLibrariesContributorCondition() {
30+
super("management.libraries.", ConditionalOnEnabledLibrariesContributor.class);
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2021 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+
/**
18+
* Auto-configuration for actuator libraries concerns.
19+
*/
20+
package org.springframework.boot.actuate.autoconfigure.libraries;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@
273273
"name": "management.info.git.mode",
274274
"defaultValue": "simple"
275275
},
276+
{
277+
"name": "management.libraries.bundled.enabled",
278+
"type": "java.lang.Boolean",
279+
"description": "Whether the libraries endpoint should show the libraries bundled in the application archive.",
280+
"defaultValue": true
281+
},
276282
{
277283
"name": "management.metrics.binders.files.enabled",
278284
"type": "java.lang.Boolean",

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorA
3333
org.springframework.boot.actuate.autoconfigure.jms.JmsHealthContributorAutoConfiguration,\
3434
org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpointAutoConfiguration,\
3535
org.springframework.boot.actuate.autoconfigure.ldap.LdapHealthContributorAutoConfiguration,\
36+
org.springframework.boot.actuate.autoconfigure.libraries.LibrariesEndpointAutoConfiguration,\
3637
org.springframework.boot.actuate.autoconfigure.liquibase.LiquibaseEndpointAutoConfiguration,\
3738
org.springframework.boot.actuate.autoconfigure.logging.LogFileWebEndpointAutoConfiguration,\
3839
org.springframework.boot.actuate.autoconfigure.logging.LoggersEndpointAutoConfiguration,\

0 commit comments

Comments
 (0)