(t *testing.T)
| 411 | } |
| 412 | |
| 413 | func TestHistogramExemplar(t *testing.T) { |
| 414 | now := time.Now() |
| 415 | |
| 416 | histogram := NewHistogram(HistogramOpts{ |
| 417 | Name: "test", |
| 418 | Help: "test help", |
| 419 | Buckets: []float64{1, 2, 3, 4}, |
| 420 | now: func() time.Time { return now }, |
| 421 | }).(*histogram) |
| 422 | |
| 423 | ts := timestamppb.New(now) |
| 424 | if err := ts.CheckValid(); err != nil { |
| 425 | t.Fatal(err) |
| 426 | } |
| 427 | expectedExemplars := []*dto.Exemplar{ |
| 428 | nil, |
| 429 | { |
| 430 | Label: []*dto.LabelPair{ |
| 431 | {Name: proto.String("id"), Value: proto.String("2")}, |
| 432 | }, |
| 433 | Value: proto.Float64(1.6), |
| 434 | Timestamp: ts, |
| 435 | }, |
| 436 | nil, |
| 437 | { |
| 438 | Label: []*dto.LabelPair{ |
| 439 | {Name: proto.String("id"), Value: proto.String("3")}, |
| 440 | }, |
| 441 | Value: proto.Float64(4), |
| 442 | Timestamp: ts, |
| 443 | }, |
| 444 | { |
| 445 | Label: []*dto.LabelPair{ |
| 446 | {Name: proto.String("id"), Value: proto.String("4")}, |
| 447 | }, |
| 448 | Value: proto.Float64(4.5), |
| 449 | Timestamp: ts, |
| 450 | }, |
| 451 | } |
| 452 | |
| 453 | histogram.ObserveWithExemplar(1.5, Labels{"id": "1"}) |
| 454 | histogram.ObserveWithExemplar(1.6, Labels{"id": "2"}) // To replace exemplar in bucket 0. |
| 455 | histogram.ObserveWithExemplar(4, Labels{"id": "3"}) |
| 456 | histogram.ObserveWithExemplar(4.5, Labels{"id": "4"}) // Should go to +Inf bucket. |
| 457 | |
| 458 | for i, ex := range histogram.exemplars { |
| 459 | var got, expected string |
| 460 | if val := ex.Load(); val != nil { |
| 461 | got = val.(*dto.Exemplar).String() |
| 462 | } |
| 463 | if expectedExemplars[i] != nil { |
| 464 | expected = expectedExemplars[i].String() |
| 465 | } |
| 466 | if got != expected { |
| 467 | t.Errorf("expected exemplar %s, got %s.", expected, got) |
| 468 | } |
| 469 | } |
| 470 | } |
nothing calls this directly
no test coverage detected