Skip to content

Commit eda3874

Browse files
committed
v1.0.1 release .
1 parent c0e5ffb commit eda3874

File tree

4 files changed

+75
-110
lines changed

4 files changed

+75
-110
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
## open-falcon ![NPM version](https://img.shields.io/npm/v/open-falcon.svg?style=flat)
1+
## open-falcon ![NPM version](https://img.shields.io/npm/v/node-open-falcon.svg?style=flat)
22

3-
open-falcon
3+
open-falcon for nodejs.
44

55
### Installation
66
```bash
7-
$ npm install open-falcon
7+
$ npm install node-open-falcon --save
88
```
99

1010
### Example
1111
```js
12-
var Falcon = require('open-falcon');
12+
var os = require('os');
13+
var Falcon = require('node-open-falcon');
14+
15+
var falcon = new Falcon({
16+
endpoint: os.hostname()
17+
});
18+
//
19+
var usage = process.memoryUsage();
20+
for(var key in usage){
21+
falcon.metric('memory.' + key, usage[key]).end();
22+
}
23+
24+
falcon.send();
1325
```
1426

1527
### API
@@ -47,5 +59,3 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
4759
THE SOFTWARE.
4860

4961
---
50-
![docor]()
51-
built upon love by [docor](git+https://github.com/turingou/docor.git) v0.3.0

index.js

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
var os = require('os');
2-
var _ = require('lodash');
31
var request = require('superagent');
42
var debug = require('debug')('open-falcon');
53

@@ -12,10 +10,25 @@ function Falcon(options){
1210
api: options
1311
};
1412
}
15-
16-
this.options = options;
17-
this.data = {};
18-
this.queue = [];
13+
var self = this, defaults = {
14+
tags : '' ,
15+
step : 60 ,
16+
value : 0 ,
17+
endpoint : 'localhost' ,
18+
counterType : 'GAUGE' ,
19+
api : 'http://127.0.0.1:1988/v1/push',
20+
timestamp : function(){
21+
return Math.floor(+new Date() / 1000);
22+
},
23+
};
24+
//
25+
for(var key in options){
26+
defaults[ key ] = options[ key ];
27+
}
28+
//
29+
this.data = {};
30+
this.queue = [];
31+
this.options = defaults;
1932
return this;
2033
}
2134

@@ -24,7 +37,6 @@ Falcon.COUNTER_TYPE = {
2437
COUNTER: 'COUNTER'
2538
};
2639

