cleanup
This commit is contained in:
1 parent
c7689cc64a
commit
ad7d4e54c3
4 files changed
+66
-354
No files matched your search
@@ -2,29 +2,21 @@ import { IStatusProvider } from "@/service/infrastructure/network/IStatusProvide
|
||||
import { GrpcStatusPayload, MonitorStatusResponse } from "@/types/network.js";
|
||||
import { Monitor, MonitorType } from "@/types/monitor.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
import { NETWORK_ERROR, timeRequest } from "@/service/infrastructure/network/utils.js";
|
||||
import { ILogger } from "@/utils/logger.js";
|
||||
import { timeRequest } from "@/service/infrastructure/network/utils.js";
|
||||
import * as grpc from "@grpc/grpc-js";
|
||||
import * as protoLoader from "@grpc/proto-loader";
|
||||
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { Network } from "inspector/promises";
|
||||
|
||||
type GrpcType = typeof grpc;
|
||||
type ProtoLoaderType = typeof protoLoader;
|
||||
|
||||
const SERVICE_NAME = "GrpcProvider";
|
||||
|
||||
export interface GrpcError extends Error {
|
||||
code: number;
|
||||
details?: string;
|
||||
}
|
||||
|
||||
export class GrpcProvider implements IStatusProvider<GrpcStatusPayload> {
|
||||
readonly type = "grpc";
|
||||
constructor(
|
||||
private logger: ILogger,
|
||||
private grpc: GrpcType,
|
||||
private protoLoader: ProtoLoaderType
|
||||
) {}
|
||||
@@ -33,10 +25,6 @@ export class GrpcProvider implements IStatusProvider<GrpcStatusPayload> {
|
||||
return type === "grpc";
|
||||
}
|
||||
|
||||
private isGrpcError(error: unknown): error is GrpcError {
|
||||
return error instanceof Error && "code" in error && typeof (error as Record<string, unknown>).code === "number";
|
||||
}
|
||||
|
||||
private getGrpcStatusName(code: number): string {
|
||||
const statusNames: Record<number, string> = {
|
||||
0: "OK",
|
||||
@@ -61,15 +49,20 @@ export class GrpcProvider implements IStatusProvider<GrpcStatusPayload> {
|
||||
}
|
||||
|
||||
async handle(monitor: Monitor): Promise<MonitorStatusResponse<GrpcStatusPayload>> {
|
||||
const { id, teamId, type, url, port, ignoreTlsErrors } = monitor;
|
||||
const grpcServiceName = monitor.grpcServiceName || "";
|
||||
const host = url?.replace(/^https?:\/\//, "").split(/[/?#:]/)[0];
|
||||
|
||||
try {
|
||||
if (!host || !port) {
|
||||
throw new Error("Valid host and port are required");
|
||||
const { id, teamId, type, url, port, ignoreTlsErrors } = monitor;
|
||||
const grpcServiceName = monitor.grpcServiceName || "";
|
||||
|
||||
if (!url) {
|
||||
throw new AppError({ message: "Monitor host is required", service: SERVICE_NAME, method: "handle" });
|
||||
}
|
||||
if (!port) {
|
||||
throw new AppError({ message: "Monitor port is required", service: SERVICE_NAME, method: "handle" });
|
||||
}
|
||||
|
||||
const host = url?.replace(/^https?:\/\//, "").split(/[/?#:]/)[0];
|
||||
const target = `${host}:${port}`;
|
||||
|
||||
const currentFilePath = fileURLToPath(import.meta.url);
|
||||
const protoPath = path.join(path.dirname(currentFilePath), "protos", "health.proto");
|
||||
const packageDefinition = this.protoLoader.loadSync(protoPath, {
|
||||
@@ -79,8 +72,23 @@ export class GrpcProvider implements IStatusProvider<GrpcStatusPayload> {
|
||||
defaults: true,
|
||||
oneofs: true,
|
||||
});
|
||||
const grpcObject = this.grpc.loadPackageDefinition(packageDefinition) as any;
|
||||
const HealthClient = grpcObject.grpc.health.v1.Health;
|
||||
const grpcObject = this.grpc.loadPackageDefinition(packageDefinition) as unknown as {
|
||||
grpc: {
|
||||
health: {
|
||||
v1: {
|
||||
Health: new (
|
||||
target: string,
|
||||
credentials: unknown
|
||||
) => {
|
||||
Check: (request: { service: string }, options: { deadline: Date }, callback: (err: unknown, response: unknown) => void) => void;
|
||||
close: () => void;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
const healthService = grpcObject.grpc.health.v1.Health;
|
||||
|
||||
let credentials;
|
||||
if (ignoreTlsErrors) {
|
||||
credentials = this.grpc.credentials.createSsl(null, null, null, {
|
||||
@@ -89,8 +97,12 @@ export class GrpcProvider implements IStatusProvider<GrpcStatusPayload> {
|
||||
} else {
|
||||
credentials = this.grpc.credentials.createInsecure();
|
||||
}
|
||||
const client = new HealthClient(target, credentials);
|
||||
const deadline = new Date(Date.now() + 10000); // 10s timeout
|
||||
|
||||
const client = new healthService(target, credentials);
|
||||
|
||||
const TIMEOUT_MS = 10000;
|
||||
const deadline = new Date(Date.now() + TIMEOUT_MS);
|
||||
|
||||
const { response, responseTime, error } = await timeRequest<GrpcStatusPayload>(() => {
|
||||
return new Promise<GrpcStatusPayload>((resolve, reject) => {
|
||||
client.Check({ service: grpcServiceName }, { deadline }, (err: unknown, response: unknown) => {
|
||||
@@ -128,51 +140,32 @@ export class GrpcProvider implements IStatusProvider<GrpcStatusPayload> {
|
||||
});
|
||||
|
||||
if (error) {
|
||||
let grpcCode = -1;
|
||||
let message = error instanceof Error ? error.message : "gRPC health check failed";
|
||||
|
||||
if (this.isGrpcError(error)) {
|
||||
grpcCode = error.code;
|
||||
message = error.details || error.message;
|
||||
}
|
||||
|
||||
const grpcError = error as AppError & { grpcPayload?: GrpcStatusPayload; grpcCode?: number };
|
||||
const payload = grpcError.grpcPayload;
|
||||
return {
|
||||
monitorId: id,
|
||||
teamId: teamId,
|
||||
type: type,
|
||||
status: false,
|
||||
code: 500,
|
||||
message: message,
|
||||
code: grpcError.grpcCode ?? 5000,
|
||||
message: grpcError.message ?? "gRPC health check failed",
|
||||
responseTime,
|
||||
payload: {
|
||||
grpcStatusCode: grpcCode,
|
||||
grpcStatusName: this.getGrpcStatusName(grpcCode),
|
||||
serviceName: grpcServiceName,
|
||||
servingStatus: "UNKNOWN",
|
||||
},
|
||||
payload: payload ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
if (!response) {
|
||||
throw new Error("No response received from gRPC health check");
|
||||
}
|
||||
|
||||
const isServing = response.servingStatus === "SERVING";
|
||||
const grpcPayload = response as GrpcStatusPayload;
|
||||
const isServing = grpcPayload.servingStatus === "SERVING";
|
||||
|
||||
return {
|
||||
monitorId: id,
|
||||
teamId: teamId,
|
||||
type: type,
|
||||
status: isServing,
|
||||
code: isServing ? 200 : NETWORK_ERROR,
|
||||
message: isServing ? "gRPC health check successful" : "gRPC health check failed",
|
||||
code: isServing ? 200 : 5000,
|
||||
message: isServing ? `gRPC service healthy (${grpcPayload.servingStatus})` : `gRPC service unhealthy (${grpcPayload.servingStatus})`,
|
||||
responseTime,
|
||||
payload: {
|
||||
grpcStatusCode: isServing ? 200 : NETWORK_ERROR,
|
||||
grpcStatusName: response.grpcStatusName,
|
||||
serviceName: response.serviceName,
|
||||
servingStatus: response.servingStatus || "UNKNOWN",
|
||||
},
|
||||
payload: grpcPayload,
|
||||
};
|
||||
} catch (err: unknown) {
|
||||
const originalMessage = err instanceof Error ? err.message : String(err);
|
||||
|
||||
@@ -6,6 +6,7 @@ import { MonitorStatusResponse } from "@/types/network.js";
|
||||
import { Agent as HttpsAgent } from "https";
|
||||
import { Monitor, MonitorType } from "@/types/monitor.js";
|
||||
import { NETWORK_ERROR } from "@/service/infrastructure/network/utils.js";
|
||||
import CacheableLookup from "cacheable-lookup";
|
||||
|
||||
export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
||||
readonly type = "http";
|
||||
@@ -13,7 +14,16 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
||||
constructor(
|
||||
private got: Got,
|
||||
private advancedMatcher: AdvancedMatcher
|
||||
) {}
|
||||
) {
|
||||
const cacheable = new CacheableLookup();
|
||||
this.got = got.extend({
|
||||
dnsCache: cacheable,
|
||||
timeout: {
|
||||
request: 30000,
|
||||
},
|
||||
retry: { limit: 1 },
|
||||
});
|
||||
}
|
||||
|
||||
supports(type: MonitorType) {
|
||||
return type === "http";
|
||||
|
||||
Reference in new issue
Block a user