Skip to content

Commit

Permalink
feat(#2528): implement instance filter (#3580)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteKoe authored Aug 16, 2024
1 parent 91b4609 commit e30447d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 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.
Expand Down Expand Up @@ -62,7 +62,7 @@ public class InstanceDiscoveryListenerTest {
public void setup() {
this.discovery = mock(DiscoveryClient.class);
InstanceRepository repository = new EventsourcingInstanceRepository(new InMemoryEventStore());
this.registry = spy(new InstanceRegistry(repository, new HashingInstanceUrlIdGenerator()));
this.registry = spy(new InstanceRegistry(repository, new HashingInstanceUrlIdGenerator(), (instance) -> true));
this.listener = new InstanceDiscoveryListener(this.discovery, this.registry, repository);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import de.codecentric.boot.admin.server.services.HashingInstanceUrlIdGenerator;
import de.codecentric.boot.admin.server.services.InfoUpdateTrigger;
import de.codecentric.boot.admin.server.services.InfoUpdater;
import de.codecentric.boot.admin.server.services.InstanceFilter;
import de.codecentric.boot.admin.server.services.InstanceIdGenerator;
import de.codecentric.boot.admin.server.services.InstanceRegistry;
import de.codecentric.boot.admin.server.services.StatusUpdateTrigger;
Expand All @@ -69,11 +70,17 @@ public AdminServerAutoConfiguration(AdminServerProperties adminServerProperties)
this.adminServerProperties = adminServerProperties;
}

@Bean
@ConditionalOnMissingBean
public InstanceFilter instanceFilter() {
return (instance) -> true;
}

@Bean
@ConditionalOnMissingBean
public InstanceRegistry instanceRegistry(InstanceRepository instanceRepository,
InstanceIdGenerator instanceIdGenerator) {
return new InstanceRegistry(instanceRepository, instanceIdGenerator);
InstanceIdGenerator instanceIdGenerator, InstanceFilter instanceFilter) {
return new InstanceRegistry(instanceRepository, instanceIdGenerator, instanceFilter);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2014-2024 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 de.codecentric.boot.admin.server.services;

import de.codecentric.boot.admin.server.domain.entities.Instance;

/**
* Interface that is applied to InstanceRegistry and returns Instances matching the
* filter, only. Default implementation is to return all instances.
*
* @author dzahbarov
* @see de.codecentric.boot.admin.server.config.AdminServerAutoConfiguration#instanceFilter()
*/
@FunctionalInterface
public interface InstanceFilter {

boolean filter(Instance instance);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@

/**
* Registry for all application instances that should be managed/administrated by the
* Spring Boot Admin server. Backed by an InstanceRepository for persistence and an
* InstanceIdGenerator for id generation.
* Spring Boot Admin server. Backed by an InstanceRepository for persistence, an
* InstanceIdGenerator for id generation and InstanceFilter for instance filtering.
*/
public class InstanceRegistry {

private final InstanceRepository repository;

private final InstanceIdGenerator generator;

public InstanceRegistry(InstanceRepository repository, InstanceIdGenerator generator) {
private final InstanceFilter filter;

public InstanceRegistry(InstanceRepository repository, InstanceIdGenerator generator, InstanceFilter filter) {
this.repository = repository;
this.generator = generator;
this.filter = filter;
}

/**
Expand All @@ -59,20 +62,20 @@ public Mono<InstanceId> register(Registration registration) {
}

/**
* Get a list of all registered instances.
* @return list of all instances.
* Get a list of all registered instances that satisfy the filter.
* @return list of all instances satisfying the filter.
*/
public Flux<Instance> getInstances() {
return repository.findAll();
return repository.findAll().filter(filter::filter);
}

/**
* Get a list of all registered application instances.
* Get a list of all registered application instances that satisfy the filter.
* @param name the name to search for.
* @return list of instances for the given application
* @return list of instances for the given application that satisfy the filter.
*/
public Flux<Instance> getInstances(String name) {
return repository.findByName(name);
return repository.findByName(name).filter(filter::filter);
}

/**
Expand All @@ -81,7 +84,7 @@ public Flux<Instance> getInstances(String name) {
* @return a Mono with the Instance.
*/
public Mono<Instance> getInstance(InstanceId id) {
return repository.find(id);
return repository.find(id).filter(filter::filter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 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.
Expand All @@ -17,6 +17,7 @@
package de.codecentric.boot.admin.server.services;

import java.util.ArrayList;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -47,7 +48,10 @@ public class InstanceRegistryTest {
public void setUp() {
repository = new EventsourcingInstanceRepository(new InMemoryEventStore());
idGenerator = new HashingInstanceUrlIdGenerator();
registry = new InstanceRegistry(repository, idGenerator);
registry = new InstanceRegistry(repository, idGenerator, (instance) -> {
Map<String, String> metadata = instance.getRegistration().getMetadata();
return !metadata.containsKey("displayed") || !metadata.get("displayed").equals("false");
});
}

@Test
Expand Down Expand Up @@ -118,4 +122,19 @@ public void findByName() {
.verifyComplete();
}

@Test
public void findByNameAndFilter() {
InstanceId id1 = registry.register(Registration.create("abc", "http://localhost:8080/health").build()).block();
registry
.register(Registration.create("abc", "http://localhost:8081/health").metadata("displayed", "false").build())
.block();

StepVerifier.create(registry.getInstances("abc"))
.recordWith(ArrayList::new)
.thenConsumeWhile((a) -> true)
.consumeRecordedWith(
(applications) -> assertThat(applications.stream().map(Instance::getId)).containsExactly(id1))
.verifyComplete();
}

}

0 comments on commit e30447d

Please sign in to comment.