File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,10 @@ function mapToJsonArray (map) {
2525 return Array . from ( map . values ( ) ) . map ( v => v . toJSON ( ) )
2626}
2727
28+ function hasPoints ( metric ) {
29+ return metric . points . length > 0
30+ }
31+
2832class Metric {
2933 constructor ( namespace , metric , common , tags ) {
3034 this . namespace = namespace . toString ( )
@@ -172,10 +176,16 @@ class MetricsCollection extends Map {
172176
173177 toJSON ( ) {
174178 if ( ! this . size ) return
179+
180+ const series = mapToJsonArray ( this )
181+ . filter ( hasPoints )
182+
183+ if ( ! series . length ) return
184+
175185 const { namespace } = this
176186 return {
177187 namespace,
178- series : mapToJsonArray ( this )
188+ series
179189 }
180190 }
181191}
Original file line number Diff line number Diff line change @@ -157,6 +157,33 @@ describe('metrics', () => {
157157 ]
158158 } )
159159 } )
160+
161+ it ( 'should not send empty metrics' , ( ) => {
162+ const manager = new metrics . NamespaceManager ( )
163+
164+ const ns = manager . namespace ( 'test' )
165+
166+ const metric = ns . count ( 'metric' , { bar : 'baz' } )
167+ metric . inc ( )
168+ metric . reset ( )
169+
170+ const config = {
171+ hostname : 'localhost' ,
172+ port : 12345 ,
173+ tags : {
174+ 'runtime-id' : 'abc123'
175+ }
176+ }
177+ const application = {
178+ language_name : 'nodejs' ,
179+ tracer_version : '1.2.3'
180+ }
181+ const host = { }
182+
183+ manager . send ( config , application , host )
184+
185+ expect ( sendData ) . to . not . have . been . called
186+ } )
160187 } )
161188
162189 describe ( 'Namespace' , ( ) => {
@@ -249,6 +276,18 @@ describe('metrics', () => {
249276 }
250277 } )
251278 } )
279+
280+ it ( 'should skip empty metrics' , ( ) => {
281+ const ns = new metrics . Namespace ( 'test' )
282+ const metric = ns . count ( 'foo' , { bar : 'baz' } )
283+ metric . inc ( )
284+ metric . reset ( )
285+
286+ expect ( ns . toJSON ( ) ) . to . deep . equal ( {
287+ distributions : undefined ,
288+ metrics : undefined
289+ } )
290+ } )
252291 } )
253292
254293 describe ( 'CountMetric' , ( ) => {
You can’t perform that action at this time.
0 commit comments