mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-06-01 15:20:51 +00:00
Remove unused docker logs logging code
Change-type: patch
This commit is contained in:
parent
bcf6476b2d
commit
894bdeeeb6
@ -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;
|
|
@ -1,12 +1,5 @@
|
|||||||
import { BalenaLogBackend } from './balena-backend';
|
import { BalenaLogBackend } from './balena-backend';
|
||||||
import ContainerLogs from './container';
|
|
||||||
import { LocalLogBackend } from './local-backend';
|
import { LocalLogBackend } from './local-backend';
|
||||||
import { LogBackend, LogMessage } from './log-backend';
|
import { LogBackend, LogMessage } from './log-backend';
|
||||||
|
|
||||||
export {
|
export { LocalLogBackend, LogBackend, LogMessage, BalenaLogBackend };
|
||||||
ContainerLogs,
|
|
||||||
LocalLogBackend,
|
|
||||||
LogBackend,
|
|
||||||
LogMessage,
|
|
||||||
BalenaLogBackend,
|
|
||||||
};
|
|
||||||
|
@ -5,7 +5,6 @@ import * as Promise from 'bluebird';
|
|||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import * as sinon from 'sinon';
|
import * as sinon from 'sinon';
|
||||||
|
|
||||||
import { ContainerLogs } from '~/src/logging/container';
|
|
||||||
import * as config from '~/src/config';
|
import * as config from '~/src/config';
|
||||||
|
|
||||||
describe('Logger', function () {
|
describe('Logger', function () {
|
||||||
@ -128,16 +127,4 @@ describe('Logger', function () {
|
|||||||
expect(msg).to.have.property('timestamp').that.is.at.least(timestamp);
|
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,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user