Remove unused docker logs logging code

Change-type: patch
This commit is contained in:
Pagan Gazzard 2023-10-11 14:01:27 +01:00
parent bcf6476b2d
commit 894bdeeeb6
3 changed files with 1 additions and 121 deletions

View File

@ -1,100 +0,0 @@
import * as es from 'event-stream';
import { EventEmitter } from 'events';
import * as _ from 'lodash';
import * as Stream from 'stream';
import StrictEventEmitter from 'strict-event-emitter-types';
import { docker } from '../lib/docker-utils';
export interface ContainerLog {
message: string;
timestamp: number;
isStdout: boolean;
}
interface LogsEvents {
log: ContainerLog;
closed: void;
error: Error;
}
type LogsEventEmitter = StrictEventEmitter<EventEmitter, LogsEvents>;
export class ContainerLogs extends (EventEmitter as new () => LogsEventEmitter) {
public constructor(public containerId: string) {
super();
}
public async attach(lastSentTimestamp: number) {
const logOpts = {
follow: true,
timestamps: true,
since: Math.floor(lastSentTimestamp / 1000),
};
const stdoutLogOpts = { stdout: true, stderr: false, ...logOpts };
const stderrLogOpts = { stderr: true, stdout: false, ...logOpts };
const container = docker.getContainer(this.containerId);
const stdoutStream = await container.logs(stdoutLogOpts);
const stderrStream = await container.logs(stderrLogOpts);
[
[stdoutStream, true],
[stderrStream, false],
].forEach(([stream, isStdout]: [Stream.Readable, boolean]) => {
stream
.on('error', (err) => {
this.emit(
'error',
new Error(`Error on container logs: ${err} ${err.stack}`),
);
})
.pipe(es.split())
.on('data', (logBuf: Buffer | string) => {
if (_.isString(logBuf)) {
logBuf = Buffer.from(logBuf);
}
const logMsg = ContainerLogs.extractMessage(logBuf);
if (logMsg != null) {
this.emit('log', { isStdout, ...logMsg });
}
})
.on('error', (err) => {
this.emit(
'error',
new Error(`Error on container logs: ${err} ${err.stack}`),
);
})
.on('end', () => this.emit('closed'));
});
}
private static extractMessage(
msgBuf: Buffer,
): { message: string; timestamp: number } | undefined {
// Non-tty message format from:
// https://docs.docker.com/engine/api/v1.30/#operation/ContainerAttach
if (
_.includes([0, 1, 2], msgBuf[0]) &&
_.every(msgBuf.slice(1, 7), (c) => c === 0)
) {
// Take the header from this message, and parse it as normal
msgBuf = msgBuf.slice(8);
}
const logLine = msgBuf.toString();
const space = logLine.indexOf(' ');
if (space > 0) {
let timestamp = new Date(logLine.substr(0, space)).getTime();
if (_.isNaN(timestamp)) {
timestamp = Date.now();
}
return {
timestamp,
message: logLine.substr(space + 1),
};
}
return;
}
}
export default ContainerLogs;

View File

@ -1,12 +1,5 @@
import { BalenaLogBackend } from './balena-backend';
import ContainerLogs from './container';
import { LocalLogBackend } from './local-backend';
import { LogBackend, LogMessage } from './log-backend';
export {
ContainerLogs,
LocalLogBackend,
LogBackend,
LogMessage,
BalenaLogBackend,
};
export { LocalLogBackend, LogBackend, LogMessage, BalenaLogBackend };

View File

@ -5,7 +5,6 @@ import * as Promise from 'bluebird';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { ContainerLogs } from '~/src/logging/container';
import * as config from '~/src/config';
describe('Logger', function () {
@ -128,16 +127,4 @@ describe('Logger', function () {
expect(msg).to.have.property('timestamp').that.is.at.least(timestamp);
});
});
it('should support non-tty log lines', function () {
const message =
'\u0001\u0000\u0000\u0000\u0000\u0000\u0000?2018-09-21T12:37:09.819134000Z this is the message';
const buffer = Buffer.from(message);
// @ts-expect-error accessing a private function
expect(ContainerLogs.extractMessage(buffer)).to.deep.equal({
message: 'this is the message',
timestamp: 1537533429819,
});
});
});