Commit 393d3da5 by Johannes Edmeier

Add MetadataContirbutor

in order to easily contribute programmatically to the metadata this commit adds an MetadataContributor as extionsion point.
parent 6a0c7b78
......@@ -21,8 +21,14 @@ import de.codecentric.boot.admin.client.registration.ApplicationRegistrator;
import de.codecentric.boot.admin.client.registration.DefaultApplicationFactory;
import de.codecentric.boot.admin.client.registration.RegistrationApplicationListener;
import de.codecentric.boot.admin.client.registration.ServletApplicationFactory;
import de.codecentric.boot.admin.client.registration.metadata.CompositeMetadataContributor;
import de.codecentric.boot.admin.client.registration.metadata.MetadataContributor;
import de.codecentric.boot.admin.client.registration.metadata.StartupDateMetadataContributor;
import java.util.Collections;
import java.util.List;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration;
......@@ -37,6 +43,7 @@ import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.TaskScheduler;
......@@ -61,9 +68,10 @@ public class SpringBootAdminClientAutoConfiguration {
ServerProperties server,
ServletContext servletContext,
PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint) {
WebEndpointProperties webEndpoint,
MetadataContributor metadataContributor) {
return new ServletApplicationFactory(instance, management, server, servletContext, pathMappedEndpoints,
webEndpoint);
webEndpoint, metadataContributor);
}
}
......@@ -76,8 +84,10 @@ public class SpringBootAdminClientAutoConfiguration {
ManagementServerProperties management,
ServerProperties server,
PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint) {
return new DefaultApplicationFactory(instance, management, server, pathMappedEndpoints, webEndpoint);
WebEndpointProperties webEndpoint,
MetadataContributor metadataContributor) {
return new DefaultApplicationFactory(instance, management, server, pathMappedEndpoints, webEndpoint,
metadataContributor);
}
}
......@@ -102,8 +112,10 @@ public class SpringBootAdminClientAutoConfiguration {
ManagementServerProperties management,
ServerProperties server,
PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint) {
return new DefaultApplicationFactory(instance, management, server, pathMappedEndpoints, webEndpoint);
WebEndpointProperties webEndpoint,
MetadataContributor metadataContributor) {
return new DefaultApplicationFactory(instance, management, server, pathMappedEndpoints, webEndpoint,
metadataContributor);
}
@Bean
......@@ -127,4 +139,18 @@ public class SpringBootAdminClientAutoConfiguration {
listener.setRegisterPeriod(client.getPeriod());
return listener;
}
@Bean
@Primary
@ConditionalOnMissingBean
public CompositeMetadataContributor metadataContributor(ObjectProvider<List<MetadataContributor>> contributorProvider) {
List<MetadataContributor> contributors = contributorProvider.getIfAvailable(Collections::emptyList);
return new CompositeMetadataContributor(contributors);
}
@Bean
@ConditionalOnMissingBean
public StartupDateMetadataContributor startupDateMetadataContributor() {
return new StartupDateMetadataContributor();
}
}
......@@ -17,11 +17,10 @@
package de.codecentric.boot.admin.client.registration;
import de.codecentric.boot.admin.client.config.InstanceProperties;
import de.codecentric.boot.admin.client.registration.metadata.MetadataContributor;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
......@@ -47,7 +46,7 @@ public class DefaultApplicationFactory implements ApplicationFactory {
private final ManagementServerProperties management;
private final PathMappedEndpoints pathMappedEndpoints;
private final WebEndpointProperties webEndpoint;
private final OffsetDateTime timestamp;
private final MetadataContributor metadataContributor;
private Integer localServerPort;
private Integer localManagementPort;
......@@ -56,13 +55,14 @@ public class DefaultApplicationFactory implements ApplicationFactory {
ManagementServerProperties management,
ServerProperties server,
PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint) {
WebEndpointProperties webEndpoint,
MetadataContributor metadataContributor) {
this.instance = instance;
this.management = management;
this.server = server;
this.pathMappedEndpoints = pathMappedEndpoints;
this.webEndpoint = webEndpoint;
this.timestamp = OffsetDateTime.now();
this.metadataContributor = metadataContributor;
}
@Override
......@@ -162,15 +162,11 @@ public class DefaultApplicationFactory implements ApplicationFactory {
}
protected Map<String, String> getMetadata() {
if (instance.getMetadata().containsKey("startup")) {
return instance.getMetadata();
} else {
Map<String, String> metadata = new LinkedHashMap<>();
metadata.put("startup", this.timestamp.format(DateTimeFormatter.ISO_DATE_TIME));
metadata.putAll(metadataContributor.getMetadata());
metadata.putAll(instance.getMetadata());
return metadata;
}
}
protected String getServiceHost() {
InetAddress address = server.getAddress();
......
......@@ -17,6 +17,7 @@
package de.codecentric.boot.admin.client.registration;
import de.codecentric.boot.admin.client.config.InstanceProperties;
import de.codecentric.boot.admin.client.registration.metadata.MetadataContributor;
import javax.servlet.ServletContext;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
......@@ -35,8 +36,9 @@ public class ServletApplicationFactory extends DefaultApplicationFactory {
ServerProperties server,
ServletContext servletContext,
PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint) {
super(instance, management, server, pathMappedEndpoints, webEndpoint);
WebEndpointProperties webEndpoint,
MetadataContributor metadataContributor) {
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor);
this.servletContext = servletContext;
this.servlet = server.getServlet();
this.managementServlet = management.getServlet();
......
/*
* Copyright 2014-2018 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
*
* http://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 de.codecentric.boot.admin.client.registration.metadata;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class CompositeMetadataContributor implements MetadataContributor {
private final List<MetadataContributor> delegates;
public CompositeMetadataContributor(List<MetadataContributor> delegates) {
this.delegates = delegates;
}
@Override
public Map<String, String> getMetadata() {
Map<String, String> metadata = new LinkedHashMap<>();
delegates.forEach(delegate -> metadata.putAll(delegate.getMetadata()));
return metadata;
}
}
/*
* Copyright 2014-2018 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
*
* http://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 de.codecentric.boot.admin.client.registration.metadata;
import java.util.Map;
@FunctionalInterface
public interface MetadataContributor {
Map<String, String> getMetadata();
}
/*
* Copyright 2014-2018 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
*
* http://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 de.codecentric.boot.admin.client.registration.metadata;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import static java.util.Collections.singletonMap;
public class StartupDateMetadataContributor implements MetadataContributor {
private final OffsetDateTime timestamp = OffsetDateTime.now();
@Override
public Map<String, String> getMetadata() {
return singletonMap("startup", this.timestamp.format(DateTimeFormatter.ISO_DATE_TIME));
}
}
......@@ -31,8 +31,10 @@ import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.server.Ssl;
import org.springframework.boot.web.server.WebServer;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
......@@ -44,7 +46,7 @@ public class DefaultApplicationFactoryTest {
private WebEndpointProperties webEndpoint = new WebEndpointProperties();
private DefaultApplicationFactory factory = new DefaultApplicationFactory(instanceProperties, management, server,
pathMappedEndpoints, webEndpoint);
pathMappedEndpoints, webEndpoint, () -> singletonMap("contributor", "test"));
@Before
public void setup() {
......@@ -65,6 +67,7 @@ public class DefaultApplicationFactoryTest {
@Test
public void test_default() {
instanceProperties.setMetadata(singletonMap("instance", "test"));
when(pathMappedEndpoints.getPath("health")).thenReturn("/actuator/health");
publishApplicationReadyEvent(factory, 8080, null);
......@@ -72,6 +75,8 @@ public class DefaultApplicationFactoryTest {
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8080/actuator");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8080/actuator/health");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/");
assertThat(app.getMetadata()).containsExactly(entry("contributor", "test"), entry("instance", "test"));
}
@Test
......
......@@ -20,6 +20,7 @@ import de.codecentric.boot.admin.client.config.InstanceProperties;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
......@@ -43,7 +44,7 @@ public class ServletApplicationFactoryTest {
private PathMappedEndpoints pathMappedEndpoints = mock(PathMappedEndpoints.class);
private WebEndpointProperties webEndpoint = new WebEndpointProperties();
private ServletApplicationFactory factory = new ServletApplicationFactory(instance, management, server,
servletContext, pathMappedEndpoints, webEndpoint);
servletContext, pathMappedEndpoints, webEndpoint, Collections::emptyMap);
@Before
public void setup() {
......
/*
* Copyright 2014-2018 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
*
* http://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 de.codecentric.boot.admin.client.registration.metadata;
import java.util.Map;
import org.junit.Test;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
public class CompositeMetadataContributorTest {
@Test
public void should_merge_metadata() {
CompositeMetadataContributor contributor = new CompositeMetadataContributor(
asList(() -> singletonMap("a", "first"), () -> singletonMap("b", "second"),
() -> singletonMap("b", "second-new")));
Map<String, String> metadata = contributor.getMetadata();
assertThat(metadata).containsExactly(entry("a", "first"), entry("b", "second-new"));
}
@Test
public void should_return_empty_metadata() {
CompositeMetadataContributor contributor = new CompositeMetadataContributor(emptyList());
Map<String, String> metadata = contributor.getMetadata();
assertThat(metadata).isEmpty();
}
}
/*
* Copyright 2014-2018 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
*
* http://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 de.codecentric.boot.admin.client.registration.metadata;
import java.util.Map;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class StartupDateMetadataContributorTest {
@Test
public void should_return_startupdate() {
StartupDateMetadataContributor contributor = new StartupDateMetadataContributor();
Map<String, String> metadata = contributor.getMetadata();
assertThat(metadata).hasSize(1).hasEntrySatisfying("startup", (value) -> assertThat(value).isNotEmpty());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment