-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also convert it to be a generic metrics client
- Loading branch information
WANG, Yun
authored and
WANG, Yun
committed
Jan 25, 2017
1 parent
a2a1a2a
commit 5555daf
Showing
11 changed files
with
140 additions
and
227 deletions.
There are no files selected for viewing
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,3 @@ | ||
'use strict' | ||
|
||
module.exports = require('require-directory')(module) |
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,34 @@ | ||
'use strict' | ||
|
||
const _ = require('lodash') | ||
const influx = require('influx') | ||
|
||
function formatHttpMetrics (metrics) { | ||
var formattedMetrics = [] | ||
if (!_.isArray(metrics)) { | ||
metrics = [metrics] | ||
} | ||
_.forEach(metrics, function (value) { | ||
formattedMetrics.push({ | ||
measurement: value.measure, | ||
tags: value.tags, | ||
fields: value.fields | ||
}) | ||
}) | ||
return formattedMetrics | ||
} | ||
|
||
module.exports = function (options) { | ||
return (messages) => { | ||
var url = 'http://' + (options.host || 'localhost') + ':' + (options.port || 8186) + '/' + options.database | ||
var InfluxClient = new influx.InfluxDB(url) | ||
|
||
return InfluxClient.writePoints(formatHttpMetrics(messages)) | ||
.then(function () { | ||
console.log('Written points') | ||
}) | ||
.catch(function (err) { | ||
console.error('ERROR sending the influx line metrics: ', (err.error || err)) | ||
}) | ||
} | ||
} |
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,39 @@ | ||
'use strict' | ||
|
||
const _ = require('lodash') | ||
const telegraf = require('telegrafjs') | ||
|
||
function formatUdpMessages (messages) { | ||
var buffer = '' | ||
if (!_.isArray(messages)) { | ||
messages = [messages] | ||
} | ||
_.forEach(messages, function (message) { | ||
var measurement = new telegraf.Measurement( | ||
message.measure, | ||
message.tags, | ||
message.fields | ||
) | ||
buffer += measurement.toString() + '\n' | ||
}) | ||
return buffer | ||
} | ||
|
||
module.exports = function (options) { | ||
return (messages) => { | ||
var udpClient = new telegraf.TelegrafUDPClient({ | ||
host: options.host, | ||
port: options.port | ||
}) | ||
return udpClient.connect() | ||
.then(function () { | ||
return udpClient.sendMeasurement(formatUdpMessages(messages)) | ||
}) | ||
.then(function () { | ||
return udpClient.close() | ||
}) | ||
.catch(function (err) { | ||
console.error('ERROR sending the metrics: ', (err.error || err)) | ||
}) | ||
} | ||
} |
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,3 +1,39 @@ | ||
'use strict' | ||
|
||
module.exports = require('./lib/metricsClient.js') | ||
const _ = require('lodash') | ||
const Promise = require('bluebird') | ||
const BufferMessenger = require('buffered-messenger-node') | ||
const handlers = require('./handlers') | ||
|
||
/* | ||
* Metrics Client sends metrics via different handler. | ||
*/ | ||
var MetricsClient = function (options) { | ||
var self = this | ||
var handler = options.handler && _.isFunction(options.handler) | ||
? options.handler(options) | ||
: handlers[options.handler](options) || _.noop | ||
|
||
self.client = new BufferMessenger({ | ||
handler: handler, | ||
maxBufferSize: options.maxBufferSize, | ||
flushInterval: options.flushInterval | ||
}) | ||
} | ||
|
||
MetricsClient.prototype.send = function (metrics) { | ||
var self = this | ||
if (!_.isArray(metrics)) { | ||
return self.client.send(metrics) | ||
} | ||
return Promise.each(metrics, function (item) { | ||
return self.client.send(item) | ||
}) | ||
} | ||
|
||
MetricsClient.prototype.close = function () { | ||
var self = this | ||
return self.client.close() | ||
} | ||
|
||
module.exports = MetricsClient |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
Oops, something went wrong.