heath impl
This commit is contained in:
85
src/health/indicators/db-pool-stats.health.ts
Normal file
85
src/health/indicators/db-pool-stats.health.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { HealthIndicator, HealthIndicatorResult } from '@nestjs/terminus';
|
||||
import { InjectConnection } from '@nestjs/typeorm';
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class DbPoolStatsIndicator extends HealthIndicator {
|
||||
constructor(
|
||||
@InjectConnection('oracle') private oracleConnection: DataSource,
|
||||
@InjectConnection('postgres') private postgresConnection: DataSource,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async checkOraclePoolStats(): Promise<HealthIndicatorResult> {
|
||||
const key = 'oracle_pool';
|
||||
|
||||
try {
|
||||
// Obter estatísticas do pool Oracle (usando query específica do Oracle)
|
||||
const queryResult = await this.oracleConnection.query(`
|
||||
SELECT
|
||||
'ORACLEDB_POOL' as source,
|
||||
COUNT(*) as total_connections
|
||||
FROM
|
||||
V$SESSION
|
||||
WHERE
|
||||
TYPE = 'USER'
|
||||
AND PROGRAM LIKE 'nodejs%'
|
||||
`);
|
||||
|
||||
const status = {
|
||||
isHealthy: true,
|
||||
totalConnections: queryResult?.[0]?.total_connections || 0,
|
||||
connectionClass: 'PORTALJURU',
|
||||
};
|
||||
|
||||
return this.getStatus(key, status.isHealthy, {
|
||||
totalConnections: status.totalConnections,
|
||||
connectionClass: status.connectionClass,
|
||||
});
|
||||
} catch (error) {
|
||||
// Em caso de erro, ainda retornar um status (não saudável)
|
||||
return this.getStatus(key, false, {
|
||||
message: `Erro ao obter estatísticas do pool Oracle: ${error.message}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async checkPostgresPoolStats(): Promise<HealthIndicatorResult> {
|
||||
const key = 'postgres_pool';
|
||||
|
||||
try {
|
||||
// Obter estatísticas do pool PostgreSQL
|
||||
const queryResult = await this.postgresConnection.query(`
|
||||
SELECT
|
||||
count(*) as total_connections,
|
||||
sum(CASE WHEN state = 'active' THEN 1 ELSE 0 END) as active_connections,
|
||||
sum(CASE WHEN state = 'idle' THEN 1 ELSE 0 END) as idle_connections
|
||||
FROM
|
||||
pg_stat_activity
|
||||
WHERE
|
||||
datname = current_database()
|
||||
AND application_name LIKE 'nodejs%'
|
||||
`);
|
||||
|
||||
const status = {
|
||||
isHealthy: true,
|
||||
totalConnections: parseInt(queryResult?.[0]?.total_connections) || 0,
|
||||
activeConnections: parseInt(queryResult?.[0]?.active_connections) || 0,
|
||||
idleConnections: parseInt(queryResult?.[0]?.idle_connections) || 0,
|
||||
};
|
||||
|
||||
return this.getStatus(key, status.isHealthy, {
|
||||
totalConnections: status.totalConnections,
|
||||
activeConnections: status.activeConnections,
|
||||
idleConnections: status.idleConnections,
|
||||
});
|
||||
} catch (error) {
|
||||
// Em caso de erro, ainda retornar um status (não saudável)
|
||||
return this.getStatus(key, false, {
|
||||
message: `Erro ao obter estatísticas do pool PostgreSQL: ${error.message}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
150
src/health/indicators/dist/db-pool-stats.health.js
vendored
Normal file
150
src/health/indicators/dist/db-pool-stats.health.js
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (_) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
exports.__esModule = true;
|
||||
exports.DbPoolStatsIndicator = void 0;
|
||||
var common_1 = require("@nestjs/common");
|
||||
var terminus_1 = require("@nestjs/terminus");
|
||||
var typeorm_1 = require("@nestjs/typeorm");
|
||||
var DbPoolStatsIndicator = /** @class */ (function (_super) {
|
||||
__extends(DbPoolStatsIndicator, _super);
|
||||
function DbPoolStatsIndicator(oracleConnection, postgresConnection) {
|
||||
var _this = _super.call(this) || this;
|
||||
_this.oracleConnection = oracleConnection;
|
||||
_this.postgresConnection = postgresConnection;
|
||||
return _this;
|
||||
}
|
||||
DbPoolStatsIndicator.prototype.checkOraclePoolStats = function () {
|
||||
var _a;
|
||||
return __awaiter(this, void 0, Promise, function () {
|
||||
var key, queryResult, status, error_1;
|
||||
return __generator(this, function (_b) {
|
||||
switch (_b.label) {
|
||||
case 0:
|
||||
key = 'oracle_pool';
|
||||
_b.label = 1;
|
||||
case 1:
|
||||
_b.trys.push([1, 3, , 4]);
|
||||
return [4 /*yield*/, this.oracleConnection.query("\n SELECT \n 'ORACLEDB_POOL' as source,\n COUNT(*) as total_connections\n FROM \n V$SESSION \n WHERE \n TYPE = 'USER' \n AND PROGRAM LIKE 'nodejs%'\n ")];
|
||||
case 2:
|
||||
queryResult = _b.sent();
|
||||
status = {
|
||||
isHealthy: true,
|
||||
totalConnections: ((_a = queryResult === null || queryResult === void 0 ? void 0 : queryResult[0]) === null || _a === void 0 ? void 0 : _a.total_connections) || 0,
|
||||
connectionClass: 'PORTALJURU'
|
||||
};
|
||||
return [2 /*return*/, this.getStatus(key, status.isHealthy, {
|
||||
totalConnections: status.totalConnections,
|
||||
connectionClass: status.connectionClass
|
||||
})];
|
||||
case 3:
|
||||
error_1 = _b.sent();
|
||||
// Em caso de erro, ainda retornar um status (não saudável)
|
||||
return [2 /*return*/, this.getStatus(key, false, {
|
||||
message: "Erro ao obter estat\u00EDsticas do pool Oracle: " + error_1.message
|
||||
})];
|
||||
case 4: return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
DbPoolStatsIndicator.prototype.checkPostgresPoolStats = function () {
|
||||
var _a, _b, _c;
|
||||
return __awaiter(this, void 0, Promise, function () {
|
||||
var key, queryResult, status, error_2;
|
||||
return __generator(this, function (_d) {
|
||||
switch (_d.label) {
|
||||
case 0:
|
||||
key = 'postgres_pool';
|
||||
_d.label = 1;
|
||||
case 1:
|
||||
_d.trys.push([1, 3, , 4]);
|
||||
return [4 /*yield*/, this.postgresConnection.query("\n SELECT \n count(*) as total_connections,\n sum(CASE WHEN state = 'active' THEN 1 ELSE 0 END) as active_connections,\n sum(CASE WHEN state = 'idle' THEN 1 ELSE 0 END) as idle_connections\n FROM \n pg_stat_activity \n WHERE \n datname = current_database()\n AND application_name LIKE 'nodejs%'\n ")];
|
||||
case 2:
|
||||
queryResult = _d.sent();
|
||||
status = {
|
||||
isHealthy: true,
|
||||
totalConnections: parseInt((_a = queryResult === null || queryResult === void 0 ? void 0 : queryResult[0]) === null || _a === void 0 ? void 0 : _a.total_connections) || 0,
|
||||
activeConnections: parseInt((_b = queryResult === null || queryResult === void 0 ? void 0 : queryResult[0]) === null || _b === void 0 ? void 0 : _b.active_connections) || 0,
|
||||
idleConnections: parseInt((_c = queryResult === null || queryResult === void 0 ? void 0 : queryResult[0]) === null || _c === void 0 ? void 0 : _c.idle_connections) || 0
|
||||
};
|
||||
return [2 /*return*/, this.getStatus(key, status.isHealthy, {
|
||||
totalConnections: status.totalConnections,
|
||||
activeConnections: status.activeConnections,
|
||||
idleConnections: status.idleConnections
|
||||
})];
|
||||
case 3:
|
||||
error_2 = _d.sent();
|
||||
// Em caso de erro, ainda retornar um status (não saudável)
|
||||
return [2 /*return*/, this.getStatus(key, false, {
|
||||
message: "Erro ao obter estat\u00EDsticas do pool PostgreSQL: " + error_2.message
|
||||
})];
|
||||
case 4: return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
DbPoolStatsIndicator = __decorate([
|
||||
common_1.Injectable(),
|
||||
__param(0, typeorm_1.InjectConnection('oracle')),
|
||||
__param(1, typeorm_1.InjectConnection('postgres'))
|
||||
], DbPoolStatsIndicator);
|
||||
return DbPoolStatsIndicator;
|
||||
}(terminus_1.HealthIndicator));
|
||||
exports.DbPoolStatsIndicator = DbPoolStatsIndicator;
|
||||
122
src/health/indicators/dist/typeorm.health.js
vendored
Normal file
122
src/health/indicators/dist/typeorm.health.js
vendored
Normal file
@@ -0,0 +1,122 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (_) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
exports.__esModule = true;
|
||||
exports.TypeOrmHealthIndicator = void 0;
|
||||
var common_1 = require("@nestjs/common");
|
||||
var terminus_1 = require("@nestjs/terminus");
|
||||
var typeorm_1 = require("@nestjs/typeorm");
|
||||
var TypeOrmHealthIndicator = /** @class */ (function (_super) {
|
||||
__extends(TypeOrmHealthIndicator, _super);
|
||||
function TypeOrmHealthIndicator(oracleConnection, postgresConnection) {
|
||||
var _this = _super.call(this) || this;
|
||||
_this.oracleConnection = oracleConnection;
|
||||
_this.postgresConnection = postgresConnection;
|
||||
return _this;
|
||||
}
|
||||
TypeOrmHealthIndicator.prototype.checkOracle = function () {
|
||||
return __awaiter(this, void 0, Promise, function () {
|
||||
var key, isHealthy, result, result;
|
||||
return __generator(this, function (_a) {
|
||||
key = 'oracle';
|
||||
try {
|
||||
isHealthy = this.oracleConnection.isInitialized;
|
||||
result = this.getStatus(key, isHealthy);
|
||||
if (isHealthy) {
|
||||
return [2 /*return*/, result];
|
||||
}
|
||||
throw new terminus_1.HealthCheckError('Oracle healthcheck failed', result);
|
||||
}
|
||||
catch (error) {
|
||||
result = this.getStatus(key, false, { message: error.message });
|
||||
throw new terminus_1.HealthCheckError('Oracle healthcheck failed', result);
|
||||
}
|
||||
return [2 /*return*/];
|
||||
});
|
||||
});
|
||||
};
|
||||
TypeOrmHealthIndicator.prototype.checkPostgres = function () {
|
||||
return __awaiter(this, void 0, Promise, function () {
|
||||
var key, isHealthy, result, result;
|
||||
return __generator(this, function (_a) {
|
||||
key = 'postgres';
|
||||
try {
|
||||
isHealthy = this.postgresConnection.isInitialized;
|
||||
result = this.getStatus(key, isHealthy);
|
||||
if (isHealthy) {
|
||||
return [2 /*return*/, result];
|
||||
}
|
||||
throw new terminus_1.HealthCheckError('Postgres healthcheck failed', result);
|
||||
}
|
||||
catch (error) {
|
||||
result = this.getStatus(key, false, { message: error.message });
|
||||
throw new terminus_1.HealthCheckError('Postgres healthcheck failed', result);
|
||||
}
|
||||
return [2 /*return*/];
|
||||
});
|
||||
});
|
||||
};
|
||||
TypeOrmHealthIndicator = __decorate([
|
||||
common_1.Injectable(),
|
||||
__param(0, typeorm_1.InjectConnection('oracle')),
|
||||
__param(1, typeorm_1.InjectConnection('postgres'))
|
||||
], TypeOrmHealthIndicator);
|
||||
return TypeOrmHealthIndicator;
|
||||
}(terminus_1.HealthIndicator));
|
||||
exports.TypeOrmHealthIndicator = TypeOrmHealthIndicator;
|
||||
56
src/health/indicators/typeorm.health.ts
Normal file
56
src/health/indicators/typeorm.health.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { HealthIndicator, HealthIndicatorResult, HealthCheckError } from '@nestjs/terminus';
|
||||
import { InjectConnection } from '@nestjs/typeorm';
|
||||
import { Connection, DataSource } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class TypeOrmHealthIndicator extends HealthIndicator {
|
||||
constructor(
|
||||
@InjectConnection('oracle') private oracleConnection: DataSource,
|
||||
@InjectConnection('postgres') private postgresConnection: DataSource,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async checkOracle(): Promise<HealthIndicatorResult> {
|
||||
const key = 'oracle';
|
||||
|
||||
try {
|
||||
// Verifica se a conexão com Oracle está funcionando
|
||||
const isHealthy = this.oracleConnection.isInitialized;
|
||||
|
||||
const result = this.getStatus(key, isHealthy);
|
||||
|
||||
if (isHealthy) {
|
||||
return result;
|
||||
}
|
||||
|
||||
throw new HealthCheckError('Oracle healthcheck failed', result);
|
||||
} catch (error) {
|
||||
// Se houver qualquer erro, consideramos a conexão como não saudável
|
||||
const result = this.getStatus(key, false, { message: error.message });
|
||||
throw new HealthCheckError('Oracle healthcheck failed', result);
|
||||
}
|
||||
}
|
||||
|
||||
async checkPostgres(): Promise<HealthIndicatorResult> {
|
||||
const key = 'postgres';
|
||||
|
||||
try {
|
||||
// Verifica se a conexão com Postgres está funcionando
|
||||
const isHealthy = this.postgresConnection.isInitialized;
|
||||
|
||||
const result = this.getStatus(key, isHealthy);
|
||||
|
||||
if (isHealthy) {
|
||||
return result;
|
||||
}
|
||||
|
||||
throw new HealthCheckError('Postgres healthcheck failed', result);
|
||||
} catch (error) {
|
||||
// Se houver qualquer erro, consideramos a conexão como não saudável
|
||||
const result = this.getStatus(key, false, { message: error.message });
|
||||
throw new HealthCheckError('Postgres healthcheck failed', result);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user