Skip to content

Commit 1e7634b

Browse files
author
Corin Lawson
committed
Introduces tests for histogram support
Prior to this change, the histogram support was untested. This change introduces a new integration test that reads a user query containing a number of histogram metrics. Also, additional checks have been added to TestBooleanConversionToValueAndString to test dbToUint64. Signed-off-by: Corin Lawson <[email protected]>
1 parent 20d6bac commit 1e7634b

File tree

3 files changed

+146
-1
lines changed

3 files changed

+146
-1
lines changed

cmd/postgres_exporter/postgres_exporter_integration_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,26 @@ func (s *IntegrationSuite) TestUnknownMetricParsingDoesntCrash(c *C) {
126126
// scrape the exporter and make sure it works
127127
exporter.scrape(ch)
128128
}
129+
130+
// TestExtendQueriesDoesntCrash tests that specifying extend.query-path doesn't
131+
// crash.
132+
func (s *IntegrationSuite) TestExtendQueriesDoesntCrash(c *C) {
133+
// Setup a dummy channel to consume metrics
134+
ch := make(chan prometheus.Metric, 100)
135+
go func() {
136+
for range ch {
137+
}
138+
}()
139+
140+
dsn := os.Getenv("DATA_SOURCE_NAME")
141+
c.Assert(dsn, Not(Equals), "")
142+
143+
exporter := NewExporter(
144+
strings.Split(dsn, ","),
145+
WithUserQueriesPath("../user_queries_test.yaml"),
146+
)
147+
c.Assert(exporter, NotNil)
148+
149+
// scrape the exporter and make sure it works
150+
exporter.scrape(ch)
151+
}

cmd/postgres_exporter/postgres_exporter_test.go

+72-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ package main
44

