2024-06-14 11:09:53 +00:00
|
|
|
import { ProbeExpressRequest } from "../Types/Request";
|
|
|
|
import DiskSize from "Common/Types/DiskSize";
|
|
|
|
import BadRequestException from "Common/Types/Exception/BadRequestException";
|
|
|
|
import ProductType from "Common/Types/MeteredPlan/ProductType";
|
|
|
|
import ObjectID from "Common/Types/ObjectID";
|
|
|
|
import TelemetryServiceService from "CommonServer/Services/TelemetryServiceService";
|
|
|
|
import TelemetryUsageBillingService from "CommonServer/Services/TelemetryUsageBillingService";
|
2024-02-02 11:05:34 +00:00
|
|
|
import {
|
2024-06-14 11:09:53 +00:00
|
|
|
ExpressRequest,
|
|
|
|
ExpressResponse,
|
|
|
|
NextFunction,
|
|
|
|
} from "CommonServer/Utils/Express";
|
|
|
|
import logger from "CommonServer/Utils/Logger";
|
|
|
|
import TelemetryService from "Model/Models/TelemetryService";
|
|
|
|
import { DEFAULT_RETENTION_IN_DAYS } from "Model/Models/TelemetryUsageBilling";
|
2024-02-02 11:02:02 +00:00
|
|
|
|
|
|
|
export interface TelemetryRequest extends ExpressRequest {
|
2024-06-14 11:09:53 +00:00
|
|
|
serviceId: ObjectID; // Service ID
|
2024-07-17 23:27:04 +00:00
|
|
|
serviceName: string; // Service Name
|
2024-06-14 11:09:53 +00:00
|
|
|
projectId: ObjectID; // Project ID
|
|
|
|
dataRententionInDays: number; // how long the data should be retained.
|
|
|
|
productType: ProductType; // what is the product type of the request - logs, metrics or traces.
|
2024-02-02 11:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class TelemetryIngest {
|
2024-06-14 11:09:53 +00:00
|
|
|
public static async isAuthorizedServiceMiddleware(
|
|
|
|
req: ProbeExpressRequest,
|
|
|
|
_res: ExpressResponse,
|
|
|
|
next: NextFunction,
|
|
|
|
): Promise<void> {
|
|
|
|
try {
|
|
|
|
// check header.
|
2024-02-02 11:02:02 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const serviceTokenInHeader: string | undefined = req.headers[
|
|
|
|
"x-oneuptime-service-token"
|
|
|
|
] as string | undefined;
|
2024-02-02 11:02:02 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!serviceTokenInHeader) {
|
|
|
|
throw new BadRequestException(
|
|
|
|
"Missing header: x-oneuptime-service-token",
|
|
|
|
);
|
|
|
|
}
|
2024-02-02 11:02:02 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// size of req.body in bytes.
|
|
|
|
const sizeInBytes: number = Buffer.byteLength(JSON.stringify(req.body));
|
2024-02-02 11:02:02 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const sizeToGb: number = DiskSize.byteSizeToGB(sizeInBytes);
|
2024-02-02 11:02:02 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// load from the database and set the cache.
|
|
|
|
const service: TelemetryService | null =
|
|
|
|
await TelemetryServiceService.findOneBy({
|
|
|
|
query: {
|
|
|
|
telemetryServiceToken: new ObjectID(serviceTokenInHeader as string),
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
projectId: true,
|
|
|
|
retainTelemetryDataForDays: true,
|
2024-07-17 23:27:04 +00:00
|
|
|
name: true,
|
2024-06-14 11:09:53 +00:00
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
});
|
2024-02-02 11:02:02 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!service) {
|
2024-07-09 19:08:45 +00:00
|
|
|
throw new BadRequestException(
|
|
|
|
"Invalid service token: " + serviceTokenInHeader,
|
|
|
|
);
|
2024-06-14 11:09:53 +00:00
|
|
|
}
|
2024-02-02 11:02:02 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
(req as TelemetryRequest).serviceId = service.id as ObjectID;
|
|
|
|
(req as TelemetryRequest).projectId = service.projectId as ObjectID;
|
2024-07-17 23:27:04 +00:00
|
|
|
(req as TelemetryRequest).serviceName = service.name as string;
|
2024-06-14 11:09:53 +00:00
|
|
|
(req as TelemetryRequest).dataRententionInDays =
|
|
|
|
service.retainTelemetryDataForDays || DEFAULT_RETENTION_IN_DAYS;
|
2024-04-05 08:40:36 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
(req as TelemetryRequest).serviceId = service.id as ObjectID;
|
|
|
|
(req as TelemetryRequest).projectId = service.projectId as ObjectID;
|
2024-02-02 11:02:02 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// report to Usage Service.
|
|
|
|
TelemetryUsageBillingService.updateUsageBilling({
|
|
|
|
projectId: (req as TelemetryRequest).projectId,
|
|
|
|
productType: (req as TelemetryRequest).productType,
|
|
|
|
dataIngestedInGB: sizeToGb,
|
|
|
|
telemetryServiceId: (req as TelemetryRequest).serviceId,
|
|
|
|
retentionInDays: (req as TelemetryRequest).dataRententionInDays,
|
|
|
|
}).catch((err: Error) => {
|
|
|
|
logger.error("Failed to update usage billing for OTel");
|
|
|
|
logger.error(err);
|
|
|
|
});
|
2024-02-02 11:02:02 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
return next(err);
|
2024-02-02 11:02:02 +00:00
|
|
|
}
|
2024-06-14 11:09:53 +00:00
|
|
|
}
|
2024-02-02 11:05:34 +00:00
|
|
|
}
|