Skip to content

Commit

Permalink
Issue #226 feat: added health check API (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
HarishGangula authored Jan 29, 2024
1 parent 9fb75f6 commit 5be2f0d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
5 changes: 5 additions & 0 deletions api-service/src/configs/RoutesConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ export const routesConfig = {
method: "get",
path: "/status"
}
},
health: {
api_id: "api.health",
method: "get",
path: "/health"
}
}

5 changes: 5 additions & 0 deletions api-service/src/connectors/DbConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export class DbConnector implements IConnector {
return await this.pool.destroy()
}

async health() {
await this.connect()
await this.pool.select(1)
}

execute(type: keyof typeof this.typeToMethod, property: any) {
this.method = this.typeToMethod[ type ]
return this.method(property[ "table" ], property[ "fields" ])
Expand Down
8 changes: 4 additions & 4 deletions api-service/src/helpers/ErrorResponseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export class ErrorResponseHandler {
"headers": req.headers,
"url": req.url,
"error": {
"message": error.message,
"stack": error.stack,
"data": error.data,
"code": error.code,
"message": error?.message,
"stack": error?.stack,
"data": error?.data,
"code": error?.code,
"error": error,
}
}));
Expand Down
6 changes: 5 additions & 1 deletion api-service/src/routes/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import { WrapperService } from "../services/WrapperService";
import { onRequest } from "../helpers/prometheus/helpers";
import promEntities from "../helpers/prometheus/entities";
import { metricsScrapeHandler } from "../helpers/prometheus";
import { HealthService } from "../services/HealthService";

export const validationService = new ValidationService();
export const queryService = new QueryService(new HTTPConnector(`${config.query_api.druid.host}:${config.query_api.druid.port}`));
const httpDruidConnector = new HTTPConnector(`${config.query_api.druid.host}:${config.query_api.druid.port}`)
export const queryService = new QueryService(httpDruidConnector);
export const kafkaConnector = new KafkaConnector()
export const dbConnector = new DbConnector(config.db_connector_config);
export const datasourceService = new DataSourceService(dbConnector, config.table_names.datasources);
Expand All @@ -30,6 +32,7 @@ export const ingestorService = new IngestorService(kafkaConnector,);
export const exhaustService = new ClientCloudService(config.exhaust_config.cloud_storage_provider, config.exhaust_config.cloud_storage_config);
export const wrapperService = new WrapperService();
export const globalCache: any = new Map()
export const healthService = new HealthService(dbConnector, kafkaConnector, httpDruidConnector)
export const router = express.Router()
dbConnector.init()
/** Query API(s) */
Expand Down Expand Up @@ -72,3 +75,4 @@ router.post(routesConfig.query_wrapper.native_post.path, ResponseHandler.setApiI
router.get(routesConfig.query_wrapper.native_get.path, ResponseHandler.setApiId(routesConfig.query_wrapper.native_get.api_id), onRequest({ entity: promEntities.data_out }), wrapperService.forwardNativeGet)
router.delete(routesConfig.query_wrapper.native_delete.path, ResponseHandler.setApiId(routesConfig.query_wrapper.native_delete.api_id), onRequest({ entity: promEntities.data_out }), wrapperService.forwardNativeDel)
router.get(routesConfig.query_wrapper.druid_status.path, ResponseHandler.setApiId(routesConfig.query_wrapper.druid_status.api_id), wrapperService.nativeStatus)
router.get(routesConfig.health.path, ResponseHandler.setApiId(routesConfig.health.api_id), healthService.checkHealth.bind(healthService))
46 changes: 46 additions & 0 deletions api-service/src/services/HealthService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { AxiosInstance } from "axios";
import { NextFunction, Request, Response } from "express";
import _ from "lodash";
import { ResponseHandler } from "../helpers/ResponseHandler";
import { ErrorResponseHandler } from "../helpers/ErrorResponseHandler";
import { IConnector } from "../models/DatasetModels";
import { DbConnector } from "../connectors/DbConnector";
import { KafkaConnector } from "../connectors/KafkaConnector";




export class HealthService {
private dbConnector: DbConnector;
private kafkaConnector: KafkaConnector;
private httpDruidConnector: AxiosInstance;
private errorHandler: ErrorResponseHandler;
constructor(dbConnector: DbConnector, kafkaConnector: KafkaConnector, httpDruidConnector: IConnector) {
this.errorHandler = new ErrorResponseHandler("HealthService");
this.httpDruidConnector = httpDruidConnector.connect()
this.dbConnector = dbConnector;
this.kafkaConnector = kafkaConnector;
}

checkHealth(req: Request, res: Response, next: NextFunction) {
Promise.all([this.checkDruidHealth(), this.checkKafkaHealth(), this.checkPostgresHealth()])
.then(() => {
ResponseHandler.successResponse(req, res, { status: 200, data: {} })
}).catch(error => {
this.errorHandler.handleError(req, res, next, error)
})
}

private async checkDruidHealth() {
await this.httpDruidConnector.get("/status/health")
}

private async checkKafkaHealth() {
await this.kafkaConnector.connect()
}

private async checkPostgresHealth() {
await this.dbConnector.health()
}

}

0 comments on commit 5be2f0d

Please sign in to comment.