-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
prometheus_exporter_test.go
134 lines (119 loc) · 4.11 KB
/
prometheus_exporter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2016 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package metric
import (
"bytes"
"strings"
"testing"
"github.com/prometheus/common/expfmt"
"github.com/stretchr/testify/require"
)
func TestPrometheusExporter(t *testing.T) {
r1, r2 := NewRegistry(), NewRegistry()
// r2 has a registry-level label, r1 does not.
r2.AddLabel("registry", "two")
r1.AddMetric(NewGauge(Metadata{Name: "one.gauge"}))
r2.AddMetric(NewGauge(Metadata{Name: "two.gauge"}))
c1Meta := Metadata{Name: "shared.counter"}
c1Meta.AddLabel("counter", "one")
c2Meta := Metadata{Name: "shared.counter"}
c2Meta.AddLabel("counter", "two")
r1.AddMetric(NewCounter(c1Meta))
r2.AddMetric(NewCounter(c2Meta))
pe := MakePrometheusExporter()
const includeChildMetrics = false
pe.ScrapeRegistry(r1, includeChildMetrics)
pe.ScrapeRegistry(r2, includeChildMetrics)
type metricLabels map[string]string
type family struct {
// List of metric. The order depends on the order of "AddMetricsFromRegistry".
// Each entry is a list of labels for the metric at that index, "registry labels" followed by
// "metric labels" in the order they were added.
metrics []metricLabels
}
expected := map[string]family{
"one_gauge": {[]metricLabels{
{},
}},
"two_gauge": {[]metricLabels{
{"registry": "two"},
}},
"shared_counter": {[]metricLabels{
{"counter": "one"},
{"counter": "two", "registry": "two"},
}},
}
if lenExpected, lenExporter := len(expected), len(pe.families); lenExpected != lenExporter {
t.Errorf("wrong number of families, expected %d, got %d", lenExpected, lenExporter)
}
for name, fam := range expected {
fam2, ok := pe.families[name]
if !ok {
t.Errorf("exporter does not have metric family named %s", name)
}
if lenExpected, lenExporter := len(fam.metrics), len(fam2.GetMetric()); lenExpected != lenExporter {
t.Errorf("wrong number of metrics for family %s, expected %d, got %d", name, lenExpected, lenExporter)
}
for i, met := range fam2.GetMetric() {
expectedLabels := fam.metrics[i]
if lenExpected, lenExporter := len(expectedLabels), len(met.Label); lenExpected != lenExporter {
t.Errorf("wrong number of labels for metric %d in family %s, expected %d, got %d",
i, name, lenExpected, lenExporter)
}
for _, l := range met.Label {
if val, ok := expectedLabels[l.GetName()]; !ok {
t.Errorf("unexpected label name %s for metric %d in family %s", l.GetName(), i, name)
} else if val != l.GetValue() {
t.Errorf("label %s for metric %d in family %s has value %s, expected %s",
l.GetName(), i, name, l.GetValue(), val)
}
}
}
}
// Test Gather
families, err := pe.Gather()
if err != nil {
t.Errorf("unexpected error from Gather(): %v", err)
}
for _, fam := range families {
if len(fam.Metric) == 0 {
t.Errorf("gathered %s has no data points", fam.GetName())
}
}
// Test clearMetrics
pe.clearMetrics()
for _, fam := range pe.families {
if numPoints := len(fam.Metric); numPoints != 0 {
t.Errorf("%s has %d data points, want 0", fam.GetName(), numPoints)
}
}
// Check families returned by Gather are empty, right after calling clearMetrics
// before another call to scrape.
families, err = pe.Gather()
if err != nil {
t.Errorf("unexpected error from Gather(): %v", err)
}
for _, fam := range families {
if num := len(fam.Metric); num != 0 {
t.Errorf("gathered %s has %d data points but expect none", fam.GetName(), num)
}
}
// Test ScrapeAndPrintAsText
var buf bytes.Buffer
pe = MakePrometheusExporter()
err = pe.ScrapeAndPrintAsText(&buf, expfmt.FmtText, func(exporter *PrometheusExporter) {
exporter.ScrapeRegistry(r1, true)
})
require.NoError(t, err)
output := buf.String()
require.Regexp(t, "one_gauge 0", output)
require.Regexp(t, "shared_counter{counter=\"one\"}", output)
require.Len(t, strings.Split(output, "\n"), 7)
}