Search in sources :

Example 6 with StatusInfo

use of de.codecentric.boot.admin.server.domain.values.StatusInfo in project spring-boot-admin by codecentric.

the class PagerdutyNotifierTest method test_onApplicationEvent_trigger.

@Test
public void test_onApplicationEvent_trigger() {
    StepVerifier.create(notifier.notify(new InstanceStatusChangedEvent(INSTANCE.getId(), INSTANCE.getVersion() + 1, StatusInfo.ofUp()))).verifyComplete();
    reset(restTemplate);
    StatusInfo down = StatusInfo.ofDown();
    when(repository.find(INSTANCE.getId())).thenReturn(Mono.just(INSTANCE.withStatusInfo(down)));
    StepVerifier.create(notifier.notify(new InstanceStatusChangedEvent(INSTANCE.getId(), INSTANCE.getVersion() + 2, down))).verifyComplete();
    Map<String, Object> expected = new HashMap<>();
    expected.put("service_key", "--service--");
    expected.put("incident_key", "App/-id-");
    expected.put("event_type", "trigger");
    expected.put("description", "App/-id- is DOWN");
    expected.put("client", "TestClient");
    expected.put("client_url", URI.create("http://localhost"));
    Map<String, Object> details = new HashMap<>();
    details.put("from", "UP");
    details.put("to", down);
    expected.put("details", details);
    Map<String, Object> context = new HashMap<>();
    context.put("type", "link");
    context.put("href", "http://health");
    context.put("text", "Application health-endpoint");
    expected.put("contexts", Arrays.asList(context));
    verify(restTemplate).postForEntity(eq(PagerdutyNotifier.DEFAULT_URI), eq(expected), eq(Void.class));
}
Also used : StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) HashMap(java.util.HashMap) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Test(org.junit.jupiter.api.Test)

Example 7 with StatusInfo

use of de.codecentric.boot.admin.server.domain.values.StatusInfo in project spring-boot-admin by codecentric.

the class Instance method apply.

private Instance apply(InstanceEvent event, boolean isNewEvent) {
    Assert.notNull(event, "'event' must not be null");
    Assert.isTrue(this.id.equals(event.getInstance()), "'event' must refer the same instance");
    Assert.isTrue(event.getVersion() >= this.nextVersion(), () -> "Event " + event.getVersion() + " must be greater or equal to " + this.nextVersion());
    List<InstanceEvent> unsavedEvents = appendToEvents(event, isNewEvent);
    if (event instanceof InstanceRegisteredEvent) {
        Registration registration = ((InstanceRegisteredEvent) event).getRegistration();
        return new Instance(this.id, event.getVersion(), registration, true, StatusInfo.ofUnknown(), event.getTimestamp(), Info.empty(), Endpoints.empty(), updateBuildVersion(registration.getMetadata()), updateTags(registration.getMetadata()), unsavedEvents);
    } else if (event instanceof InstanceRegistrationUpdatedEvent) {
        Registration registration = ((InstanceRegistrationUpdatedEvent) event).getRegistration();
        return new Instance(this.id, event.getVersion(), registration, this.registered, this.statusInfo, this.statusTimestamp, this.info, this.endpoints, updateBuildVersion(registration.getMetadata(), this.info.getValues()), updateTags(registration.getMetadata(), this.info.getValues()), unsavedEvents);
    } else if (event instanceof InstanceStatusChangedEvent) {
        StatusInfo statusInfo = ((InstanceStatusChangedEvent) event).getStatusInfo();
        return new Instance(this.id, event.getVersion(), this.registration, this.registered, statusInfo, event.getTimestamp(), this.info, this.endpoints, this.buildVersion, this.tags, unsavedEvents);
    } else if (event instanceof InstanceEndpointsDetectedEvent) {
        Endpoints endpoints = ((InstanceEndpointsDetectedEvent) event).getEndpoints();
        return new Instance(this.id, event.getVersion(), this.registration, this.registered, this.statusInfo, this.statusTimestamp, this.info, endpoints, this.buildVersion, this.tags, unsavedEvents);
    } else if (event instanceof InstanceInfoChangedEvent) {
        Info info = ((InstanceInfoChangedEvent) event).getInfo();
        Map<String, ?> metaData = (this.registration != null) ? this.registration.getMetadata() : emptyMap();
        return new Instance(this.id, event.getVersion(), this.registration, this.registered, this.statusInfo, this.statusTimestamp, info, this.endpoints, updateBuildVersion(metaData, info.getValues()), updateTags(metaData, info.getValues()), unsavedEvents);
    } else if (event instanceof InstanceDeregisteredEvent) {
        return new Instance(this.id, event.getVersion(), this.registration, false, StatusInfo.ofUnknown(), event.getTimestamp(), Info.empty(), Endpoints.empty(), null, Tags.empty(), unsavedEvents);
    }
    return this;
}
Also used : InstanceRegisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent) InstanceInfoChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent) InstanceEndpointsDetectedEvent(de.codecentric.boot.admin.server.domain.events.InstanceEndpointsDetectedEvent) Info(de.codecentric.boot.admin.server.domain.values.Info) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) InstanceRegistrationUpdatedEvent(de.codecentric.boot.admin.server.domain.events.InstanceRegistrationUpdatedEvent) InstanceEvent(de.codecentric.boot.admin.server.domain.events.InstanceEvent) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Endpoints(de.codecentric.boot.admin.server.domain.values.Endpoints) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) Registration(de.codecentric.boot.admin.server.domain.values.Registration) InstanceDeregisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent)

