| 1 |
// Copyright 2014 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package prometheus |
| 15 |
|
| 16 |
import ( |
| 17 |
"math" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
"time" |
| 21 |
|
| 22 |
dto "github.com/prometheus/client_model/go" |
| 23 |
"google.golang.org/protobuf/proto" |
| 24 |
"google.golang.org/protobuf/types/known/timestamppb" |
| 25 |
) |
| 26 |
|
| 27 |
func TestCounterAdd(t *testing.T) { |
| 28 |
now := time.Now() |
| 29 |
|
| 30 |
counter := NewCounter(CounterOpts{ |
| 31 |
Name: "test", |
| 32 |
Help: "test help", |
| 33 |
ConstLabels: Labels{"a": "1", "b": "2"}, |
| 34 |
now: func() time.Time { return now }, |
| 35 |
}).(*counter) |
| 36 |
counter.Inc() |
| 37 |
if expected, got := 0.0, math.Float64frombits(counter.valBits); expected != got { |
| 38 |
t.Errorf("Expected %f, got %f.", expected, got) |
| 39 |
} |
| 40 |
if expected, got := uint64(1), counter.valInt; expected != got { |
| 41 |
t.Errorf("Expected %d, got %d.", expected, got) |
| 42 |
} |
| 43 |
counter.Add(42) |
| 44 |
if expected, got := 0.0, math.Float64frombits(counter.valBits); expected != got { |
| 45 |
t.Errorf("Expected %f, got %f.", expected, got) |
| 46 |
} |
| 47 |
if expected, got := uint64(43), counter.valInt; expected != got { |
| 48 |
t.Errorf("Expected %d, got %d.", expected, got) |
| 49 |
} |
| 50 |
|
| 51 |
counter.Add(24.42) |
| 52 |
if expected, got := 24.42, math.Float64frombits(counter.valBits); expected != got { |
| 53 |
t.Errorf("Expected %f, got %f.", expected, got) |
| 54 |
} |
| 55 |
if expected, got := uint64(43), counter.valInt; expected != got { |
| 56 |
t.Errorf("Expected %d, got %d.", expected, got) |
| 57 |
} |
| 58 |
|
| 59 |
if expected, got := "counter cannot decrease in value", decreaseCounter(counter).Error(); expected != got { |
| 60 |
t.Errorf("Expected error %q, got %q.", expected, got) |
| 61 |
} |
| 62 |
|
| 63 |
m := &dto.Metric{} |
| 64 |
counter.Write(m) |
| 65 |
|
| 66 |
expected := &dto.Metric{ |
| 67 |
Label: []*dto.LabelPair{ |
| 68 |
{Name: proto.String("a"), Value: proto.String("1")}, |
| 69 |
{Name: proto.String("b"), Value: proto.String("2")}, |
| 70 |
}, |
| 71 |
Counter: &dto.Counter{ |
| 72 |
Value: proto.Float64(67.42), |
| 73 |
CreatedTimestamp: timestamppb.New(now), |
| 74 |
}, |
| 75 |
} |
| 76 |
if !proto.Equal(expected, m) { |
| 77 |
t.Errorf("expected %q, got %q", expected, m) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func decreaseCounter(c *counter) (err error) { |
| 82 |
defer func() { |
| 83 |
if e := recover(); e != nil { |
| 84 |
err = e.(error) |
| 85 |
} |
| 86 |
}() |
| 87 |
c.Add(-1) |
| 88 |
return nil |
| 89 |
} |
| 90 |
|
| 91 |
func TestCounterVecGetMetricWithInvalidLabelValues(t *testing.T) { |
| 92 |
testCases := []struct { |
| 93 |
desc string |
| 94 |
labels Labels |
| 95 |
}{ |
| 96 |
{ |
| 97 |
desc: "non utf8 label value", |
| 98 |
labels: Labels{"a": "\xFF"}, |
| 99 |
}, |
| 100 |
{ |
| 101 |
desc: "not enough label values", |
| 102 |
labels: Labels{}, |
| 103 |
}, |
| 104 |
{ |
| 105 |
desc: "too many label values", |
| 106 |
labels: Labels{"a": "1", "b": "2"}, |
| 107 |
}, |
| 108 |
} |
| 109 |
|
| 110 |
for _, test := range testCases { |
| 111 |
counterVec := NewCounterVec(CounterOpts{ |
| 112 |
Name: "test", |
| 113 |
}, []string{"a"}) |
| 114 |
|
| 115 |
labelValues := make([]string, 0, len(test.labels)) |
| 116 |
for _, val := range test.labels { |
| 117 |
labelValues = append(labelValues, val) |
| 118 |
} |
| 119 |
|
| 120 |
expectPanic(t, func() { |
| 121 |
counterVec.WithLabelValues(labelValues...) |
| 122 |
}, "WithLabelValues: expected panic because: "+test.desc) |
| 123 |
expectPanic(t, func() { |
| 124 |
counterVec.With(test.labels) |
| 125 |
}, "WithLabelValues: expected panic because: "+test.desc) |
| 126 |
|
| 127 |
if _, err := counterVec.GetMetricWithLabelValues(labelValues...); err == nil { |
| 128 |
t.Errorf("GetMetricWithLabelValues: expected error because: %s", test.desc) |
| 129 |
} |
| 130 |
if _, err := counterVec.GetMetricWith(test.labels); err == nil { |
| 131 |
t.Errorf("GetMetricWith: expected error because: %s", test.desc) |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func expectPanic(t *testing.T, op func(), errorMsg string) { |
| 137 |
defer func() { |
| 138 |
if err := recover(); err == nil { |
| 139 |
t.Error(errorMsg) |
| 140 |
} |
| 141 |
}() |
| 142 |
|
| 143 |
op() |
| 144 |
} |
| 145 |
|
| 146 |
func TestCounterAddInf(t *testing.T) { |
| 147 |
now := time.Now() |
| 148 |
|
| 149 |
counter := NewCounter(CounterOpts{ |
| 150 |
Name: "test", |
| 151 |
Help: "test help", |
| 152 |
now: func() time.Time { return now }, |
| 153 |
}).(*counter) |
| 154 |
|
| 155 |
counter.Inc() |
| 156 |
if expected, got := 0.0, math.Float64frombits(counter.valBits); expected != got { |
| 157 |
t.Errorf("Expected %f, got %f.", expected, got) |
| 158 |
} |
| 159 |
if expected, got := uint64(1), counter.valInt; expected != got { |
| 160 |
t.Errorf("Expected %d, got %d.", expected, got) |
| 161 |
} |
| 162 |
|
| 163 |
counter.Add(math.Inf(1)) |
| 164 |
if expected, got := math.Inf(1), math.Float64frombits(counter.valBits); expected != got { |
| 165 |
t.Errorf("valBits expected %f, got %f.", expected, got) |
| 166 |
} |
| 167 |
if expected, got := uint64(1), counter.valInt; expected != got { |
| 168 |
t.Errorf("valInts expected %d, got %d.", expected, got) |
| 169 |
} |
| 170 |
|
| 171 |
counter.Inc() |
| 172 |
if expected, got := math.Inf(1), math.Float64frombits(counter.valBits); expected != got { |
| 173 |
t.Errorf("Expected %f, got %f.", expected, got) |
| 174 |
} |
| 175 |
if expected, got := uint64(2), counter.valInt; expected != got { |
| 176 |
t.Errorf("Expected %d, got %d.", expected, got) |
| 177 |
} |
| 178 |
|
| 179 |
m := &dto.Metric{} |
| 180 |
counter.Write(m) |
| 181 |
|
| 182 |
expected := &dto.Metric{ |
| 183 |
Counter: &dto.Counter{ |
| 184 |
Value: proto.Float64(math.Inf(1)), |
| 185 |
CreatedTimestamp: timestamppb.New(now), |
| 186 |
}, |
| 187 |
} |
| 188 |
|
| 189 |
if !proto.Equal(expected, m) { |
| 190 |
t.Errorf("expected %q, got %q", expected, m) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func TestCounterAddLarge(t *testing.T) { |
| 195 |
now := time.Now() |
| 196 |
|
| 197 |
counter := NewCounter(CounterOpts{ |
| 198 |
Name: "test", |
| 199 |
Help: "test help", |
| 200 |
now: func() time.Time { return now }, |
| 201 |
}).(*counter) |
| 202 |
|
| 203 |
// large overflows the underlying type and should therefore be stored in valBits. |
| 204 |
large := math.Nextafter(float64(math.MaxUint64), 1e20) |
| 205 |
counter.Add(large) |
| 206 |
if expected, got := large, math.Float64frombits(counter.valBits); expected != got { |
| 207 |
t.Errorf("valBits expected %f, got %f.", expected, got) |
| 208 |
} |
| 209 |
if expected, got := uint64(0), counter.valInt; expected != got { |
| 210 |
t.Errorf("valInts expected %d, got %d.", expected, got) |
| 211 |
} |
| 212 |
|
| 213 |
m := &dto.Metric{} |
| 214 |
counter.Write(m) |
| 215 |
|
| 216 |
expected := &dto.Metric{ |
| 217 |
Counter: &dto.Counter{ |
| 218 |
Value: proto.Float64(large), |
| 219 |
CreatedTimestamp: timestamppb.New(now), |
| 220 |
}, |
| 221 |
} |
| 222 |
|
| 223 |
if !proto.Equal(expected, m) { |
| 224 |
t.Errorf("expected %q, got %q", expected, m) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
func TestCounterAddSmall(t *testing.T) { |
| 229 |
now := time.Now() |
| 230 |
|
| 231 |
counter := NewCounter(CounterOpts{ |
| 232 |
Name: "test", |
| 233 |
Help: "test help", |
| 234 |
now: func() time.Time { return now }, |
| 235 |
}).(*counter) |
| 236 |
|
| 237 |
small := 0.000000000001 |
| 238 |
counter.Add(small) |
| 239 |
if expected, got := small, math.Float64frombits(counter.valBits); expected != got { |
| 240 |
t.Errorf("valBits expected %f, got %f.", expected, got) |
| 241 |
} |
| 242 |
if expected, got := uint64(0), counter.valInt; expected != got { |
| 243 |
t.Errorf("valInts expected %d, got %d.", expected, got) |
| 244 |
} |
| 245 |
|
| 246 |
m := &dto.Metric{} |
| 247 |
counter.Write(m) |
| 248 |
|
| 249 |
expected := &dto.Metric{ |
| 250 |
Counter: &dto.Counter{ |
| 251 |
Value: proto.Float64(small), |
| 252 |
CreatedTimestamp: timestamppb.New(now), |
| 253 |
}, |
| 254 |
} |
| 255 |
|
| 256 |
if !proto.Equal(expected, m) { |
| 257 |
t.Errorf("expected %q, got %q", expected, m) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
func TestCounterExemplar(t *testing.T) { |
| 262 |
now := time.Now() |
| 263 |
|
| 264 |
counter := NewCounter(CounterOpts{ |
| 265 |
Name: "test", |
| 266 |
Help: "test help", |
| 267 |
now: func() time.Time { return now }, |
| 268 |
}).(*counter) |
| 269 |
|
| 270 |
ts := timestamppb.New(now) |
| 271 |
if err := ts.CheckValid(); err != nil { |
| 272 |
t.Fatal(err) |
| 273 |
} |
| 274 |
expectedExemplar := &dto.Exemplar{ |
| 275 |
Label: []*dto.LabelPair{ |
| 276 |
{Name: proto.String("foo"), Value: proto.String("bar")}, |
| 277 |
}, |
| 278 |
Value: proto.Float64(42), |
| 279 |
Timestamp: ts, |
| 280 |
} |
| 281 |
|
| 282 |
counter.AddWithExemplar(42, Labels{"foo": "bar"}) |
| 283 |
if expected, got := expectedExemplar.String(), counter.exemplar.Load().(*dto.Exemplar).String(); expected != got { |
| 284 |
t.Errorf("expected exemplar %s, got %s.", expected, got) |
| 285 |
} |
| 286 |
|
| 287 |
addExemplarWithInvalidLabel := func() (err error) { |
| 288 |
defer func() { |
| 289 |
if e := recover(); e != nil { |
| 290 |
err = e.(error) |
| 291 |
} |
| 292 |
}() |
| 293 |
// Should panic because of invalid label name. |
| 294 |
counter.AddWithExemplar(42, Labels{"in\x80valid": "smile"}) |
| 295 |
return nil |
| 296 |
} |
| 297 |
if addExemplarWithInvalidLabel() == nil { |
| 298 |
t.Error("adding exemplar with invalid label succeeded") |
| 299 |
} |
| 300 |
|
| 301 |
addExemplarWithOversizedLabels := func() (err error) { |
| 302 |
defer func() { |
| 303 |
if e := recover(); e != nil { |
| 304 |
err = e.(error) |
| 305 |
} |
| 306 |
}() |
| 307 |
// Should panic because of 129 runes. |
| 308 |
counter.AddWithExemplar(42, Labels{ |
| 309 |
"abcdefghijklmnopqrstuvwxyz": "26+16 characters", |
| 310 |
"x1234567": "8+15 characters", |
| 311 |
"z": strings.Repeat("x", 63), |
| 312 |
}) |
| 313 |
return nil |
| 314 |
} |
| 315 |
if addExemplarWithOversizedLabels() == nil { |
| 316 |
t.Error("adding exemplar with oversized labels succeeded") |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
func TestCounterVecCreatedTimestampWithDeletes(t *testing.T) { |
| 321 |
now := time.Now() |
| 322 |
|
| 323 |
counterVec := NewCounterVec(CounterOpts{ |
| 324 |
Name: "test", |
| 325 |
Help: "test help", |
| 326 |
now: func() time.Time { return now }, |
| 327 |
}, []string{"label"}) |
| 328 |
|
| 329 |
// First use of "With" should populate CT. |
| 330 |
counterVec.WithLabelValues("1") |
| 331 |
expected := map[string]time.Time{"1": now} |
| 332 |
|
| 333 |
now = now.Add(1 * time.Hour) |
| 334 |
expectCTsForMetricVecValues(t, counterVec.MetricVec, dto.MetricType_COUNTER, expected) |
| 335 |
|
| 336 |
// Two more labels at different times. |
| 337 |
counterVec.WithLabelValues("2") |
| 338 |
expected["2"] = now |
| 339 |
|
| 340 |
now = now.Add(1 * time.Hour) |
| 341 |
|
| 342 |
counterVec.WithLabelValues("3") |
| 343 |
expected["3"] = now |
| 344 |
|
| 345 |
now = now.Add(1 * time.Hour) |
| 346 |
expectCTsForMetricVecValues(t, counterVec.MetricVec, dto.MetricType_COUNTER, expected) |
| 347 |
|
| 348 |
// Recreate metric instance should reset created timestamp to now. |
| 349 |
counterVec.DeleteLabelValues("1") |
| 350 |
counterVec.WithLabelValues("1") |
| 351 |
expected["1"] = now |
| 352 |
|
| 353 |
now = now.Add(1 * time.Hour) |
| 354 |
expectCTsForMetricVecValues(t, counterVec.MetricVec, dto.MetricType_COUNTER, expected) |
| 355 |
} |
| 356 |
|
| 357 |
func expectCTsForMetricVecValues(t testing.TB, vec *MetricVec, typ dto.MetricType, ctsPerLabelValue map[string]time.Time) { |
| 358 |
t.Helper() |
| 359 |
|
| 360 |
for val, ct := range ctsPerLabelValue { |
| 361 |
var metric dto.Metric |
| 362 |
m, err := vec.GetMetricWithLabelValues(val) |
| 363 |
if err != nil { |
| 364 |
t.Fatal(err) |
| 365 |
} |
| 366 |
|
| 367 |
if err := m.Write(&metric); err != nil { |
| 368 |
t.Fatal(err) |
| 369 |
} |
| 370 |
|
| 371 |
var gotTs time.Time |
| 372 |
switch typ { |
| 373 |
case dto.MetricType_COUNTER: |
| 374 |
gotTs = metric.Counter.CreatedTimestamp.AsTime() |
| 375 |
case dto.MetricType_HISTOGRAM: |
| 376 |
gotTs = metric.Histogram.CreatedTimestamp.AsTime() |
| 377 |
case dto.MetricType_SUMMARY: |
| 378 |
gotTs = metric.Summary.CreatedTimestamp.AsTime() |
| 379 |
default: |
| 380 |
t.Fatalf("unknown metric type %v", typ) |
| 381 |
} |
| 382 |
|
| 383 |
if !gotTs.Equal(ct) { |
| 384 |
t.Errorf("expected created timestamp for %s with label value %q: %s, got %s", typ, val, ct, gotTs) |
| 385 |
} |
| 386 |
} |
| 387 |
} |