-
Notifications
You must be signed in to change notification settings - Fork 143
Add TCP support #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TCP support #79
Changes from 5 commits
08b79b6
c0b0d47
265b222
69de8cb
b571d8c
c87dcf3
957411a
9c82ab4
13bd3ea
456b2ac
e1c84bd
eb7cac1
64d5c54
39c25ac
0b9109a
52a697c
815a968
e9c617e
d91308d
e779962
26ad94a
ddbc5f7
ae15044
5979432
3998a4a
3c960d4
08bec86
ce3aefd
6b780ca
e76fefb
20a856b
24bd985
ce810eb
4ce7a1b
082f84b
014ab95
c815723
cce0708
74ab81c
ceddf7d
196dd6c
16634ff
29b7186
1e55f02
2683b7b
ba0ab7b
034b7b7
e0dd22c
dee23b3
fe2d446
34215ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| var dgram = require('dgram'), | ||
| util = require('util'), | ||
| dns = require('dns'), | ||
| net = require('net'), | ||
| helpers = require('./helpers'), | ||
| applyStatsFns = require('./statsFunctions'); | ||
|
|
||
|
|
@@ -25,7 +26,7 @@ var dgram = require('dgram'), | |
| * @constructor | ||
| */ | ||
| var Client = function (host, port, prefix, suffix, globalize, cacheDns, mock, | ||
| globalTags, maxBufferSize, bufferFlushInterval, telegraf, sampleRate) { | ||
| globalTags, maxBufferSize, bufferFlushInterval, telegraf, sampleRate, protocol) { | ||
| var options = host || {}, | ||
| self = this; | ||
|
|
||
|
|
@@ -42,18 +43,37 @@ var Client = function (host, port, prefix, suffix, globalize, cacheDns, mock, | |
| maxBufferSize : maxBufferSize, | ||
| bufferFlushInterval: bufferFlushInterval, | ||
| telegraf : telegraf, | ||
| sampleRate : sampleRate | ||
| sampleRate : sampleRate, | ||
| protocol : protocol | ||
| }; | ||
| } | ||
|
|
||
| function createSocket(args) { | ||
|
||
| var socket; | ||
|
|
||
| if (args.protocol === 'tcp') { | ||
| socket = net.connect(args.port, args.host); | ||
| socket.setKeepAlive(true); | ||
| } else { | ||
| socket = dgram.createSocket('udp4'); | ||
| } | ||
|
|
||
| return socket; | ||
| } | ||
|
|
||
| // hidden global_tags option for backwards compatibility | ||
| options.globalTags = options.globalTags || options.global_tags; | ||
|
|
||
| this.protocol = options.protocol; | ||
|
||
| this.host = options.host || 'localhost'; | ||
| this.port = options.port || 8125; | ||
| this.prefix = options.prefix || ''; | ||
| this.suffix = options.suffix || ''; | ||
| this.socket = options.isChild ? options.socket : dgram.createSocket('udp4'); | ||
| this.socket = options.isChild ? options.socket : createSocket({ | ||
| host: this.host, | ||
| port: this.port, | ||
| protocol: this.protocol | ||
| }); | ||
| this.mock = options.mock; | ||
| this.globalTags = typeof options.globalTags === "object" ? | ||
| helpers.formatTags(options.globalTags, options.telegraf) : []; | ||
|
|
@@ -216,6 +236,11 @@ Client.prototype.send = function (message, tags, callback) { | |
| message += '|#' + mergedTags.join(','); | ||
| } | ||
| } | ||
|
|
||
| if (this.protocol === 'tcp') { | ||
| message += '\n'; | ||
| } | ||
|
||
|
|
||
| this._send(message, callback); | ||
| }; | ||
|
|
||
|
|
@@ -298,7 +323,11 @@ Client.prototype.sendMessage = function(message, callback){ | |
| } | ||
| var buf = new Buffer(message); | ||
| try { | ||
| this.socket.send(buf, 0, buf.length, this.port, this.host, callback); | ||
| if(this.protocol === 'tcp') { | ||
| this.socket.write(buf, 'ascii', callback); | ||
| } else { | ||
| this.socket.send(buf, 0, buf.length, this.port, this.host, callback); | ||
| } | ||
| } | ||
| catch(err) { | ||
| var errMessage = 'Error sending hot-shots message: ' + err; | ||
|
|
@@ -375,7 +404,8 @@ var ChildClient = function (parent, options) { | |
| helpers.overrideTags(parent.globalTags, options.globalTags, parent.telegraf) : parent.globalTags, | ||
| maxBufferSize : parent.maxBufferSize, | ||
| bufferFlushInterval: parent.bufferFlushInterval, | ||
| telegraf : parent.telegraf | ||
| telegraf : parent.telegraf, | ||
| protocol : parent.protocol | ||
| }); | ||
| }; | ||
| util.inherits(ChildClient, Client); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Someone is going to do "TCP" at some point and get confused. How about adding:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in fe2d446.