Example 8 with StatusInfo

use of de.codecentric.boot.admin.server.domain.values.StatusInfo in project spring-boot-admin by codecentric.

the class StatusUpdater method convertStatusInfo.

protected Mono<StatusInfo> convertStatusInfo(ClientResponse response) {
    Boolean hasCompatibleContentType = response.headers().contentType().map((mt) -> mt.isCompatibleWith(MediaType.APPLICATION_JSON) || mt.isCompatibleWith(ACTUATOR_V2_MEDIATYPE)).orElse(false);
    StatusInfo statusInfoFromStatus = this.getStatusInfoFromStatus(response.statusCode(), emptyMap());
    if (hasCompatibleContentType) {
        return response.bodyToMono(RESPONSE_TYPE).map((body) -> {
            if (body.get("status") instanceof String) {
                return StatusInfo.from(body);
            }
            return getStatusInfoFromStatus(response.statusCode(), body);
        }).defaultIfEmpty(statusInfoFromStatus);
    }
    return response.releaseBody().then(Mono.just(statusInfoFromStatus));
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) Logger(org.slf4j.Logger) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MediaType(org.springframework.http.MediaType) InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) LoggerFactory(org.slf4j.LoggerFactory) Endpoint(de.codecentric.boot.admin.server.domain.values.Endpoint) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) Level(java.util.logging.Level) LinkedHashMap(java.util.LinkedHashMap) Instance(de.codecentric.boot.admin.server.domain.entities.Instance) HttpStatus(org.springframework.http.HttpStatus) Map(java.util.Map) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) ACTUATOR_V2_MEDIATYPE(de.codecentric.boot.admin.server.utils.MediaType.ACTUATOR_V2_MEDIATYPE) InstanceWebClient(de.codecentric.boot.admin.server.web.client.InstanceWebClient) InstanceRepository(de.codecentric.boot.admin.server.domain.entities.InstanceRepository) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo)

Example 9 with StatusInfo

use of de.codecentric.boot.admin.server.domain.values.StatusInfo in project spring-boot-admin by codecentric.

the class TelegramNotifierTest method test_onApplicationEvent_trigger.

