Skip to content

Commit fac8305

Browse files
authored
feat: Add health check endpoint (#776)
Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com>
1 parent 3ea64d5 commit fac8305

File tree

6 files changed

+191
-2
lines changed

6 files changed

+191
-2
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Health Check API
2+
3+
The License Pre-Validation Service (LPVS) provides a health check endpoint to monitor the status of the service, specifically to check the current load on the processing queue.
4+
5+
## Endpoint
6+
7+
- **URL:** `/health`
8+
- **Method:** `GET`
9+
10+
## Description
11+
12+
This endpoint allows you to check the health of the LPVS service by retrieving the current number of items in the processing queue. This can be used for monitoring purposes to ensure the service is running and to gauge its current workload.
13+
14+
A high or continuously growing queue length might indicate that the service is under heavy load or that there are issues with processing items.
15+
16+
## Response
17+
18+
The endpoint returns a JSON object with a single key, `queueLength`, which represents the total number of items currently in the processing queue (persisted in the database).
19+
20+
### Success Response
21+
22+
- **Code:** `200 OK`
23+
- **Content:**
24+
```json
25+
{
26+
"queueLength": 5
27+
}
28+
```
29+
30+
### Response Schema
31+
32+
| Key | Type | Description |
33+
|-------------|--------|--------------------------------------------------|
34+
| `queueLength` | integer| The current number of items in the processing queue. |
35+
36+
## Example Usage
37+
38+
You can check the service health using a tool like `curl`:
39+
40+
```bash
41+
curl -X GET http://localhost:8080/health
42+
```
43+
44+
**Example Output:**
45+
46+
```json
47+
{
48+
"queueLength": 0
49+
}
50+
```
51+
52+
This output indicates that the service is running and there are currently no items in the processing queue.

doc/lpvs-api.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ paths:
115115
schema:
116116
$ref: '#/components/schemas/WebhookResponseForbidden'
117117

118+
/health:
119+
get:
120+
tags:
121+
- Health Check API
122+
summary: Service Health Check
123+
description: Endpoint for checking the health of the service. Returns the current number of items in the processing queue.
124+
responses:
125+
'200':
126+
description: 200 OK
127+
content:
128+
application/json:
129+
schema:
130+
$ref: '#/components/schemas/HealthResponse'
131+
118132
components:
119133
schemas:
120134
WebhookRequest:
@@ -182,3 +196,12 @@ components:
182196
type: string
183197
format: uuid
184198
example: Error
199+
200+
HealthResponse:
201+
type: object
202+
properties:
203+
queueLength:
204+
type: integer
205+
format: int64
206+
description: The current number of items in the processing queue.
207+
example: 5

doc/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ nav:
3636
- Service mode:
3737
- GitHub webhook configuration: user-guide/service/webhook.md
3838
- Run service using pre-built Docker image: user-guide/service/docker.md
39+
- Health check: user-guide/service/health.md
3940
- Build and run service from the source code:
4041
- Prerequisites:
4142
- Scanner installation: user-guide/service/scanner.md
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) 2025, Samsung Electronics Co., Ltd. All rights reserved.
3+
*
4+
* Use of this source code is governed by a MIT license that can be
5+
* found in the LICENSE file.
6+
*/
7+
package com.lpvs.controller;
8+
9+
import com.lpvs.repository.LPVSQueueRepository;
10+
import lombok.extern.slf4j.Slf4j;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.http.ResponseEntity;
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RestController;
16+
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
20+
/**
21+
* Controller for handling health check requests.
22+
* This class provides an endpoint to check the health of the service,
23+
* specifically returning the current length of the processing queue.
24+
*/
25+
@RestController
26+
@RequestMapping("/health")
27+
@Slf4j
28+
public class HealthController {
29+
30+
/**
31+
* Repository for accessing LPVSQueue entities to determine queue size.
32+
*/
33+
private final LPVSQueueRepository queueRepository;
34+
35+
/**
36+
* Constructor for HealthController.
37+
*
38+
* @param queueRepository Repository for accessing LPVSQueue entities.
39+
*/
40+
@Autowired
41+
public HealthController(LPVSQueueRepository queueRepository) {
42+
this.queueRepository = queueRepository;
43+
}
44+
45+
/**
46+
* Endpoint for checking the health of the service.
47+
* Returns the current number of items in the processing queue.
48+
*
49+
* @return A ResponseEntity containing a map with the queue length.
50+
*/
51+
@GetMapping
52+
public ResponseEntity<Map<String, Long>> getHealthStatus() {
53+
long queueLength = queueRepository.count();
54+
Map<String, Long> response = new HashMap<>();
55+
response.put("queueLength", queueLength);
56+
log.info("Current queue length: {}", queueLength);
57+
return ResponseEntity.ok(response);
58+
}
59+
}

src/main/java/com/lpvs/controller/package-info.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
/**
99
* This package contains the controller classes for handling various aspects of the License Pre-Validation Service (LPVS).
10-
* Controllers in this package manage interactions related to GitHub webhooks.
10+
* Controllers in this package manage interactions related to GitHub webhooks and service health checks.
1111
* <p>
1212
* - {@link com.lpvs.controller.GitHubController}: Manages GitHub webhook events, processes payloads, and interacts
1313
* with LPVS services for queue handling and GitHub operations.
14+
* - {@link com.lpvs.controller.HealthController}: Provides a health check endpoint to monitor the service,
15+
* specifically returning the current length of the processing queue.
1416
* </p>
1517
* These controllers play a crucial role in integrating LPVS functionalities into different parts of the application,
16-
* such as handling external events.
18+
* such as handling external events and providing service status information.
1719
*/
1820
package com.lpvs.controller;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) 2025, Samsung Electronics Co., Ltd. All rights reserved.
3+
*
4+
* Use of this source code is governed by a MIT license that can be
5+
* found in the LICENSE file.
6+
*/
7+
package com.lpvs.controller;
8+
9+
import com.lpvs.repository.LPVSQueueRepository;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.ExtendWith;
12+
import org.mockito.InjectMocks;
13+
import org.mockito.Mock;
14+
import org.mockito.junit.jupiter.MockitoExtension;
15+
import org.springframework.http.HttpStatus;
16+
import org.springframework.http.ResponseEntity;
17+
18+
import java.util.Map;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.mockito.Mockito.when;
22+
23+
@ExtendWith(MockitoExtension.class)
24+
public class HealthControllerTest {
25+
26+
@Mock private LPVSQueueRepository queueRepository;
27+
28+
@InjectMocks private HealthController healthController;
29+
30+
@Test
31+
public void testGetHealthStatus_Success_EmptyQueue() {
32+
when(queueRepository.count()).thenReturn(0L);
33+
34+
ResponseEntity<Map<String, Long>> response = healthController.getHealthStatus();
35+
36+
assertEquals(HttpStatus.OK, response.getStatusCode());
37+
Map<String, Long> responseBody = response.getBody();
38+
assertEquals(0L, responseBody.get("queueLength"));
39+
}
40+
41+
@Test
42+
public void testGetHealthStatus_Success_WithItems() {
43+
long expectedQueueLength = 5L;
44+
when(queueRepository.count()).thenReturn(expectedQueueLength);
45+
46+
ResponseEntity<Map<String, Long>> response = healthController.getHealthStatus();
47+
48+
assertEquals(HttpStatus.OK, response.getStatusCode());
49+
Map<String, Long> responseBody = response.getBody();
50+
assertEquals(expectedQueueLength, responseBody.get("queueLength"));
51+
}
52+
}

0 commit comments

Comments
 (0)