-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logstash): Tcp udp transport (#74)
* feat(logstash): added dynamic logstash transport * test: added tests for dynamic logstash transport * refactor: fix cr Co-authored-by: Arik Furman <[email protected]>
- Loading branch information
1 parent
8b77fc4
commit 0d889e3
Showing
8 changed files
with
2,525 additions
and
2,754 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export enum LogstashProtocol { | ||
TCP = 'tcp', | ||
UDP = 'udp', | ||
DYNAMIC = 'dynamic', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { LogstashProtocol } from '../configurations/logstash-protocol'; | ||
import { LogstashOption, LogstashTransport } from 'winston-logstash-ts'; | ||
|
||
const sizeof = require('object-sizeof'); | ||
|
||
const MAX_UDP_PACKET_SIZE_IN_BYTES = 65535; | ||
type DynamicProtocolOptions = Omit<LogstashOption, 'protocol'>; | ||
|
||
export class DynamicLogstashTransport extends LogstashTransport { | ||
constructor(options: DynamicProtocolOptions) { | ||
super(options); | ||
} | ||
public send(message: any, callback: any): Promise<void> { | ||
if (sizeof(message) < MAX_UDP_PACKET_SIZE_IN_BYTES) { | ||
this.protocol = LogstashProtocol.UDP; | ||
return super.send(message, callback); | ||
} else { | ||
this.protocol = LogstashProtocol.TCP; | ||
return super.send(message, callback); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { LogstashProtocol } from '../src/configurations/logstash-protocol'; | ||
import { DynamicLogstashTransport } from '../src/transports/dynamic-logstash-transport'; | ||
|
||
jest.mock('object-sizeof', () => jest.fn().mockReturnValueOnce(50).mockReturnValueOnce(70000)); | ||
|
||
describe('dynamic-logstash-transport tests', () => { | ||
const logstashHost = 'test'; | ||
const logstashPort = 8080; | ||
const dynamicLogstashTransport = new DynamicLogstashTransport({ | ||
host: logstashHost, | ||
port: logstashPort, | ||
}); | ||
const message = 'hi'; | ||
const cb = {}; | ||
test('gets message and sends it using UDP', async () => { | ||
await expect(dynamicLogstashTransport.send(message, cb)).rejects.toThrowError(); | ||
expect((dynamicLogstashTransport as any).protocol).toEqual(LogstashProtocol.UDP); | ||
}); | ||
test('gets message and sends it using TCP', async () => { | ||
await expect(dynamicLogstashTransport.send(message, cb)).rejects.toThrowError(); | ||
expect((dynamicLogstashTransport as any).protocol).toEqual(LogstashProtocol.TCP); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters