@@ -420,6 +420,7 @@ func TestDistributor_MetricsCleanup(t *testing.T) {
420
420
"cortex_distributor_metadata_in_total" ,
421
421
"cortex_distributor_non_ha_samples_received_total" ,
422
422
"cortex_distributor_latest_seen_sample_timestamp_seconds" ,
423
+ "cortex_distributor_received_samples_per_labelset_total" ,
423
424
}
424
425
425
426
allMetrics := append (removedMetrics , permanentMetrics ... )
@@ -438,6 +439,8 @@ func TestDistributor_MetricsCleanup(t *testing.T) {
438
439
d .nonHASamples .WithLabelValues ("userA" ).Add (5 )
439
440
d .dedupedSamples .WithLabelValues ("userA" , "cluster1" ).Inc () // We cannot clean this metric
440
441
d .latestSeenSampleTimestampPerUser .WithLabelValues ("userA" ).Set (1111 )
442
+ d .receivedSamplesPerLabelSet .WithLabelValues ("userA" , sampleMetricTypeFloat , "{}" ).Add (5 )
443
+ d .receivedSamplesPerLabelSet .WithLabelValues ("userA" , sampleMetricTypeHistogram , "{}" ).Add (10 )
441
444
442
445
h , _ , _ := r .GetAllInstanceDescs (ring .WriteNoExtend )
443
446
ingId0 , _ := r .GetInstanceIdByAddr (h [0 ].Addr )
@@ -473,6 +476,11 @@ func TestDistributor_MetricsCleanup(t *testing.T) {
473
476
cortex_distributor_received_metadata_total{user="userA"} 5
474
477
cortex_distributor_received_metadata_total{user="userB"} 10
475
478
479
+ # HELP cortex_distributor_received_samples_per_labelset_total The total number of received samples per label set, excluding rejected and deduped samples.
480
+ # TYPE cortex_distributor_received_samples_per_labelset_total counter
481
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="float",user="userA"} 5
482
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="histogram",user="userA"} 10
483
+
476
484
# HELP cortex_distributor_received_samples_total The total number of received samples, excluding rejected and deduped samples.
477
485
# TYPE cortex_distributor_received_samples_total counter
478
486
cortex_distributor_received_samples_total{type="float",user="userA"} 5
@@ -4081,6 +4089,129 @@ func TestDistributor_Push_RelabelDropWillExportMetricOfDroppedSamples(t *testing
4081
4089
}
4082
4090
}
4083
4091
4092
+ func TestDistributor_PushLabelSetMetrics (t * testing.T ) {
4093
+ t .Parallel ()
4094
+ inputSeries := []labels.Labels {
4095
+ {
4096
+ {Name : "__name__" , Value : "foo" },
4097
+ {Name : "cluster" , Value : "one" },
4098
+ },
4099
+ {
4100
+ {Name : "__name__" , Value : "bar" },
4101
+ {Name : "cluster" , Value : "one" },
4102
+ },
4103
+ {
4104
+ {Name : "__name__" , Value : "bar" },
4105
+ {Name : "cluster" , Value : "two" },
4106
+ },
4107
+ {
4108
+ {Name : "__name__" , Value : "foo" },
4109
+ {Name : "cluster" , Value : "three" },
4110
+ },
4111
+ }
4112
+
4113
+ var err error
4114
+ var limits validation.Limits
4115
+ flagext .DefaultValues (& limits )
4116
+ limits .LimitsPerLabelSet = []validation.LimitsPerLabelSet {
4117
+ {Hash : 0 , LabelSet : labels .FromStrings ("cluster" , "one" )},
4118
+ {Hash : 1 , LabelSet : labels .FromStrings ("cluster" , "two" )},
4119
+ {Hash : 2 , LabelSet : labels .EmptyLabels ()},
4120
+ }
4121
+
4122
+ ds , _ , regs , _ := prepare (t , prepConfig {
4123
+ numIngesters : 2 ,
4124
+ happyIngesters : 2 ,
4125
+ numDistributors : 1 ,
4126
+ shardByAllLabels : true ,
4127
+ limits : & limits ,
4128
+ })
4129
+ reg := regs [0 ]
4130
+
4131
+ // Push the series to the distributor
4132
+ id := "user"
4133
+ req := mockWriteRequest (inputSeries , 1 , 1 , false )
4134
+ ctx := user .InjectOrgID (context .Background (), id )
4135
+ _ , err = ds [0 ].Push (ctx , req )
4136
+ require .NoError (t , err )
4137
+
4138
+ ds [0 ].updateLabelSetMetrics ()
4139
+ require .NoError (t , testutil .GatherAndCompare (reg , strings .NewReader (`
4140
+ # HELP cortex_distributor_received_samples_per_labelset_total The total number of received samples per label set, excluding rejected and deduped samples.
4141
+ # TYPE cortex_distributor_received_samples_per_labelset_total counter
4142
+ cortex_distributor_received_samples_per_labelset_total{labelset="{cluster=\"one\"}",type="float",user="user"} 2
4143
+ cortex_distributor_received_samples_per_labelset_total{labelset="{cluster=\"one\"}",type="histogram",user="user"} 0
4144
+ cortex_distributor_received_samples_per_labelset_total{labelset="{cluster=\"two\"}",type="float",user="user"} 1
4145
+ cortex_distributor_received_samples_per_labelset_total{labelset="{cluster=\"two\"}",type="histogram",user="user"} 0
4146
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="float",user="user"} 1
4147
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="histogram",user="user"} 0
4148
+ ` ), "cortex_distributor_received_samples_per_labelset_total" ))
4149
+
4150
+ // Push more series.
4151
+ inputSeries = []labels.Labels {
4152
+ {
4153
+ {Name : "__name__" , Value : "baz" },
4154
+ {Name : "cluster" , Value : "two" },
4155
+ },
4156
+ {
4157
+ {Name : "__name__" , Value : "foo" },
4158
+ {Name : "cluster" , Value : "four" },
4159
+ },
4160
+ }
4161
+ // Write the same request twice for different users.
4162
+ req = mockWriteRequest (inputSeries , 1 , 1 , false )
4163
+ ctx2 := user .InjectOrgID (context .Background (), "user2" )
4164
+ _ , err = ds [0 ].Push (ctx , req )
4165
+ require .NoError (t , err )
4166
+ req = mockWriteRequest (inputSeries , 1 , 1 , false )
4167
+ _ , err = ds [0 ].Push (ctx2 , req )
4168
+ require .NoError (t , err )
4169
+ ds [0 ].updateLabelSetMetrics ()
4170
+ require .NoError (t , testutil .GatherAndCompare (reg , strings .NewReader (`
4171
+ # HELP cortex_distributor_received_samples_per_labelset_total The total number of received samples per label set, excluding rejected and deduped samples.
4172
+ # TYPE cortex_distributor_received_samples_per_labelset_total counter
4173
+ cortex_distributor_received_samples_per_labelset_total{labelset="{cluster=\"one\"}",type="float",user="user"} 2
4174
+ cortex_distributor_received_samples_per_labelset_total{labelset="{cluster=\"one\"}",type="histogram",user="user"} 0
4175
+ cortex_distributor_received_samples_per_labelset_total{labelset="{cluster=\"two\"}",type="float",user="user"} 2
4176
+ cortex_distributor_received_samples_per_labelset_total{labelset="{cluster=\"two\"}",type="float",user="user2"} 1
4177
+ cortex_distributor_received_samples_per_labelset_total{labelset="{cluster=\"two\"}",type="histogram",user="user"} 0
4178
+ cortex_distributor_received_samples_per_labelset_total{labelset="{cluster=\"two\"}",type="histogram",user="user2"} 0
4179
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="float",user="user"} 2
4180
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="float",user="user2"} 1
4181
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="histogram",user="user"} 0
4182
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="histogram",user="user2"} 0
4183
+ ` ), "cortex_distributor_received_samples_per_labelset_total" ))
4184
+
4185
+ // Remove existing limits and add new limits
4186
+ limits .LimitsPerLabelSet = []validation.LimitsPerLabelSet {
4187
+ {Hash : 3 , LabelSet : labels .FromStrings ("cluster" , "three" )},
4188
+ {Hash : 4 , LabelSet : labels .FromStrings ("cluster" , "four" )},
4189
+ {Hash : 2 , LabelSet : labels .EmptyLabels ()},
4190
+ }
4191
+ ds [0 ].limits , err = validation .NewOverrides (limits , nil )
4192
+ require .NoError (t , err )
4193
+ ds [0 ].updateLabelSetMetrics ()
4194
+ // Old label set metrics are removed. New label set metrics will be added when
4195
+ // new requests come in.
4196
+ require .NoError (t , testutil .GatherAndCompare (reg , strings .NewReader (`
4197
+ # HELP cortex_distributor_received_samples_per_labelset_total The total number of received samples per label set, excluding rejected and deduped samples.
4198
+ # TYPE cortex_distributor_received_samples_per_labelset_total counter
4199
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="float",user="user"} 2
4200
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="float",user="user2"} 1
4201
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="histogram",user="user"} 0
4202
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="histogram",user="user2"} 0
4203
+ ` ), "cortex_distributor_received_samples_per_labelset_total" ))
4204
+
4205
+ // Metrics from `user` got removed but `user2` metric should remain.
4206
+ ds [0 ].cleanupInactiveUser (id )
4207
+ require .NoError (t , testutil .GatherAndCompare (reg , strings .NewReader (`
4208
+ # HELP cortex_distributor_received_samples_per_labelset_total The total number of received samples per label set, excluding rejected and deduped samples.
4209
+ # TYPE cortex_distributor_received_samples_per_labelset_total counter
4210
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="float",user="user2"} 1
4211
+ cortex_distributor_received_samples_per_labelset_total{labelset="{}",type="histogram",user="user2"} 0
4212
+ ` ), "cortex_distributor_received_samples_per_labelset_total" ))
4213
+ }
4214
+
4084
4215
func countMockIngestersCalls (ingesters []* mockIngester , name string ) int {
4085
4216
count := 0
4086
4217
for i := 0 ; i < len (ingesters ); i ++ {
0 commit comments