Skip to content

Commit

Permalink
apply comments from Andy;
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 227 deletions.
34 changes: 16 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,25 @@ npm install git+https://github.com/Nordstrom/metrics-client-node.git --save
```

### Usage
To get started, initialize a new instance with protocol.
To get started, initialize a new instance with protocol.
```js
const MetricsClient = require('metrics-client-node')
var client = new MetricsClient({
protocol: 'udp',
handler: 'telegrafHttpHandler',
host: 'localhost',
port: 8092
})
```
Or to enable buffer
```js
const MetricsClient = require('metrics-client-node')
var client = new MetricsClient({
protocol: 'http',
bufferEnabled: true,
host: metrics.lambda.uri,
port: +metrics.lambda.port,
database: metrics.influxdb,
maxBufferSize: +metrics.bufferSize,
flushInterval: +metrics.flushInterval
port: 8186,
database: 'test',
maxBufferSize: 0
})
```


To send message(s)
```js
client.send(message)
```
We accept one message or a list of messages, which needs to have a format of
```json
```js
{
measure: 'measure-name',
fields: { field1: 123, field2: 'someOtherValse' },
Expand All @@ -52,4 +42,12 @@ client.close()
```

### Limitation
We only support 2 protocols: http and udp; and udp is only default protocol.
We only have 2 handler implementations: telegrafHttpHandler and telegrafUdpHandler. User can provide the implementation of its own handler implementation.
```js
var handler = function(options) {
return (messages) => {
// implementation here
}
}
```

3 changes: 3 additions & 0 deletions handlers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = require('require-directory')(module)
34 changes: 34 additions & 0 deletions handlers/telegrafHttpHandler.js
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))
})
}
}
39 changes: 39 additions & 0 deletions handlers/telegrafUdpHandler.js
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))
})
}
}
38 changes: 37 additions & 1 deletion index.js
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
98 changes: 0 additions & 98 deletions lib/metricsClient.js

This file was deleted.

28 changes: 0 additions & 28 deletions lib/telegrafHttpClient.js

This file was deleted.

34 changes: 0 additions & 34 deletions lib/telegrafUdpClient.js

This file was deleted.

40 changes: 0 additions & 40 deletions lib/utils.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"buffered-messenger-node": "git+https://github.com/Nordstrom/buffered-messenger-node.git",
"influx": "^5.0.4",
"lodash": "^4.17.2",
"require-directory": "^2.1.1",
"telegrafjs": "^0.1.3"
},
"devDependencies": {
Expand Down

0 comments on commit 5555daf

Please sign in to comment.