2024-05-29 14:25:09 +00:00
|
|
|
import TelemetryIngest, {
|
2024-06-14 11:09:53 +00:00
|
|
|
TelemetryRequest,
|
|
|
|
} from "../Middleware/TelemetryIngest";
|
|
|
|
import OneUptimeDate from "Common/Types/Date";
|
|
|
|
import { JSONObject } from "Common/Types/JSON";
|
|
|
|
import ProductType from "Common/Types/MeteredPlan/ProductType";
|
2024-08-07 21:50:32 +00:00
|
|
|
import LogService from "Common/Server/Services/LogService";
|
2024-02-02 12:21:29 +00:00
|
|
|
import Express, {
|
2024-06-14 11:09:53 +00:00
|
|
|
ExpressRequest,
|
|
|
|
ExpressResponse,
|
|
|
|
ExpressRouter,
|
|
|
|
NextFunction,
|
2024-08-07 21:50:32 +00:00
|
|
|
} from "Common/Server/Utils/Express";
|
|
|
|
import logger from "Common/Server/Utils/Logger";
|
|
|
|
import Response from "Common/Server/Utils/Response";
|
2024-08-05 19:05:53 +00:00
|
|
|
import Log from "Common/Models/AnalyticsModels/Log";
|
2024-08-04 22:41:07 +00:00
|
|
|
import LogSeverity from "Common/Types/Log/LogSeverity";
|
2024-08-02 01:25:54 +00:00
|
|
|
import OTelIngestService from "../Service/OTelIngest";
|
|
|
|
import ObjectID from "Common/Types/ObjectID";
|
2024-08-02 19:44:59 +00:00
|
|
|
import JSONFunctions from "Common/Types/JSONFunctions";
|
2024-02-02 12:21:29 +00:00
|
|
|
|
|
|
|
export class FluentRequestMiddleware {
|
2024-06-14 11:09:53 +00:00
|
|
|
public static async getProductType(
|
|
|
|
req: ExpressRequest,
|
|
|
|
_res: ExpressResponse,
|
|
|
|
next: NextFunction,
|
|
|
|
): Promise<void> {
|
|
|
|
try {
|
|
|
|
(req as TelemetryRequest).productType = ProductType.Logs;
|
|
|
|
return next();
|
|
|
|
} catch (err) {
|
|
|
|
return next(err);
|
2024-02-02 12:21:29 +00:00
|
|
|
}
|
2024-06-14 11:09:53 +00:00
|
|
|
}
|
2024-02-02 12:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const router: ExpressRouter = Express.getRouter();
|
|
|
|
|
|
|
|
router.post(
|
2024-06-14 11:09:53 +00:00
|
|
|
"/fluentd/v1/logs",
|
|
|
|
FluentRequestMiddleware.getProductType,
|
|
|
|
TelemetryIngest.isAuthorizedServiceMiddleware,
|
|
|
|
async (
|
|
|
|
req: ExpressRequest,
|
|
|
|
res: ExpressResponse,
|
|
|
|
next: NextFunction,
|
|
|
|
): Promise<void> => {
|
|
|
|
try {
|
|
|
|
logger.debug("Fluent Ingestor API called");
|
2024-02-02 12:21:29 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const dbLogs: Array<Log> = [];
|
2024-02-02 12:21:29 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const logItems: Array<JSONObject | string> = req.body as Array<
|
|
|
|
JSONObject | string
|
|
|
|
>;
|
2024-02-02 12:21:29 +00:00
|
|
|
|
2024-08-02 01:25:54 +00:00
|
|
|
let oneuptimeServiceName: string | string[] | undefined =
|
|
|
|
req.headers["x-oneuptime-service-name"];
|
|
|
|
|
|
|
|
if (!oneuptimeServiceName) {
|
|
|
|
oneuptimeServiceName = "Unknown Service";
|
|
|
|
}
|
|
|
|
|
|
|
|
const telemetryService: {
|
|
|
|
serviceId: ObjectID;
|
|
|
|
dataRententionInDays: number;
|
|
|
|
} = await OTelIngestService.telemetryServiceFromName({
|
|
|
|
serviceName: oneuptimeServiceName as string,
|
|
|
|
projectId: (req as TelemetryRequest).projectId,
|
|
|
|
});
|
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
for (let logItem of logItems) {
|
|
|
|
const dbLog: Log = new Log();
|
2024-02-02 12:21:29 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
dbLog.projectId = (req as TelemetryRequest).projectId;
|
2024-08-02 01:25:54 +00:00
|
|
|
dbLog.serviceId = telemetryService.serviceId;
|
2024-06-14 11:09:53 +00:00
|
|
|
dbLog.severityNumber = 0;
|
|
|
|
const currentTimeAndDate: Date = OneUptimeDate.getCurrentDate();
|
|
|
|
dbLog.timeUnixNano = OneUptimeDate.toUnixNano(currentTimeAndDate);
|
|
|
|
dbLog.time = currentTimeAndDate;
|
2024-02-02 12:21:29 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
dbLog.severityText = LogSeverity.Unspecified;
|
2024-02-02 12:21:29 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (typeof logItem !== "string") {
|
|
|
|
logItem = JSON.stringify(logItem);
|
|
|
|
}
|
2024-02-02 12:21:29 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
dbLog.body = logItem as string;
|
2024-02-02 12:21:29 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
dbLogs.push(dbLog);
|
|
|
|
}
|
2024-02-02 12:21:29 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
await LogService.createMany({
|
|
|
|
items: dbLogs,
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
});
|
2024-02-02 12:21:29 +00:00
|
|
|
|
2024-08-02 19:44:59 +00:00
|
|
|
OTelIngestService.recordDataIngestedUsgaeBilling({
|
|
|
|
services: {
|
|
|
|
[oneuptimeServiceName as string]: {
|
|
|
|
dataIngestedInGB: JSONFunctions.getSizeOfJSONinGB(req.body),
|
|
|
|
dataRententionInDays: telemetryService.dataRententionInDays,
|
|
|
|
serviceId: telemetryService.serviceId,
|
|
|
|
serviceName: oneuptimeServiceName as string,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
projectId: (req as TelemetryRequest).projectId,
|
|
|
|
productType: ProductType.Logs,
|
|
|
|
}).catch((err: Error) => {
|
|
|
|
logger.error(err);
|
|
|
|
});
|
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
return Response.sendEmptySuccessResponse(req, res);
|
|
|
|
} catch (err) {
|
|
|
|
return next(err);
|
2024-02-02 12:21:29 +00:00
|
|
|
}
|
2024-06-14 11:09:53 +00:00
|
|
|
},
|
2024-02-02 12:21:29 +00:00
|
|
|
);
|
|
|
|
|
2024-02-02 12:59:23 +00:00
|
|
|
export default router;
|