@Test
public void test_onApplicationEvent_trigger() {
    StatusInfo infoDown = StatusInfo.ofDown();
    @SuppressWarnings("unchecked") ArgumentCaptor<HttpEntity<Map<String, Object>>> httpRequest = ArgumentCaptor.forClass((Class<HttpEntity<Map<String, Object>>>) (Class<?>) HttpEntity.class);
    when(restTemplate.postForEntity(isA(String.class), httpRequest.capture(), eq(Void.class))).thenReturn(ResponseEntity.ok().build());
    StepVerifier.create(notifier.notify(new InstanceStatusChangedEvent(instance.getId(), instance.getVersion(), StatusInfo.ofUp()))).verifyComplete();
    StepVerifier.create(notifier.notify(new InstanceStatusChangedEvent(instance.getId(), instance.getVersion(), infoDown))).verifyComplete();
    verify(restTemplate).getForObject(eq("https://telegram.com/bot--token-/sendmessage?chat_id={chat_id}&text={text}" + "&parse_mode={parse_mode}&disable_notification={disable_notification}"), eq(Void.class), eq(getParameters("DOWN")));
}
Also used : HttpEntity(org.springframework.http.HttpEntity) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Test(org.junit.jupiter.api.Test)

Example 10 with StatusInfo

use of de.codecentric.boot.admin.server.domain.values.StatusInfo in project spring-boot-admin by codecentric.

the class InstanceStatusChangedEventMixinTest method verifyDeserialize.

@Test
public void verifyDeserialize() throws JSONException, JsonProcessingException {
    String json = new JSONObject().put("instance", "test123").put("version", 12345678L).put("timestamp", 1587751031.000000000).put("type", "STATUS_CHANGED").put("statusInfo", new JSONObject().put("status", "OFFLINE").put("details", new JSONObject().put("foo", "bar"))).toString();
    InstanceStatusChangedEvent event = objectMapper.readValue(json, InstanceStatusChangedEvent.class);
    assertThat(event).isNotNull();
    assertThat(event.getInstance()).isEqualTo(InstanceId.of("test123"));
    assertThat(event.getVersion()).isEqualTo(12345678L);
    assertThat(event.getTimestamp()).isEqualTo(Instant.ofEpochSecond(1587751031).truncatedTo(ChronoUnit.SECONDS));
    StatusInfo statusInfo = event.getStatusInfo();
    assertThat(statusInfo).isNotNull();
    assertThat(statusInfo.getStatus()).isEqualTo("OFFLINE");
    assertThat(statusInfo.getDetails()).containsOnly(entry("foo", "bar"));
}
Also used : JSONObject(org.json.JSONObject) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Test(org.junit.jupiter.api.Test)

Aggregations

StatusInfo (de.codecentric.boot.admin.server.domain.values.StatusInfo)13 Test (org.junit.jupiter.api.Test)11 InstanceStatusChangedEvent (de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent)9 InstanceId (de.codecentric.boot.admin.server.domain.values.InstanceId)4 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 Instance (de.codecentric.boot.admin.server.domain.entities.Instance)2 InstanceRepository (de.codecentric.boot.admin.server.domain.entities.InstanceRepository)2 Info (de.codecentric.boot.admin.server.domain.values.Info)2 Registration (de.codecentric.boot.admin.server.domain.values.Registration)2 Instant (java.time.Instant)2 HttpEntity (org.springframework.http.HttpEntity)2 EventsourcingInstanceRepository (de.codecentric.boot.admin.server.domain.entities.EventsourcingInstanceRepository)1 InstanceDeregisteredEvent (de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent)1 InstanceEndpointsDetectedEvent (de.codecentric.boot.admin.server.domain.events.InstanceEndpointsDetectedEvent)1 InstanceEvent (de.codecentric.boot.admin.server.domain.events.InstanceEvent)1 InstanceInfoChangedEvent (de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent)1 InstanceRegisteredEvent (de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent)1 InstanceRegistrationUpdatedEvent (de.codecentric.boot.admin.server.domain.events.InstanceRegistrationUpdatedEvent)1 Endpoint (de.codecentric.boot.admin.server.domain.values.Endpoint)1