summaryrefslogtreecommitdiff
path: root/plugins/metrics/config_test.go
blob: 24c8406cfabdd927cbdc98f4be5400f4ee8a6205 (plain)
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
package metrics

import (
	"bytes"
	"testing"

	json "github.com/json-iterator/go"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/stretchr/testify/assert"
)

func Test_Config_Hydrate_Error1(t *testing.T) {
	cfg := `{"request": {"From": "Something"}}`
	c := &Config{}
	f := new(bytes.Buffer)
	f.WriteString(cfg)

	err := json.Unmarshal(f.Bytes(), &c)
	if err != nil {
		t.Fatal(err)
	}
}

func Test_Config_Hydrate_Error2(t *testing.T) {
	cfg := `{"dir": "/dir/"`
	c := &Config{}

	f := new(bytes.Buffer)
	f.WriteString(cfg)

	err := json.Unmarshal(f.Bytes(), &c)
	assert.Error(t, err)
}

func Test_Config_Metrics(t *testing.T) {
	cfg := `{
"collect":{
	"metric1":{"type": "gauge"},
	"metric2":{	"type": "counter"},
	"metric3":{"type": "summary"},
	"metric4":{"type": "histogram"}
}
}`
	c := &Config{}
	f := new(bytes.Buffer)
	f.WriteString(cfg)

	err := json.Unmarshal(f.Bytes(), &c)
	if err != nil {
		t.Fatal(err)
	}

	m, err := c.getCollectors()
	assert.NoError(t, err)

	assert.IsType(t, prometheus.NewGauge(prometheus.GaugeOpts{}), m["metric1"])
	assert.IsType(t, prometheus.NewCounter(prometheus.CounterOpts{}), m["metric2"])
	assert.IsType(t, prometheus.NewSummary(prometheus.SummaryOpts{}), m["metric3"])
	assert.IsType(t, prometheus.NewHistogram(prometheus.HistogramOpts{}), m["metric4"])
}

func Test_Config_MetricsVector(t *testing.T) {
	cfg := `{
"collect":{
	"metric1":{"type": "gauge","labels":["label"]},
	"metric2":{	"type": "counter","labels":["label"]},
	"metric3":{"type": "summary","labels":["label"]},
	"metric4":{"type": "histogram","labels":["label"]}
}
}`
	c := &Config{}
	f := new(bytes.Buffer)
	f.WriteString(cfg)

	err := json.Unmarshal(f.Bytes(), &c)
	if err != nil {
		t.Fatal(err)
	}

	m, err := c.getCollectors()
	assert.NoError(t, err)

	assert.IsType(t, prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{}), m["metric1"])
	assert.IsType(t, prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{}), m["metric2"])
	assert.IsType(t, prometheus.NewSummaryVec(prometheus.SummaryOpts{}, []string{}), m["metric3"])
	assert.IsType(t, prometheus.NewHistogramVec(prometheus.HistogramOpts{}, []string{}), m["metric4"])
}