4
4
"context"
5
5
"database/sql"
6
6
"testing"
7
- "time"
8
7
9
8
"github.com/prometheus/client_golang/prometheus"
10
9
"github.com/stretchr/testify/assert"
@@ -33,16 +32,18 @@ func TestPGPubsub_Metrics(t *testing.T) {
33
32
registry := prometheus .NewRegistry ()
34
33
ctx := testutil .Context (t , testutil .WaitLong )
35
34
36
- latencyMeasureInterval := time .Second
37
- start := time .Now ()
38
- uut , err := pubsub .New (ctx , logger , db , connectionURL , latencyMeasureInterval )
35
+ uut , err := pubsub .New (ctx , logger , db , connectionURL )
39
36
require .NoError (t , err )
40
37
defer uut .Close ()
41
38
42
39
err = registry .Register (uut )
43
40
require .NoError (t , err )
44
41
42
+ // each Gather measures pubsub latency by publishing a message & subscribing to it
43
+ var gatherCount float64
44
+
45
45
metrics , err := registry .Gather ()
46
+ gatherCount ++
46
47
require .NoError (t , err )
47
48
require .True (t , testutil .PromGaugeHasValue (t , metrics , 0 , "coder_pubsub_current_events" ))
48
49
require .True (t , testutil .PromGaugeHasValue (t , metrics , 0 , "coder_pubsub_current_subscribers" ))
@@ -62,24 +63,21 @@ func TestPGPubsub_Metrics(t *testing.T) {
62
63
_ = testutil .RequireRecvCtx (ctx , t , messageChannel )
63
64
64
65
require .Eventually (t , func () bool {
66
+ latencyBytes := gatherCount * pubsub .LatencyMessageLength
65
67
metrics , err = registry .Gather ()
68
+ gatherCount ++
66
69
assert .NoError (t , err )
67
-
68
- latencyMeasurements := (time .Since (start ).Truncate (latencyMeasureInterval ).Seconds () / latencyMeasureInterval .Seconds ()) + 1
69
- t .Log (latencyMeasurements )
70
- latencyMsgSize := pubsub .LatencyMessageLength * latencyMeasurements
71
-
72
70
return testutil .PromGaugeHasValue (t , metrics , 1 , "coder_pubsub_current_events" ) &&
73
71
testutil .PromGaugeHasValue (t , metrics , 1 , "coder_pubsub_current_subscribers" ) &&
74
72
testutil .PromGaugeHasValue (t , metrics , 1 , "coder_pubsub_connected" ) &&
75
- testutil .PromCounterHasValue (t , metrics , 1 + latencyMeasurements , "coder_pubsub_publishes_total" , "true" ) &&
76
- testutil .PromCounterHasValue (t , metrics , 1 + latencyMeasurements , "coder_pubsub_subscribes_total" , "true" ) &&
77
- testutil .PromCounterHasValue (t , metrics , 1 + latencyMeasurements , "coder_pubsub_messages_total" , "normal" ) &&
78
- testutil .PromCounterHasValue (t , metrics , float64 (len (data ))+ latencyMsgSize , "coder_pubsub_received_bytes_total" ) &&
79
- testutil .PromCounterHasValue (t , metrics , float64 (len (data ))+ latencyMsgSize , "coder_pubsub_published_bytes_total" ) &&
73
+ testutil .PromCounterHasValue (t , metrics , gatherCount , "coder_pubsub_publishes_total" , "true" ) &&
74
+ testutil .PromCounterHasValue (t , metrics , gatherCount , "coder_pubsub_subscribes_total" , "true" ) &&
75
+ testutil .PromCounterHasValue (t , metrics , gatherCount , "coder_pubsub_messages_total" , "normal" ) &&
76
+ testutil .PromCounterHasValue (t , metrics , float64 (len (data ))+ latencyBytes , "coder_pubsub_received_bytes_total" ) &&
77
+ testutil .PromCounterHasValue (t , metrics , float64 (len (data ))+ latencyBytes , "coder_pubsub_published_bytes_total" ) &&
80
78
testutil .PromGaugeAssertion (t , metrics , func (in float64 ) bool { return in > 0 }, "coder_pubsub_send_latency_seconds" ) &&
81
79
testutil .PromGaugeAssertion (t , metrics , func (in float64 ) bool { return in > 0 }, "coder_pubsub_receive_latency_seconds" ) &&
82
- testutil .PromCounterHasValue (t , metrics , latencyMeasurements , "coder_pubsub_latency_measures_total" ) &&
80
+ testutil .PromCounterHasValue (t , metrics , gatherCount , "coder_pubsub_latency_measures_total" ) &&
83
81
! testutil .PromCounterGathered (t , metrics , "coder_pubsub_latency_measure_errs_total" )
84
82
}, testutil .WaitShort , testutil .IntervalFast )
85
83
@@ -102,25 +100,22 @@ func TestPGPubsub_Metrics(t *testing.T) {
102
100
_ = testutil .RequireRecvCtx (ctx , t , messageChannel )
103
101
104
102
require .Eventually (t , func () bool {
103
+ latencyBytes := gatherCount * pubsub .LatencyMessageLength
105
104
metrics , err = registry .Gather ()
105
+ gatherCount ++
106
106
assert .NoError (t , err )
107
-
108
- latencyMeasurements := (time .Since (start ).Truncate (latencyMeasureInterval ).Seconds () / latencyMeasureInterval .Seconds ()) + 1
109
- latencyMsgSize := pubsub .LatencyMessageLength * latencyMeasurements
110
- t .Log (latencyMeasurements )
111
-
112
107
return testutil .PromGaugeHasValue (t , metrics , 1 , "coder_pubsub_current_events" ) &&
113
108
testutil .PromGaugeHasValue (t , metrics , 2 , "coder_pubsub_current_subscribers" ) &&
114
109
testutil .PromGaugeHasValue (t , metrics , 1 , "coder_pubsub_connected" ) &&
115
- testutil .PromCounterHasValue (t , metrics , 2 + latencyMeasurements , "coder_pubsub_publishes_total" , "true" ) &&
116
- testutil .PromCounterHasValue (t , metrics , 2 + latencyMeasurements , "coder_pubsub_subscribes_total" , "true" ) &&
117
- testutil .PromCounterHasValue (t , metrics , 1 + latencyMeasurements , "coder_pubsub_messages_total" , "normal" ) &&
110
+ testutil .PromCounterHasValue (t , metrics , 1 + gatherCount , "coder_pubsub_publishes_total" , "true" ) &&
111
+ testutil .PromCounterHasValue (t , metrics , 1 + gatherCount , "coder_pubsub_subscribes_total" , "true" ) &&
112
+ testutil .PromCounterHasValue (t , metrics , gatherCount , "coder_pubsub_messages_total" , "normal" ) &&
118
113
testutil .PromCounterHasValue (t , metrics , 1 , "coder_pubsub_messages_total" , "colossal" ) &&
119
- testutil .PromCounterHasValue (t , metrics , float64 (colossalSize + len (data ))+ latencyMsgSize , "coder_pubsub_received_bytes_total" ) &&
120
- testutil .PromCounterHasValue (t , metrics , float64 (colossalSize + len (data ))+ latencyMsgSize , "coder_pubsub_published_bytes_total" ) &&
114
+ testutil .PromCounterHasValue (t , metrics , float64 (colossalSize + len (data ))+ latencyBytes , "coder_pubsub_received_bytes_total" ) &&
115
+ testutil .PromCounterHasValue (t , metrics , float64 (colossalSize + len (data ))+ latencyBytes , "coder_pubsub_published_bytes_total" ) &&
121
116
testutil .PromGaugeAssertion (t , metrics , func (in float64 ) bool { return in > 0 }, "coder_pubsub_send_latency_seconds" ) &&
122
117
testutil .PromGaugeAssertion (t , metrics , func (in float64 ) bool { return in > 0 }, "coder_pubsub_receive_latency_seconds" ) &&
123
- testutil .PromCounterHasValue (t , metrics , latencyMeasurements , "coder_pubsub_latency_measures_total" ) &&
118
+ testutil .PromCounterHasValue (t , metrics , gatherCount , "coder_pubsub_latency_measures_total" ) &&
124
119
! testutil .PromCounterGathered (t , metrics , "coder_pubsub_latency_measure_errs_total" )
125
120
}, testutil .WaitShort , testutil .IntervalFast )
126
121
}
0 commit comments