oneuptime/Common/Server/API/TelemetryAPI.ts

91 lines
2.5 KiB
TypeScript

import UserMiddleware from "../Middleware/UserAuthorization";
import Express, {
ExpressRequest,
ExpressResponse,
ExpressRouter,
NextFunction,
} from "../Utils/Express";
import Response from "../Utils/Response";
import BadDataException from "Common/Types/Exception/BadDataException";
import CommonAPI from "./CommonAPI";
import DatabaseCommonInteractionProps from "Common/Types/BaseDatabase/DatabaseCommonInteractionProps";
import TelemetryType from "Common/Types/Telemetry/TelemetryType";
import TelemetryAttributeService from "../Services/TelemetryAttributeService";
import ArrayUtil from "Common/Utils/Array";
const router: ExpressRouter = Express.getRouter();
router.post(
"/telemetry/metrics/get-attributes",
UserMiddleware.getUserMiddleware,
async (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {
return getAttributes(req, res, next, TelemetryType.Metric);
},
);
router.post(
"/telemetry/logs/get-attributes",
UserMiddleware.getUserMiddleware,
async (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {
return getAttributes(req, res, next, TelemetryType.Log);
},
);
router.post(
"/telemetry/traces/get-attributes",
UserMiddleware.getUserMiddleware,
async (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {
return getAttributes(req, res, next, TelemetryType.Trace);
},
);
type GetAttributesFunction = (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction,
telemetryType: TelemetryType,
) => Promise<void>;
const getAttributes: GetAttributesFunction = async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction,
telemetryType: TelemetryType,
) => {
try {
const databaseProps: DatabaseCommonInteractionProps =
await CommonAPI.getDatabaseCommonInteractionProps(req);
if (!databaseProps) {
return Response.sendErrorResponse(
req,
res,
new BadDataException("Invalid User Sesssion"),
);
}
if (!databaseProps.tenantId) {
return Response.sendErrorResponse(
req,
res,
new BadDataException("Invalid Project ID"),
);
}
const attributes: string[] = ArrayUtil.removeDuplicates(
await TelemetryAttributeService.fetchAttributes({
projectId: databaseProps.tenantId,
telemetryType,
}),
);
return Response.sendJsonObjectResponse(req, res, {
attributes: attributes,
});
} catch (err: any) {
next(err);
}
};
export default router;