github.com/netdata/go.d.plugin@v0.58.1/pkg/prometheus/metric_family_test.go (about) 1 package prometheus 2 3 import ( 4 "testing" 5 6 "github.com/prometheus/prometheus/model/labels" 7 "github.com/prometheus/prometheus/model/textparse" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestMetricFamilies_Len(t *testing.T) { 12 tests := map[string]struct { 13 mfs MetricFamilies 14 wantLen int 15 }{ 16 "initialized with two elements": { 17 mfs: MetricFamilies{"1": nil, "2": nil}, 18 wantLen: 2, 19 }, 20 "not initialized": { 21 mfs: nil, 22 wantLen: 0, 23 }, 24 } 25 26 for name, test := range tests { 27 t.Run(name, func(t *testing.T) { 28 assert.Equal(t, test.mfs.Len(), test.wantLen) 29 }) 30 } 31 } 32 33 func TestMetricFamilies_Get(t *testing.T) { 34 const n = "metric" 35 36 tests := map[string]struct { 37 mfs MetricFamilies 38 wantMF *MetricFamily 39 }{ 40 "etric is found": { 41 mfs: MetricFamilies{n: &MetricFamily{name: n}}, 42 wantMF: &MetricFamily{name: n}, 43 }, 44 "metric is not found": { 45 mfs: MetricFamilies{"!" + n: &MetricFamily{name: n}}, 46 wantMF: nil, 47 }, 48 "not initialized": { 49 mfs: nil, 50 wantMF: nil, 51 }, 52 } 53 54 for name, test := range tests { 55 t.Run(name, func(t *testing.T) { 56 assert.Equal(t, test.mfs.Get(n), test.wantMF) 57 }) 58 } 59 } 60 61 func TestMetricFamilies_GetGauge(t *testing.T) { 62 const n = "metric" 63 64 tests := map[string]struct { 65 mfs MetricFamilies 66 wantMF *MetricFamily 67 }{ 68 "metric is found and is Gauge": { 69 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: textparse.MetricTypeGauge}}, 70 wantMF: &MetricFamily{name: n, typ: textparse.MetricTypeGauge}, 71 }, 72 "metric is found but it is not Gauge": { 73 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: textparse.MetricTypeUnknown}}, 74 wantMF: nil, 75 }, 76 "metric is not found": { 77 mfs: MetricFamilies{"!" + n: &MetricFamily{name: n, typ: textparse.MetricTypeGauge}}, 78 wantMF: nil, 79 }, 80 "not initialized": { 81 mfs: nil, 82 wantMF: nil, 83 }, 84 } 85 86 for name, test := range tests { 87 t.Run(name, func(t *testing.T) { 88 assert.Equal(t, test.mfs.GetGauge(n), test.wantMF) 89 }) 90 } 91 } 92 93 func TestMetricFamilies_GetCounter(t *testing.T) { 94 const n = "metric" 95 96 tests := map[string]struct { 97 mfs MetricFamilies 98 wantMF *MetricFamily 99 }{ 100 "metric is found and is Counter": { 101 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: textparse.MetricTypeCounter}}, 102 wantMF: &MetricFamily{name: n, typ: textparse.MetricTypeCounter}, 103 }, 104 "metric is found but it is not Counter": { 105 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: textparse.MetricTypeGauge}}, 106 wantMF: nil, 107 }, 108 "metric is not found": { 109 mfs: MetricFamilies{"!" + n: &MetricFamily{name: n, typ: textparse.MetricTypeGauge}}, 110 wantMF: nil, 111 }, 112 "not initialized": { 113 mfs: nil, 114 wantMF: nil, 115 }, 116 } 117 118 for name, test := range tests { 119 t.Run(name, func(t *testing.T) { 120 assert.Equal(t, test.mfs.GetCounter(n), test.wantMF) 121 }) 122 } 123 } 124 125 func TestMetricFamilies_GetSummary(t *testing.T) { 126 const n = "metric" 127 128 tests := map[string]struct { 129 mfs MetricFamilies 130 wantMF *MetricFamily 131 }{ 132 "metric is found and is Summary": { 133 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: textparse.MetricTypeSummary}}, 134 wantMF: &MetricFamily{name: n, typ: textparse.MetricTypeSummary}, 135 }, 136 "metric is found but it is not Summary": { 137 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: textparse.MetricTypeGauge}}, 138 wantMF: nil, 139 }, 140 "metric is not found": { 141 mfs: MetricFamilies{"!" + n: &MetricFamily{name: n, typ: textparse.MetricTypeGauge}}, 142 wantMF: nil, 143 }, 144 "not initialized": { 145 mfs: nil, 146 wantMF: nil, 147 }, 148 } 149 150 for name, test := range tests { 151 t.Run(name, func(t *testing.T) { 152 assert.Equal(t, test.mfs.GetSummary(n), test.wantMF) 153 }) 154 } 155 } 156 157 func TestMetricFamilies_GetHistogram(t *testing.T) { 158 const n = "metric" 159 160 tests := map[string]struct { 161 mfs MetricFamilies 162 wantMF *MetricFamily 163 }{ 164 "metric is found and is Histogram": { 165 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: textparse.MetricTypeHistogram}}, 166 wantMF: &MetricFamily{name: n, typ: textparse.MetricTypeHistogram}, 167 }, 168 "metric is found but it is not Histogram": { 169 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: textparse.MetricTypeGauge}}, 170 wantMF: nil, 171 }, 172 "metric is not found": { 173 mfs: MetricFamilies{"!" + n: &MetricFamily{name: n, typ: textparse.MetricTypeGauge}}, 174 wantMF: nil, 175 }, 176 "not initialized": { 177 mfs: nil, 178 wantMF: nil, 179 }, 180 } 181 182 for name, test := range tests { 183 t.Run(name, func(t *testing.T) { 184 assert.Equal(t, test.mfs.GetHistogram(n), test.wantMF) 185 }) 186 } 187 } 188 189 func TestMetricFamily_Name(t *testing.T) { 190 mf := &MetricFamily{name: "name"} 191 assert.Equal(t, mf.Name(), "name") 192 } 193 194 func TestMetricFamily_Type(t *testing.T) { 195 mf := &MetricFamily{typ: textparse.MetricTypeGauge} 196 assert.Equal(t, mf.Type(), textparse.MetricTypeGauge) 197 } 198 199 func TestMetricFamily_Help(t *testing.T) { 200 mf := &MetricFamily{help: "help"} 201 assert.Equal(t, mf.Help(), "help") 202 } 203 204 func TestMetricFamily_Metrics(t *testing.T) { 205 metrics := []Metric{{gauge: &Gauge{value: 1}, counter: &Counter{value: 1}}} 206 mf := &MetricFamily{metrics: metrics} 207 assert.Equal(t, mf.Metrics(), metrics) 208 } 209 210 func TestMetric_Labels(t *testing.T) { 211 lbs := labels.Labels{{Name: "1", Value: "1"}, {Name: "2", Value: "2"}} 212 m := &Metric{labels: lbs} 213 assert.Equal(t, m.Labels(), lbs) 214 } 215 216 func TestMetric_Gauge(t *testing.T) { 217 tests := map[string]struct { 218 m *Metric 219 want *Gauge 220 }{ 221 "gauge set": { 222 m: &Metric{gauge: &Gauge{value: 1}}, 223 want: &Gauge{value: 1}, 224 }, 225 "gauge not set": { 226 m: &Metric{}, 227 want: nil, 228 }, 229 } 230 231 for name, test := range tests { 232 t.Run(name, func(t *testing.T) { 233 assert.Equal(t, test.m.Gauge(), test.want) 234 }) 235 } 236 } 237 238 func TestMetric_Counter(t *testing.T) { 239 tests := map[string]struct { 240 m *Metric 241 want *Counter 242 }{ 243 "counter set": { 244 m: &Metric{counter: &Counter{value: 1}}, 245 want: &Counter{value: 1}, 246 }, 247 "counter not set": { 248 m: &Metric{}, 249 want: nil, 250 }, 251 } 252 253 for name, test := range tests { 254 t.Run(name, func(t *testing.T) { 255 assert.Equal(t, test.m.Counter(), test.want) 256 }) 257 } 258 } 259 260 func TestMetric_Summary(t *testing.T) { 261 tests := map[string]struct { 262 m *Metric 263 want *Summary 264 }{ 265 "summary set": { 266 m: &Metric{summary: &Summary{sum: 0.1, count: 3}}, 267 want: &Summary{sum: 0.1, count: 3}, 268 }, 269 "summary not set": { 270 m: &Metric{}, 271 want: nil, 272 }, 273 } 274 275 for name, test := range tests { 276 t.Run(name, func(t *testing.T) { 277 assert.Equal(t, test.m.Summary(), test.want) 278 }) 279 } 280 } 281 282 func TestMetric_Histogram(t *testing.T) { 283 tests := map[string]struct { 284 m *Metric 285 want *Histogram 286 }{ 287 "histogram set": { 288 m: &Metric{histogram: &Histogram{sum: 0.1, count: 3}}, 289 want: &Histogram{sum: 0.1, count: 3}, 290 }, 291 "histogram not set": { 292 m: &Metric{}, 293 want: nil, 294 }, 295 } 296 297 for name, test := range tests { 298 t.Run(name, func(t *testing.T) { 299 assert.Equal(t, test.m.Histogram(), test.want) 300 }) 301 } 302 } 303 304 func TestGauge_Value(t *testing.T) { 305 assert.Equal(t, Gauge{value: 1}.Value(), 1.0) 306 } 307 308 func TestCounter_Value(t *testing.T) { 309 assert.Equal(t, Counter{value: 1}.Value(), 1.0) 310 } 311 312 func TestSummary_Sum(t *testing.T) { 313 assert.Equal(t, Summary{sum: 1}.Sum(), 1.0) 314 } 315 316 func TestSummary_Count(t *testing.T) { 317 assert.Equal(t, Summary{count: 1}.Count(), 1.0) 318 } 319 320 func TestSummary_Quantiles(t *testing.T) { 321 assert.Equal(t, 322 Summary{quantiles: []Quantile{{quantile: 0.1, value: 1}}}.Quantiles(), 323 []Quantile{{quantile: 0.1, value: 1}}, 324 ) 325 } 326 327 func TestQuantile_Value(t *testing.T) { 328 assert.Equal(t, Quantile{value: 1}.Value(), 1.0) 329 } 330 331 func TestQuantile_Quantile(t *testing.T) { 332 assert.Equal(t, Quantile{quantile: 0.1}.Quantile(), 0.1) 333 } 334 335 func TestHistogram_Sum(t *testing.T) { 336 assert.Equal(t, Histogram{sum: 1}.Sum(), 1.0) 337 } 338 339 func TestHistogram_Count(t *testing.T) { 340 assert.Equal(t, Histogram{count: 1}.Count(), 1.0) 341 } 342 343 func TestHistogram_Buckets(t *testing.T) { 344 assert.Equal(t, 345 Histogram{buckets: []Bucket{{upperBound: 0.1, cumulativeCount: 1}}}.Buckets(), 346 []Bucket{{upperBound: 0.1, cumulativeCount: 1}}, 347 ) 348 } 349 350 func TestBucket_UpperBound(t *testing.T) { 351 assert.Equal(t, Bucket{upperBound: 0.1}.UpperBound(), 0.1) 352 } 353 354 func TestBucket_CumulativeCount(t *testing.T) { 355 assert.Equal(t, Bucket{cumulativeCount: 1}.CumulativeCount(), 1.0) 356 }