@@ -48,15 +48,20 @@ var (
4848)
4949
5050func TestDistributor_Push (t * testing.T ) {
51- metricNames := []string {
52- "cortex_distributor_latest_seen_sample_timestamp_seconds" ,
53- }
51+ // Metrics to assert on.
52+ lastSeenTimestamp := "cortex_distributor_latest_seen_sample_timestamp_seconds"
53+ distributorAppend := "cortex_distributor_ingester_appends_total"
54+ distributorAppendFailure := "cortex_distributor_ingester_append_failures_total"
5455
56+ type samplesIn struct {
57+ num int
58+ startTimestampMs int64
59+ }
5560 for name , tc := range map [string ]struct {
61+ metricNames []string
5662 numIngesters int
5763 happyIngesters int
58- samples int
59- startTimestampMs int64
64+ samples samplesIn
6065 metadata int
6166 expectedResponse * client.WriteResponse
6267 expectedError error
@@ -70,10 +75,10 @@ func TestDistributor_Push(t *testing.T) {
7075 "A push to 3 happy ingesters should succeed" : {
7176 numIngesters : 3 ,
7277 happyIngesters : 3 ,
73- samples : 5 ,
78+ samples : samplesIn { num : 5 , startTimestampMs : 123456789000 } ,
7479 metadata : 5 ,
7580 expectedResponse : success ,
76- startTimestampMs : 123456789000 ,
81+ metricNames : [] string { lastSeenTimestamp } ,
7782 expectedMetrics : `
7883 # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user.
7984 # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge
@@ -83,57 +88,95 @@ func TestDistributor_Push(t *testing.T) {
8388 "A push to 2 happy ingesters should succeed" : {
8489 numIngesters : 3 ,
8590 happyIngesters : 2 ,
86- samples : 5 ,
91+ samples : samplesIn { num : 5 , startTimestampMs : 123456789000 } ,
8792 metadata : 5 ,
8893 expectedResponse : success ,
89- startTimestampMs : 123456789000 ,
94+ metricNames : [] string { lastSeenTimestamp } ,
9095 expectedMetrics : `
9196 # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user.
9297 # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge
9398 cortex_distributor_latest_seen_sample_timestamp_seconds{user="user"} 123456789.004
9499 ` ,
95100 },
96101 "A push to 1 happy ingesters should fail" : {
97- numIngesters : 3 ,
98- happyIngesters : 1 ,
99- samples : 10 ,
100- expectedError : errFail ,
101- startTimestampMs : 123456789000 ,
102+ numIngesters : 3 ,
103+ happyIngesters : 1 ,
104+ samples : samplesIn { num : 10 , startTimestampMs : 123456789000 } ,
105+ expectedError : errFail ,
106+ metricNames : [] string { lastSeenTimestamp } ,
102107 expectedMetrics : `
103108 # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user.
104109 # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge
105110 cortex_distributor_latest_seen_sample_timestamp_seconds{user="user"} 123456789.009
106111 ` ,
107112 },
108113 "A push to 0 happy ingesters should fail" : {
109- numIngesters : 3 ,
110- happyIngesters : 0 ,
111- samples : 10 ,
112- expectedError : errFail ,
113- startTimestampMs : 123456789000 ,
114+ numIngesters : 3 ,
115+ happyIngesters : 0 ,
116+ samples : samplesIn { num : 10 , startTimestampMs : 123456789000 } ,
117+ expectedError : errFail ,
118+ metricNames : [] string { lastSeenTimestamp } ,
114119 expectedMetrics : `
115120 # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user.
116121 # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge
117122 cortex_distributor_latest_seen_sample_timestamp_seconds{user="user"} 123456789.009
118123 ` ,
119124 },
120125 "A push exceeding burst size should fail" : {
121- numIngesters : 3 ,
122- happyIngesters : 3 ,
123- samples : 25 ,
124- metadata : 5 ,
125- expectedError : httpgrpc .Errorf (http .StatusTooManyRequests , "ingestion rate limit (20) exceeded while adding 25 samples and 5 metadata" ),
126- startTimestampMs : 123456789000 ,
126+ numIngesters : 3 ,
127+ happyIngesters : 3 ,
128+ samples : samplesIn { num : 25 , startTimestampMs : 123456789000 } ,
129+ metadata : 5 ,
130+ expectedError : httpgrpc .Errorf (http .StatusTooManyRequests , "ingestion rate limit (20) exceeded while adding 25 samples and 5 metadata" ),
131+ metricNames : [] string { lastSeenTimestamp } ,
127132 expectedMetrics : `
128133 # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user.
129134 # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge
130135 cortex_distributor_latest_seen_sample_timestamp_seconds{user="user"} 123456789.024
131136 ` ,
132137 },
138+ "A push to ingesters should report the correct metrics with no metadata" : {
139+ numIngesters : 3 ,
140+ happyIngesters : 2 ,
141+ samples : samplesIn {num : 1 , startTimestampMs : 123456789000 },
142+ metadata : 0 ,
143+ metricNames : []string {distributorAppend , distributorAppendFailure },
144+ expectedResponse : success ,
145+ expectedMetrics : `
146+ # HELP cortex_distributor_ingester_append_failures_total The total number of failed batch appends sent to ingesters.
147+ # TYPE cortex_distributor_ingester_append_failures_total counter
148+ cortex_distributor_ingester_append_failures_total{ingester="2",type="samples"} 1
149+ # HELP cortex_distributor_ingester_appends_total The total number of batch appends sent to ingesters.
150+ # TYPE cortex_distributor_ingester_appends_total counter
151+ cortex_distributor_ingester_appends_total{ingester="0",type="samples"} 1
152+ cortex_distributor_ingester_appends_total{ingester="1",type="samples"} 1
153+ cortex_distributor_ingester_appends_total{ingester="2",type="samples"} 1
154+ ` ,
155+ },
156+ "A push to ingesters should report the correct metrics with no samples" : {
157+ numIngesters : 3 ,
158+ happyIngesters : 2 ,
159+ samples : samplesIn {num : 0 , startTimestampMs : 123456789000 },
160+ metadata : 1 ,
161+ metricNames : []string {distributorAppend , distributorAppendFailure },
162+ expectedResponse : success ,
163+ expectedMetrics : `
164+ # HELP cortex_distributor_ingester_append_failures_total The total number of failed batch appends sent to ingesters.
165+ # TYPE cortex_distributor_ingester_append_failures_total counter
166+ cortex_distributor_ingester_append_failures_total{ingester="2",type="metadata"} 1
167+ # HELP cortex_distributor_ingester_appends_total The total number of batch appends sent to ingesters.
168+ # TYPE cortex_distributor_ingester_appends_total counter
169+ cortex_distributor_ingester_appends_total{ingester="0",type="metadata"} 1
170+ cortex_distributor_ingester_appends_total{ingester="1",type="metadata"} 1
171+ cortex_distributor_ingester_appends_total{ingester="2",type="metadata"} 1
172+ ` ,
173+ },
133174 } {
134175 for _ , shardByAllLabels := range []bool {true , false } {
135176 t .Run (fmt .Sprintf ("[%s](shardByAllLabels=%v)" , name , shardByAllLabels ), func (t * testing.T ) {
136177 latestSeenSampleTimestampPerUser .Reset ()
178+ ingesterAppends .Reset ()
179+ ingesterAppendFailures .Reset ()
137180
138181 limits := & validation.Limits {}
139182 flagext .DefaultValues (limits )
@@ -143,14 +186,14 @@ func TestDistributor_Push(t *testing.T) {
143186 d , _ := prepare (t , tc .numIngesters , tc .happyIngesters , 0 , shardByAllLabels , limits , nil )
144187 defer services .StopAndAwaitTerminated (context .Background (), d ) //nolint:errcheck
145188
146- request := makeWriteRequest (tc .startTimestampMs , tc .samples , tc .metadata )
189+ request := makeWriteRequest (tc .samples . startTimestampMs , tc .samples . num , tc .metadata )
147190 response , err := d .Push (ctx , request )
148191 assert .Equal (t , tc .expectedResponse , response )
149192 assert .Equal (t , tc .expectedError , err )
150193
151194 // Check tracked Prometheus metrics.
152195 if tc .expectedMetrics != "" {
153- err = testutil .GatherAndCompare (prometheus .DefaultGatherer , strings .NewReader (tc .expectedMetrics ), metricNames ... )
196+ err = testutil .GatherAndCompare (prometheus .DefaultGatherer , strings .NewReader (tc .expectedMetrics ), tc . metricNames ... )
154197 assert .NoError (t , err )
155198 }
156199 })
0 commit comments