MCPcopy
hub / github.com/prometheus/client_golang / TestCounterExemplar

Function TestCounterExemplar

prometheus/counter_test.go:261–318  ·  prometheus/counter_test.go::TestCounterExemplar
(t *testing.T)

Source from the content-addressed store, hash-verified

259}
260
261func 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}

Callers

nothing calls this directly

Calls 5

StringMethod · 0.95
NewCounterFunction · 0.70
AddWithExemplarMethod · 0.65
StringMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected