1- var os = require ( 'os' ) ;
2- var _ = require ( 'lodash' ) ;
31var request = require ( 'superagent' ) ;
42var 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-
2840Falcon . 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 */
7895Falcon . 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-
108112Falcon . 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-
173157module . exports = Falcon ;
0 commit comments