Merge pull request #2209 from balena-os/logs-optimization

Logs optimizations
This commit is contained in:
flowzone-app[bot] 2023-10-12 01:33:08 +00:00 committed by GitHub
commit 9f79b4b157
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 27 deletions

View File

@ -12,7 +12,7 @@ import {
LogBackend,
LogMessage,
} from './logging';
import logMonitor from './logging/monitor';
import logMonitor, { MonitorHook } from './logging/monitor';
import * as globalEventBus from './event-bus';
import superConsole from './lib/supervisor-console';
@ -109,9 +109,7 @@ export function enable(value: boolean = true) {
}
export function log(message: LogMessage) {
if (backend != null) {
backend.log(message);
}
backend?.log(message);
}
export function logSystemMessage(
@ -139,9 +137,10 @@ export function lock(containerId: string): Bluebird.Disposer<() => void> {
});
}
type ServiceInfo = { serviceId: number; imageId: number };
export function attach(
containerId: string,
serviceInfo: { serviceId: number; imageId: number },
{ serviceId, imageId }: ServiceInfo,
): Bluebird<void> {
// First detect if we already have an attached log stream
// for this container
@ -150,9 +149,14 @@ export function attach(
}
return Bluebird.using(lock(containerId), async () => {
logMonitor.attach(containerId, (message) => {
log({ ...serviceInfo, ...message });
});
await logMonitor.attach(
containerId,
(message: Parameters<MonitorHook>[0] & Partial<ServiceInfo>) => {
message.serviceId = serviceId;
message.imageId = imageId;
log(message);
},
);
});
}

View File

@ -5,10 +5,7 @@ import * as db from '../db';
import { spawnJournalctl, toJournalDate } from '../lib/journald';
import log from '../lib/supervisor-console';
export type MonitorHook = ({
message,
isStdErr,
}: {
export type MonitorHook = (message: {
message: string;
isStdErr: boolean;
timestamp: number;
@ -70,13 +67,13 @@ class LogMonitor {
format: 'json',
filterString: '_SYSTEMD_UNIT=balena.service',
},
(row: JournalRow) => {
(row) => {
if (row.CONTAINER_ID_FULL && this.containers[row.CONTAINER_ID_FULL]) {
this.setupAttempts = 0;
this.handleRow(row);
}
},
(data: Buffer) => {
(data) => {
log.error('journalctl - balena.service stderr: ', data.toString());
},
() => {
@ -154,8 +151,8 @@ class LogMonitor {
filterString: `CONTAINER_ID_FULL=${containerId}`,
since: toJournalDate(lastSentTimestamp + 1), // increment to exclude last sent log
},
(row: JournalRow) => this.handleRow(row),
async (data: Buffer) => {
(row) => this.handleRow(row),
(data) => {
log.error(
`journalctl - container ${containerId} stderr: `,
data.toString(),
@ -169,20 +166,25 @@ class LogMonitor {
private handleRow(row: JournalRow) {
if (
row.CONTAINER_ID_FULL &&
row.CONTAINER_NAME !== 'balena_supervisor' &&
row.CONTAINER_NAME !== 'resin_supervisor'
row.CONTAINER_ID_FULL == null ||
row.CONTAINER_NAME === 'balena_supervisor' ||
row.CONTAINER_NAME === 'resin_supervisor'
) {
return;
}
const containerId = row.CONTAINER_ID_FULL;
if (this.containers[containerId] == null) {
return;
}
const message = messageFieldToString(row.MESSAGE);
if (message == null) {
return;
}
const isStdErr = row.PRIORITY === '3';
const timestamp = Math.floor(Number(row.__REALTIME_TIMESTAMP) / 1000); // microseconds to milliseconds
if (message != null && this.containers[containerId]) {
this.updateContainerSentTimestamp(containerId, timestamp);
this.containers[containerId].hook({ message, isStdErr, timestamp });
}
}
}
private updateContainerSentTimestamp(
containerId: string,