27-
2840
Falcon.prototype.use = function(middleware){
2941
middleware.apply(this, [ this ]);
3042
return this;
@@ -35,8 +47,13 @@ Falcon.prototype.set = function(key, value){
3547
return this;
3648
};
3749

38-
Falcon.prototype.metric = function(value){
39-
this.set.apply(this, [ 'metric', value ]);
50+
Falcon.prototype.metric = function(name, value){
51+
if(name){
52+
this.set('metric', name);
53+
}
54+
if(value){
55+
this.set('value', value);
56+
}
4057
return this;
4158
};
4259
/**
@@ -76,7 +93,7 @@ Falcon.prototype.step = function(value){
7693
* @return {[type]} [description]
7794
*/
7895
Falcon.prototype.type = function(value){
79-
this.set.apply(this, [ 'counterType', value ]);
96+
this.set.apply(this, [ 'counterType', value.toUpperCase() ]);
8097
return this;
8198
};
8299
/**
@@ -92,25 +109,28 @@ Falcon.prototype.tags = function(tags){
92109
return this;
93110
};
94111

95-
Falcon.prototype.defaults = function(name){
96-
switch(name){
97-
case 'endpoint':
98-
return os.hostname();
99-
case 'timestamp':
100-
return +new Date();
101-
case 'counterType':
102-
return 'COUNTER';
103-
case 'step':
104-
return 60;
105-
}
106-
}
107-
108112
Falcon.prototype.end = function(){
109113
var self = this;
110-
[ 'endpoint', 'timestamp', 'metric', 'value', 'step', 'counterType', 'tags' ].map(function(key){
111-
self.data[ key ] = self.data[ key ] || self.defaults(key);
112-
if(typeof self.data[ key ] == 'undefined'){
113-
throw new Error( `'${key}' is required .`)
114+
([
115+
'step' ,
116+
'tags' ,
117+
'value' ,
118+
'metric' ,
119+
'endpoint' ,
120+
'timestamp' ,
121+
'counterType'
122+
]).map(function(key){
123+
if(
124+
typeof self.data[ key ] == 'undefined' &&
125+
typeof self.options[key] == 'undefined'
126+
) throw new Error( `'${key}' is required .`);
127+
//
128+
if(typeof self.data[key] == 'undefined'){
129+
if(typeof self.options[key] == 'function'){
130+
self.data[ key ] = self.options[key](self);
131+
}else if(typeof self.options[key] != 'undefined'){
132+
self.data[ key ] = self.options[key];
133+
}
114134
}
115135
});
116136
this.queue.push(this.data);
@@ -134,40 +154,4 @@ Falcon.prototype.send = function(callback){
134154
return this;
135155
};
136156

137-
138-
Falcon.memory = function(name){
139-
return function(f){
140-
var usage = process.memoryUsage();
141-
142-
f
143-
.metric(`${name}.rss`)
144-
.value(usage.rss)
145-
.type(Falcon.COUNTER_TYPE.GAUGE)
146-
.tags()
147-
.end()
148-
149-
f
150-
.metric(`${name}.heapTotal`)
151-
.value(usage.heapTotal)
152-
.type(Falcon.COUNTER_TYPE.GAUGE)
153-
.tags()
154-
.end()
155-
156-
f
157-
.metric(`${name}.heapUsed`)
158-
.value(usage.heapUsed)
159-
.type(Falcon.COUNTER_TYPE.GAUGE)
160-
.tags()
161-
.end()
162-
163-
};
164-
};
165-
166-
Falcon.cpu = function(name){
167-
return function(f){
168-
f.metric(name);
169-
};
170-
};
171-
172-
173157
module.exports = Falcon;

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-open-falcon",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "open-falcon",
55
"main": "index.js",
66
"scripts": {
@@ -21,8 +21,7 @@
2121
},
2222
"homepage": "https://github.com/song940/node-open-falcon#readme",
2323
"dependencies": {
24-
"debug": "^2.2.0",
25-
"lodash": "^3.10.1",
26-
"superagent": "^1.4.0"
24+
"debug": "latest",
25+
"superagent": "latest"
2726
}
2827
}

test/test.js

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,13 @@
1+
var os = require('os');
12
var Falcon = require('../');
23

3-
var falcon = new Falcon(
4-
'http://127.0.0.1:1988/v1/push'
5-
);
4+
var falcon = new Falcon({
5+
endpoint: os.hostname()
6+
});
67
//
7-
// falcon
8-
// .metric('cpu.idle')
9-
// .endpoint()
10-
// .timestamp()
11-
// .value(1)
12-
// .step(20)
13-
// .type(Falcon.COUNTER_TYPE.COUNTER)
14-
// .tags({name: 'aa', 'bb':1})
15-
// .end()
16-
// // .send()
17-
// .use(function(f){
18-
// f.metric('test')
19-
// f.value('aa');
20-
// f.step(60);
21-
// f.type('s');
22-
// f.tags({a:1});
23-
// })
24-
// .use(function(f){
25-
// f.metric('a')
26-
// f.value('aa');
27-
// f.step(60);
28-
// f.type('s');
29-
// f.tags({a:2});
30-
// })
31-
// .send(function(err, res){
32-
// console.log(res);
33-
// })
8+
var usage = process.memoryUsage();
9+
for(var key in usage){
10+
falcon.metric('memory.' + key, usage[key]).end();
11+
}
3412

35-
setInterval(function(){
36-
37-
falcon
38-
.use(Falcon.memory('memory'))
39-
.send()
40-
41-
}, 1000);
13+
falcon.send();

0 commit comments

Comments
 (0)