Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support showDetail=false in readiness endpoint #957

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = EmptyConfiguration.class)
@RunWith(SpringRunner.class)
public class EndPointTest {
public class ManualEndPointNoExistTest {
@Autowired
private TestRestTemplate restTemplate;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alipay.sofa.actuator.autoconfigure.test;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Map;

/**
* @author huzijie
* @version ReadinessEndpointTest.java, v 0.1 2022年04月28日 11:11 AM huzijie Exp $
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = EmptyConfiguration.class)
@RunWith(SpringRunner.class)
public class ReadinessEndpointTest {
@Autowired
private TestRestTemplate restTemplate;

@Test
public void test() {
ResponseEntity<HealthResponse> response = restTemplate.getForEntity("/actuator/readiness",
HealthResponse.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
HealthResponse health = response.getBody();
Assert.assertNotNull(health);
Assert.assertNotNull(health.getDetails());

response = restTemplate.getForEntity("/actuator/readiness?showDetail=false",
HealthResponse.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
health = response.getBody();
Assert.assertNotNull(health);
Assert.assertNull(health.getDetails());
}

private static class HealthResponse {

private String status;

private Map<String, Object> details;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Map<String, Object> getDetails() {
return details;
}

public void setDetails(Map<String, Object> details) {
this.details = details;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package com.alipay.sofa.boot.actuator.health;

import org.springframework.boot.actuate.endpoint.SecurityContext;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HttpCodeStatusMapper;
import org.springframework.lang.Nullable;

/**
* @author qilong.zql
Expand All @@ -39,8 +39,8 @@ public ReadinessEndpointWebExtension(SofaBootReadinessEndpoint delegate,
}

@ReadOperation
public WebEndpointResponse<Health> getHealth(SecurityContext securityContext) {
Health result = delegate.health();
public WebEndpointResponse<Health> getHealth(@Nullable String showDetail) {
Health result = delegate.health(showDetail);
return new WebEndpointResponse<>(result, statusMapper.getStatusCode(result.getStatus()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.health.Health;
import org.springframework.lang.Nullable;

/**
* The health check HTTP checker for start status.
Expand All @@ -38,7 +39,11 @@ public SofaBootReadinessEndpoint(ReadinessCheckListener readinessCheckListener)
}

@ReadOperation
public Health health() {
return readinessCheckListener.aggregateReadinessHealth();
public Health health(@Nullable String showDetail) {
Health health = readinessCheckListener.aggregateReadinessHealth();
if (showDetail == null || Boolean.parseBoolean(showDetail)) {
return health;
}
return new Health.Builder(health.getStatus()).build();
}
}