|
| 1 | +import Transport from 'winston-transport'; |
| 2 | +import { Socket } from 'socket.io-client'; |
| 3 | +import { Logger } from 'winston'; |
| 4 | + |
| 5 | +export type ApiTransportSettings = { |
| 6 | + isActive?: boolean; |
| 7 | + sendInterval?: number; |
| 8 | + maxBufferSize?: number; |
| 9 | +}; |
| 10 | + |
| 11 | +export type ApiTransportOptions = Transport.TransportStreamOptions & ApiTransportSettings & { socket?: Socket }; |
| 12 | + |
| 13 | +type Info = { |
| 14 | + message: string; |
| 15 | + timestamp: string; |
| 16 | + level: string; |
| 17 | + scope: string; |
| 18 | +}; |
| 19 | + |
| 20 | +class ApiLogsTransport extends Transport { |
| 21 | + private logger: Logger | undefined; |
| 22 | + private socket: Socket | undefined; |
| 23 | + private isActive: boolean; |
| 24 | + private sendInterval: number; |
| 25 | + private maxBufferSize: number; |
| 26 | + private logBuffer: Info[] = []; |
| 27 | + private droppedLogs: number = 0; |
| 28 | + private timer: NodeJS.Timeout | undefined = undefined; |
| 29 | + |
| 30 | + constructor (opts?: ApiTransportOptions) { |
| 31 | + super(opts); |
| 32 | + this.isActive = opts?.isActive ?? false; |
| 33 | + this.sendInterval = opts?.sendInterval ?? 10000; |
| 34 | + this.maxBufferSize = opts?.maxBufferSize ?? 100; |
| 35 | + this.socket = opts?.socket; |
| 36 | + this.scheduleSend(); |
| 37 | + } |
| 38 | + |
| 39 | + override log (info: Info, callback?: () => void) { |
| 40 | + setImmediate(() => this.emit('logged', info)); |
| 41 | + |
| 42 | + this.logBuffer.push(info); |
| 43 | + const bufferLength = this.logBuffer.length; |
| 44 | + const bufferOverflow = bufferLength - this.maxBufferSize; |
| 45 | + |
| 46 | + if (bufferOverflow > 0) { |
| 47 | + this.logBuffer.splice(0, bufferOverflow); |
| 48 | + this.droppedLogs += bufferOverflow; |
| 49 | + } |
| 50 | + |
| 51 | + callback && callback(); |
| 52 | + } |
| 53 | + |
| 54 | + setSocket (socket: Socket) { |
| 55 | + this.socket = socket; |
| 56 | + } |
| 57 | + |
| 58 | + setLogger (logger: Logger) { |
| 59 | + this.logger = logger; |
| 60 | + } |
| 61 | + |
| 62 | + getCurrentSettings () { |
| 63 | + return { |
| 64 | + isActive: this.isActive, |
| 65 | + sendInterval: this.sendInterval, |
| 66 | + maxBufferSize: this.maxBufferSize, |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + updateSettings (settings: ApiTransportSettings) { |
| 71 | + this.isActive = settings.isActive ?? this.isActive; |
| 72 | + this.sendInterval = settings.sendInterval ?? this.sendInterval; |
| 73 | + this.maxBufferSize = settings.maxBufferSize ?? this.maxBufferSize; |
| 74 | + this.scheduleSend(); |
| 75 | + } |
| 76 | + |
| 77 | + private scheduleSend () { |
| 78 | + clearTimeout(this.timer); |
| 79 | + |
| 80 | + if (this.isActive) { |
| 81 | + this.timer = setTimeout(() => { |
| 82 | + void this.sendLogs(); |
| 83 | + }, this.sendInterval); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private async sendLogs () { |
| 88 | + if (!this.isActive || !this.socket?.connected || !this.logBuffer.length) { |
| 89 | + return this.scheduleSend(); |
| 90 | + } |
| 91 | + |
| 92 | + const payload = { |
| 93 | + logs: this.logBuffer.slice(), |
| 94 | + skipped: this.droppedLogs, |
| 95 | + }; |
| 96 | + |
| 97 | + const droppedInPayload = payload.skipped; |
| 98 | + const presentInPayload = payload.logs.length; |
| 99 | + |
| 100 | + try { |
| 101 | + const response: unknown = await this.socket.emitWithAck('probe:logs', payload); |
| 102 | + |
| 103 | + if (response === 'success') { |
| 104 | + const droppedWhileAwaiting = this.droppedLogs - droppedInPayload; |
| 105 | + const oldLogsRemaining = presentInPayload - droppedWhileAwaiting; |
| 106 | + |
| 107 | + if (oldLogsRemaining >= 0) { |
| 108 | + this.logBuffer.splice(0, oldLogsRemaining); |
| 109 | + this.droppedLogs = 0; |
| 110 | + } else { |
| 111 | + this.droppedLogs = -oldLogsRemaining; // === droppedWhileAwaiting - presentInPayload |
| 112 | + } |
| 113 | + } |
| 114 | + } catch (e) { |
| 115 | + this.logger?.error('Failed to send logs to the API.', e); |
| 116 | + } finally { |
| 117 | + this.scheduleSend(); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export default ApiLogsTransport; |
0 commit comments