-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Implement Observability, OPS Features, and API Documentation #10
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
common-lib/src/main/java/com/example/common/config/OpenApiConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.example.common.config; | ||
|
|
||
| import io.swagger.v3.oas.models.OpenAPI; | ||
| import io.swagger.v3.oas.models.info.Info; | ||
| import io.swagger.v3.oas.models.info.License; | ||
| import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
| import org.springframework.context.annotation.Bean; | ||
|
|
||
| @AutoConfiguration | ||
| public class OpenApiConfig { | ||
|
|
||
| @Bean | ||
| public OpenAPI customOpenAPI() { | ||
| return new OpenAPI() | ||
| .info(new Info() | ||
| .title("People & Tax Ecosystem API") | ||
| .version("0.0.1-SNAPSHOT") | ||
| .description("API for managing people and calculating tax in the Indian context.") | ||
| .license(new License().name("Apache 2.0").url("http://springdoc.org"))); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
common-lib/src/main/java/com/example/common/exception/ErrorResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.example.common.exception; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public record ErrorResponse( | ||
| String message, | ||
| String correlationId, | ||
| int status, | ||
| LocalDateTime timestamp) { | ||
| } |
53 changes: 53 additions & 0 deletions
53
common-lib/src/main/java/com/example/common/exception/GlobalExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package com.example.common.exception; | ||
|
|
||
| import com.example.common.logging.CorrelationIdFilter; | ||
| import org.slf4j.MDC; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| @ControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
|
|
||
| @ExceptionHandler(Exception.class) | ||
| public ResponseEntity<ErrorResponse> handleAllExceptions(Exception ex) { | ||
| if (logger.isErrorEnabled()) { | ||
| logger.error("Unhandled exception occurred: {}", ex.getMessage(), ex); | ||
| } | ||
| return buildErrorResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); | ||
| } | ||
|
|
||
| @ExceptionHandler(IllegalArgumentException.class) | ||
| public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException ex) { | ||
| if (logger.isWarnEnabled()) { | ||
| logger.warn("Invalid argument: {}", ex.getMessage()); | ||
| } | ||
| return buildErrorResponse(ex.getMessage(), HttpStatus.BAD_REQUEST); | ||
| } | ||
|
|
||
| @ExceptionHandler(IllegalStateException.class) | ||
| public ResponseEntity<ErrorResponse> handleIllegalStateException(IllegalStateException ex) { | ||
| if (logger.isWarnEnabled()) { | ||
| logger.warn("Illegal state: {}", ex.getMessage()); | ||
| } | ||
| return buildErrorResponse(ex.getMessage(), HttpStatus.BAD_REQUEST); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private ResponseEntity<ErrorResponse> buildErrorResponse(String message, HttpStatus status) { | ||
| String correlationId = MDC.get(CorrelationIdFilter.CORRELATION_ID_LOG_VAR); | ||
| ErrorResponse error = new ErrorResponse( | ||
| message, | ||
| correlationId, | ||
| status.value(), | ||
| LocalDateTime.now()); | ||
| return new ResponseEntity<>(error, status); | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
common-lib/src/main/java/com/example/common/logging/CorrelationIdFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.example.common.logging; | ||
|
|
||
| import jakarta.servlet.*; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.slf4j.MDC; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.UUID; | ||
|
|
||
| @Component | ||
| public class CorrelationIdFilter implements Filter { | ||
|
|
||
| public static final String CORRELATION_ID_HEADER = "X-Correlation-ID"; | ||
| public static final String CORRELATION_ID_LOG_VAR = "correlationId"; | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
| throws IOException, ServletException { | ||
|
|
||
| HttpServletRequest httpRequest = (HttpServletRequest) request; | ||
| HttpServletResponse httpResponse = (HttpServletResponse) response; | ||
|
|
||
| String correlationId = httpRequest.getHeader(CORRELATION_ID_HEADER); | ||
| if (correlationId == null || correlationId.isEmpty()) { | ||
| correlationId = UUID.randomUUID().toString(); | ||
| } | ||
|
|
||
| MDC.put(CORRELATION_ID_LOG_VAR, correlationId); | ||
| httpResponse.setHeader(CORRELATION_ID_HEADER, correlationId); | ||
|
|
||
| try { | ||
| chain.doFilter(request, response); | ||
| } finally { | ||
| MDC.remove(CORRELATION_ID_LOG_VAR); | ||
| } | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
common-lib/src/main/java/com/example/common/logging/CorrelationIdInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.example.common.logging; | ||
|
|
||
| import feign.RequestInterceptor; | ||
| import feign.RequestTemplate; | ||
| import org.slf4j.MDC; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class CorrelationIdInterceptor implements RequestInterceptor { | ||
|
|
||
| @Override | ||
| public void apply(RequestTemplate template) { | ||
| String correlationId = MDC.get(CorrelationIdFilter.CORRELATION_ID_LOG_VAR); | ||
| if (correlationId != null) { | ||
| template.header(CorrelationIdFilter.CORRELATION_ID_HEADER, correlationId); | ||
| } | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
common-lib/src/main/java/com/example/common/logging/ObservabilityAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.example.common.logging; | ||
|
|
||
| import com.example.common.exception.GlobalExceptionHandler; | ||
| import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||
| import org.springframework.context.annotation.Import; | ||
|
|
||
| @AutoConfiguration | ||
| @ConditionalOnWebApplication | ||
| @Import({ | ||
| CorrelationIdFilter.class, | ||
| CorrelationIdInterceptor.class, | ||
| GlobalExceptionHandler.class, | ||
| com.example.common.config.OpenApiConfig.class | ||
| }) | ||
| public class ObservabilityAutoConfiguration { | ||
| } |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| com.example.common.logging.ObservabilityAutoConfiguration |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # API Test Plan & Manual Verification | ||
|
|
||
| This document outlines the steps to verify the stability of the People & Tax Ecosystem APIs. | ||
|
|
||
| ## 🔗 Swagger / OpenAPI Documentation | ||
| Once the services are running, you can access the interactive API docs at: | ||
| - **People Service**: [http://localhost:8080/swagger-ui/index.html](http://localhost:8080/swagger-ui/index.html) | ||
| - **Tax Engine Service**: [http://localhost:8081/swagger-ui/index.html](http://localhost:8081/swagger-ui/index.html) | ||
|
|
||
| ## 🧪 1. People Management Service | ||
| **Base URL**: `http://localhost:8080` | ||
|
|
||
| ### 1.1 Create Person (Full Time Employee) | ||
| **Endpoint**: `POST /people` | ||
| **Headers**: | ||
| - `Content-Type`: `application/json` | ||
|
|
||
| **Body**: | ||
| ```json | ||
| { | ||
| "personType": "EMPLOYEE_FULL_TIME", | ||
| "id": 101, | ||
| "name": "Rahul Dravid", | ||
| "email": "rahul.dravid@example.com", | ||
| "annualSalary": 1200000 | ||
| } | ||
| ``` | ||
| **Expected Response**: `201 Created` | ||
|
|
||
| ### 1.2 Create Person (Contractor) | ||
| **Endpoint**: `POST /people` | ||
| **Body**: | ||
| ```json | ||
| { | ||
| "personType": "EMPLOYEE_CONTRACTOR", | ||
| "id": 102, | ||
| "name": "Hardik Pandya", | ||
| "email": "hardik@example.com", | ||
| "hourlyRate": 2000, | ||
| "hoursWorked": 160 | ||
| } | ||
| ``` | ||
|
|
||
| ### 1.3 Get Person by ID | ||
| **Endpoint**: `GET /people/101` | ||
| **Expected Response**: JSON object of Rahul Dravid. | ||
|
|
||
| ### 1.4 Get Monthly Income | ||
| **Endpoint**: `GET /people/101/income` | ||
| **Expected Response**: `100000.00` (12,00,000 / 12) | ||
|
|
||
| --- | ||
|
|
||
| ## 💰 2. Tax Engine Service | ||
| **Base URL**: `http://localhost:8081` | ||
|
|
||
| ### 2.1 Calculate Tax (Standalone) | ||
| **Endpoint**: `POST /tax/calculate` | ||
| **Body**: | ||
| ```json | ||
| { | ||
| "person": { | ||
| "personType": "EMPLOYEE_FULL_TIME", | ||
| "id": 999, | ||
| "name": "Richie Rich", | ||
| "email": "richie@example.com", | ||
| "annualSalary": 1500000 | ||
| }, | ||
| "regime": "NEW" | ||
| } | ||
| ``` | ||
| **Expected Response**: JSON with calculated tax breakdown. | ||
|
|
||
| ### 2.2 Calculate Tax for Existing Person (Orchestrated) | ||
| **Endpoint**: `GET /tax/calculate/101?regime=NEW` | ||
| **Description**: Fetches Rahul Dravid (101) from People Service and calculates tax. | ||
| **Verification**: Check logs for `X-Correlation-ID` to ensure it matches across both services. | ||
|
|
||
| --- | ||
|
|
||
| ## 🛠️ Verification Checklist | ||
| - [x] Swagger UI loads for both services. | ||
| - [x] `POST /people` creates data successfully. | ||
| - [x] `GET /people/{id}` retrieves correct data. | ||
| - [x] `POST /tax/calculate` returns valid tax computation. | ||
| - [x] `GET /tax/calculate/{id}` works and shows orchestration success. | ||
| - [x] Logs show matching `X-Correlation-ID` for the orchestrated call. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.