@@ -11,72 +11,72 @@ class RumInjectorMetricsTest extends Specification {
1111 def statsD = Mock (StatsDClient )
1212
1313 @Subject
14- def healthMetrics = new RumInjectorMetrics (statsD)
14+ def metrics = new RumInjectorMetrics (statsD)
1515
1616 def " test onInjectionSucceed" () {
1717 setup :
1818 def latch = new CountDownLatch (1 )
19- def healthMetrics = new RumInjectorMetrics (new Latched (statsD, latch), 10 , TimeUnit . MILLISECONDS )
20- healthMetrics . start()
19+ def metrics = new RumInjectorMetrics (new Latched (statsD, latch), 10 , TimeUnit . MILLISECONDS )
20+ metrics . start()
2121
2222 when :
23- healthMetrics . onInjectionSucceed()
23+ metrics . onInjectionSucceed()
2424 latch. await(5 , TimeUnit . SECONDS )
2525
2626 then :
2727 1 * statsD. count(' rum.injection.succeed' , 1 , _)
2828 0 * _
2929
3030 cleanup :
31- healthMetrics . close()
31+ metrics . close()
3232 }
3333
3434 def " test onInjectionFailed" () {
3535 setup :
3636 def latch = new CountDownLatch (1 )
37- def healthMetrics = new RumInjectorMetrics (new Latched (statsD, latch), 10 , TimeUnit . MILLISECONDS )
38- healthMetrics . start()
37+ def metrics = new RumInjectorMetrics (new Latched (statsD, latch), 10 , TimeUnit . MILLISECONDS )
38+ metrics . start()
3939
4040 when :
41- healthMetrics . onInjectionFailed()
41+ metrics . onInjectionFailed()
4242 latch. await(5 , TimeUnit . SECONDS )
4343
4444 then :
4545 1 * statsD. count(' rum.injection.failed' , 1 , _)
4646 0 * _
4747
4848 cleanup :
49- healthMetrics . close()
49+ metrics . close()
5050 }
5151
5252 def " test onInjectionSkipped" () {
5353 setup :
5454 def latch = new CountDownLatch (1 )
55- def healthMetrics = new RumInjectorMetrics (new Latched (statsD, latch), 10 , TimeUnit . MILLISECONDS )
56- healthMetrics . start()
55+ def metrics = new RumInjectorMetrics (new Latched (statsD, latch), 10 , TimeUnit . MILLISECONDS )
56+ metrics . start()
5757
5858 when :
59- healthMetrics . onInjectionSkipped()
59+ metrics . onInjectionSkipped()
6060 latch. await(5 , TimeUnit . SECONDS )
6161
6262 then :
6363 1 * statsD. count(' rum.injection.skipped' , 1 , _)
6464 0 * _
6565
6666 cleanup :
67- healthMetrics . close()
67+ metrics . close()
6868 }
6969
70- def " test multiple events" () {
70+ def " test flushing multiple events" () {
7171 setup :
7272 def latch = new CountDownLatch (3 ) // expecting 3 metric types
73- def healthMetrics = new RumInjectorMetrics (new Latched (statsD, latch), 10 , TimeUnit . MILLISECONDS )
74- healthMetrics . start()
73+ def metrics = new RumInjectorMetrics (new Latched (statsD, latch), 10 , TimeUnit . MILLISECONDS )
74+ metrics . start()
7575
7676 when :
77- healthMetrics . onInjectionSucceed()
78- healthMetrics . onInjectionFailed()
79- healthMetrics . onInjectionSkipped()
77+ metrics . onInjectionSucceed()
78+ metrics . onInjectionFailed()
79+ metrics . onInjectionSkipped()
8080 latch. await(5 , TimeUnit . SECONDS )
8181
8282 then :
@@ -86,23 +86,59 @@ class RumInjectorMetricsTest extends Specification {
8686 0 * _
8787
8888 cleanup :
89- healthMetrics . close()
89+ metrics . close()
9090 }
9191
92- def " test summary" () {
92+ def " test that flushing only reports non-zero deltas" () {
93+ setup :
94+ def latch = new CountDownLatch (1 ) // expecting only 1 metric call (non-zero delta)
95+ def metrics = new RumInjectorMetrics (new Latched (statsD, latch), 10 , TimeUnit . MILLISECONDS )
96+ metrics. start()
97+
9398 when :
94- healthMetrics. onInjectionSucceed()
95- healthMetrics. onInjectionFailed()
96- healthMetrics. onInjectionSkipped()
97- def summary = healthMetrics. summary()
99+ metrics. onInjectionSucceed()
100+ metrics. onInjectionSucceed()
101+ latch. await(5 , TimeUnit . SECONDS )
98102
99103 then :
100- summary. contains(" injectionSucceed=1" )
101- summary. contains(" injectionFailed=1" )
104+ 1 * statsD. count(' rum.injection.succeed' , 2 , _)
105+ // should not be called since they have delta of 0
106+ 0 * statsD. count(' rum.injection.failed' , _, _)
107+ 0 * statsD. count(' rum.injection.skipped' , _, _)
108+ 0 * _
109+
110+ cleanup :
111+ metrics. close()
112+ }
113+
114+ def " test summary with multiple events" () {
115+ when :
116+ metrics. onInjectionSucceed()
117+ metrics. onInjectionFailed()
118+ metrics. onInjectionSucceed()
119+ metrics. onInjectionFailed()
120+ metrics. onInjectionSucceed()
121+ metrics. onInjectionSkipped()
122+ def summary = metrics. summary()
123+
124+ then :
125+ summary. contains(" injectionSucceed=3" )
126+ summary. contains(" injectionFailed=2" )
102127 summary. contains(" injectionSkipped=1" )
103128 0 * _
104129 }
105130
131+ def " test metrics start at zero" () {
132+ when :
133+ def summary = metrics. summary()
134+
135+ then :
136+ summary. contains(" injectionSucceed=0" )
137+ summary. contains(" injectionFailed=0" )
138+ summary. contains(" injectionSkipped=0" )
139+ 0 * _
140+ }
141+
106142 // taken from HealthMetricsTest
107143 private static class Latched implements StatsDClient {
108144 final StatsDClient delegate
0 commit comments