Skip to content

Commit

Permalink
Logs: move logs validation to the point of ingest rather than publish
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
Page- committed Dec 24, 2024
1 parent 86c3d9b commit ac1ffa8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
20 changes: 0 additions & 20 deletions src/features/device-logs/lib/backends/loki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,25 +353,6 @@ export class LokiBackend implements DeviceLogsBackend {
return `{fleet_id="${ctx.appId}"}`;
}

private validateLog(log: DeviceLog): asserts log is DeviceLog {
if (typeof log.message !== 'string') {
throw new BadRequestError('DeviceLog message must be string');
} else if (typeof log.timestamp !== 'number') {
throw new BadRequestError('DeviceLog timestamp must be number');
} else if (typeof log.isSystem !== 'boolean') {
throw new BadRequestError('DeviceLog isSystem must be boolean');
} else if (typeof log.isStdErr !== 'boolean') {
throw new BadRequestError('DeviceLog isStdErr must be boolean');
} else if (
typeof log.serviceId !== 'number' &&
log.serviceId !== undefined
) {
throw new BadRequestError(
'DeviceLog serviceId must be number or undefined',
);
}
}

private fromStreamToDeviceLogs(stream: loki.StreamAdapter): DeviceLog[] {
try {
return stream.getEntriesList().map((entry) => {
Expand Down Expand Up @@ -399,7 +380,6 @@ export class LokiBackend implements DeviceLogsBackend {
logs: Array<DeviceLog & { version?: number }>,
) {
return logs.map((log) => {
this.validateLog(log);
const timestamp = new loki.Timestamp();
timestamp.setSeconds(Math.floor(Number(log.nanoTimestamp / 1000000000n)));
timestamp.setNanos(Number(log.nanoTimestamp % 1000000000n));
Expand Down
7 changes: 6 additions & 1 deletion src/features/device-logs/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,12 @@ function handleStreamingWrite(
}

parser.on('error', close).on('data', (sLog: SupervisorLog) => {
const log = supervisor.convertLog(sLog);
let log;
try {
log = supervisor.convertLog(sLog);
} catch {
return;
}
if (!log) {
return;
}
Expand Down
22 changes: 19 additions & 3 deletions src/features/device-logs/lib/supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,36 @@ export class Supervisor {
return this.convertLog(log);
}

public convertLog(log: SupervisorLog): DeviceLog | undefined {
public convertLog(log: {
[key in keyof SupervisorLog]: unknown;
}): DeviceLog | undefined {
// see struct.ts for explanation on this
if (log.uuid) {
return;
}
return {
if (typeof log.message !== 'string') {
throw new errors.BadRequestError('DeviceLog message must be string');
}
if (typeof log.timestamp !== 'number') {
throw new errors.BadRequestError('DeviceLog timestamp must be number');
}
const validatedLog: DeviceLog = {
nanoTimestamp: getNanoTimestamp(),
createdAt: Date.now(),
timestamp: log.timestamp,
isSystem: log.isSystem === true,
isStdErr: log.isStdErr === true,
message: log.message,
serviceId: log.serviceId,
};
if ('serviceId' in log) {
if (typeof log.serviceId !== 'number') {
throw new errors.BadRequestError(
'DeviceLog serviceId must be number or undefined',
);
}
validatedLog.serviceId = log.serviceId;
}
return validatedLog;
}

private isOldLog(log: any): log is OldSupervisorLog {
Expand Down

0 comments on commit ac1ffa8

Please sign in to comment.