55
import (
66
"io/ioutil"
7+
"math"
78
"os"
89
"reflect"
910
"testing"
11+
"time"
1012

1113
"github.com/blang/semver"
1214
"github.com/prometheus/client_golang/prometheus"
@@ -275,13 +277,30 @@ func UnsetEnvironment(c *C, d string) {
275277
c.Assert(err, IsNil)
276278
}
277279

280+
type isNaNChecker struct {
281+
*CheckerInfo
282+
}
283+
284+
var IsNaN Checker = &isNaNChecker{
285+
&CheckerInfo{Name: "IsNaN", Params: []string{"value"}},
286+
}
287+
288+
func (checker *isNaNChecker) Check(params []interface{}, names []string) (result bool, error string) {
289+
param, ok := (params[0]).(float64)
290+
if !ok {
291+
return false, "obtained value type is not a float"
292+
}
293+
return math.IsNaN(param), ""
294+
}
295+
278296
// test boolean metric type gets converted to float
279297
func (s *FunctionalSuite) TestBooleanConversionToValueAndString(c *C) {
280298

281299
type TestCase struct {
282300
input interface{}
283301
expectedString string
284302
expectedValue float64
303+
expectedCount uint64
285304
expectedOK bool
286305
}
287306

@@ -290,19 +309,71 @@ func (s *FunctionalSuite) TestBooleanConversionToValueAndString(c *C) {
290309
input: true,
291310
expectedString: "true",
292311
expectedValue: 1.0,
312+
expectedCount: 1,
293313
expectedOK: true,
294314
},
295315
{
296316
input: false,
297317
expectedString: "false",
298318
expectedValue: 0.0,
319+
expectedCount: 0,
320+
expectedOK: true,
321+
},
322+
{
323+
input: nil,
324+
expectedString: "",
325+
expectedValue: math.NaN(),
326+
expectedCount: 0,
327+
expectedOK: true,
328+
},
329+
{
330+
input: TestCase{},
331+
expectedString: "",
332+
expectedValue: math.NaN(),
333+
expectedCount: 0,
334+
expectedOK: false,
335+
},
336+
{
337+
input: 123.0,
338+
expectedString: "123",
339+
expectedValue: 123.0,
340+
expectedCount: 123,
341+
expectedOK: true,
342+
},
343+
{
344+
input: "123",
345+
expectedString: "123",
346+
expectedValue: 123.0,
347+
expectedCount: 123,
348+
expectedOK: true,
349+
},
350+
{
351+
input: []byte("123"),
352+
expectedString: "123",
353+
expectedValue: 123.0,
354+
expectedCount: 123,
355+
expectedOK: true,
356+
},
357+
{
358+
input: time.Unix(1600000000, 0),
359+
expectedString: "1600000000",
360+
expectedValue: 1600000000.0,
361+
expectedCount: 1600000000,
299362
expectedOK: true,
300363
},
301364
}
302365

303366
for _, cs := range cases {
304367
value, ok := dbToFloat64(cs.input)
305-
c.Assert(value, Equals, cs.expectedValue)
368+
if math.IsNaN(cs.expectedValue) {
369+
c.Assert(value, IsNaN)
370+
} else {
371+
c.Assert(value, Equals, cs.expectedValue)
372+
}
373+
c.Assert(ok, Equals, cs.expectedOK)
374+
375+
count, ok := dbToUint64(cs.input)
376+
c.Assert(count, Equals, cs.expectedCount)
306377
c.Assert(ok, Equals, cs.expectedOK)
307378

308379
str, ok := dbToString(cs.input)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
random:
2+
query: |
3+
WITH data AS (SELECT floor(random()*10) AS d FROM generate_series(1,100)),
4+
metrics AS (SELECT SUM(d) AS sum, COUNT(*) AS count FROM data),
5+
buckets AS (SELECT le, SUM(CASE WHEN d <= le THEN 1 ELSE 0 END) AS d
6+
FROM data, UNNEST(ARRAY[1, 2, 4, 8]) AS le GROUP BY le)
7+
SELECT
8+
sum AS histogram_sum,
9+
count AS histogram_count,
10+
ARRAY_AGG(le) AS histogram,
11+
ARRAY_AGG(d) AS histogram_bucket,
12+
ARRAY_AGG(le) AS missing,
13+
ARRAY_AGG(le) AS missing_sum,
14+
ARRAY_AGG(d) AS missing_sum_bucket,
15+
ARRAY_AGG(le) AS missing_count,
16+
ARRAY_AGG(d) AS missing_count_bucket,
17+
sum AS missing_count_sum,
18+
ARRAY_AGG(le) AS unexpected_sum,
19+
ARRAY_AGG(d) AS unexpected_sum_bucket,
20+
'data' AS unexpected_sum_sum,
21+
ARRAY_AGG(le) AS unexpected_count,
22+
ARRAY_AGG(d) AS unexpected_count_bucket,
23+
sum AS unexpected_count_sum,
24+
'nan' AS unexpected_count_count,
25+
ARRAY_AGG(le) AS unexpected_bytes,
26+
ARRAY_AGG(d) AS unexpected_bytes_bucket,
27+
sum AS unexpected_bytes_sum,
28+
'nan'::bytea AS unexpected_bytes_count
29+
FROM metrics, buckets GROUP BY 1,2
30+
metrics:
31+
- histogram:
32+
usage: "HISTOGRAM"
33+
description: "Random data"
34+
- missing:
35+
usage: "HISTOGRAM"
36+
description: "nonfatal error"
37+
- missing_sum:
38+
usage: "HISTOGRAM"
39+
description: "nonfatal error"
40+
- missing_count:
41+
usage: "HISTOGRAM"
42+
description: "nonfatal error"
43+
- unexpected_sum:
44+
usage: "HISTOGRAM"
45+
description: "nonfatal error"
46+
- unexpected_count:
47+
usage: "HISTOGRAM"
48+
description: "nonfatal error"
49+
- unexpected_bytes:
50+
usage: "HISTOGRAM"
51+
description: "nonfatal error"

0 commit comments

Comments
 (0)