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

Function TestNewConstMetricWithCreatedTimestamp

prometheus/value_test.go:61–110  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

59}
60
61func TestNewConstMetricWithCreatedTimestamp(t *testing.T) {
62 now := time.Now()
63
64 for _, tcase := range []struct {
65 desc string
66 metricType ValueType
67 createdTimestamp time.Time
68 expecErr bool
69 expectedCt *timestamppb.Timestamp
70 }{
71 {
72 desc: "gauge with CT",
73 metricType: GaugeValue,
74 createdTimestamp: now,
75 expecErr: true,
76 expectedCt: nil,
77 },
78 {
79 desc: "counter with CT",
80 metricType: CounterValue,
81 createdTimestamp: now,
82 expecErr: false,
83 expectedCt: timestamppb.New(now),
84 },
85 } {
86 t.Run(tcase.desc, func(t *testing.T) {
87 metricDesc := NewDesc(
88 "sample_value",
89 "sample value",
90 nil,
91 nil,
92 )
93 m, err := NewConstMetricWithCreatedTimestamp(metricDesc, tcase.metricType, float64(1), tcase.createdTimestamp)
94 if tcase.expecErr && err == nil {
95 t.Errorf("Expected error is test %s, got no err", tcase.desc)
96 }
97 if !tcase.expecErr && err != nil {
98 t.Errorf("Didn't expect error in test %s, got %s", tcase.desc, err.Error())
99 }
100
101 if tcase.expectedCt != nil {
102 var metric dto.Metric
103 m.Write(&metric)
104 if metric.Counter.CreatedTimestamp.AsTime() != tcase.expectedCt.AsTime() {
105 t.Errorf("Expected timestamp %v, got %v", tcase.expectedCt, &metric.Counter.CreatedTimestamp)
106 }
107 }
108 })
109 }
110}

Callers

nothing calls this directly

Calls 5

NewDescFunction · 0.85
RunMethod · 0.80
WriteMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected