-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcollector_test.go
167 lines (146 loc) · 5.38 KB
/
collector_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bytes"
"fmt"
"github.com/n9e/prometheus-exporter-collector/collector"
"github.com/n9e/prometheus-exporter-collector/config"
"github.com/stretchr/testify/assert"
"testing"
)
const validConfigParam = `{
"exporter_urls":["http://127.0.0.1:8080/metrics"],
"append_tags": ["region=bj", "dept=cloud"],
"endpoint": "127.0.0.1",
"ignore_metrics_prefix": [],
"timeout": 500
}
`
func TestConfigParse(t *testing.T) {
err := config.Parse([]byte(validConfigParam))
assert.NoError(t, err)
}
const validPromGaugeMetric = `# HELP mysql_global_status_buffer_pool_dirty_pages Innodb buffer pool dirty pages.
# TYPE mysql_global_status_buffer_pool_dirty_pages gauge
mysql_global_status_buffer_pool_dirty_pages 132
# HELP mysql_global_status_buffer_pool_pages Innodb buffer pool pages by state.
# TYPE mysql_global_status_buffer_pool_pages gauge
mysql_global_status_buffer_pool_pages{state="data"} 794548
mysql_global_status_buffer_pool_pages{state="free"} 243560
mysql_global_status_buffer_pool_pages{state="misc"} 10460
mysql_global_status_buffer_pool_pages{state="old"} 293137
`
const validPromCounterMetric = `# HELP mysql_global_status_buffer_pool_page_changes_total Innodb buffer pool page state changes.
# TYPE mysql_global_status_buffer_pool_page_changes_total counter
mysql_global_status_buffer_pool_page_changes_total{operation="flushed"} 1.4547024e+07
mysql_global_status_buffer_pool_page_changes_total{operation="lru_flushed"} 0
mysql_global_status_buffer_pool_page_changes_total{operation="made_not_young"} 1.7149202e+07
mysql_global_status_buffer_pool_page_changes_total{operation="made_young"} 979263
# HELP mysql_exporter_scrapes_total Total number of times MySQL was scraped for metrics.
# TYPE mysql_exporter_scrapes_total counter
mysql_exporter_scrapes_total 1
`
const validPromSummaryMetric = `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 543
go_gc_duration_seconds{quantile="0.25"} 33
go_gc_duration_seconds{quantile="0.5"} 66
go_gc_duration_seconds{quantile="0.75"} 23
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 794548
go_gc_duration_seconds_count 1444
`
const validPromHistogramMetric = `# HELP mysql_global_status_test histogram test
# TYPE mysql_global_status_test histogram
mysql_global_status_test_bucket{le="500"} 111
mysql_global_status_test_bucket{le="50"} 85
mysql_global_status_test_bucket{le="5"} 378
mysql_global_status_test_bucket{le="1"} 22
mysql_global_status_test_sum 794548
mysql_global_status_test_count 1422
`
const validPromUntypedMetric = `# HELP mysql_global_status_aborted_clients Generic metric from SHOW GLOBAL STATUS.
# TYPE mysql_global_status_aborted_clients untyped
mysql_global_status_aborted_clients 62539
`
func TestPromMetricParser(t *testing.T) {
err := config.Parse([]byte(validConfigParam))
assert.NoError(t, err)
metrics, err := collector.Parse([]byte(validPromUntypedMetric))
assert.NoError(t, err)
assert.Len(t, metrics, 1)
metrics, err = collector.Parse([]byte(validPromGaugeMetric))
assert.NoError(t, err)
assert.Len(t, metrics, 5)
metrics, err = collector.Parse([]byte(validPromCounterMetric))
assert.NoError(t, err)
assert.Len(t, metrics, 5)
metrics, err = collector.Parse([]byte(validPromSummaryMetric))
assert.NoError(t, err)
assert.Len(t, metrics, 7)
metrics, err = collector.Parse([]byte(validPromHistogramMetric))
assert.NoError(t, err)
assert.Len(t, metrics, 6)
}
const validStdin = `{
"exporter_urls": [
"http://xxxxx:9103/metrics?dns=xxxx:3306",
"http://xxxxx:9103/metrics?dns=xxxx:3307"
],
"endpoint": "xxxx",
"append_tags": ["region=bj", "dept=cloud"],
"ignore_metrics_prefix": ["gc_","go_"],
"timeout": 500
}
`
func TestStdin(t *testing.T) {
bs := bytes.NewReader([]byte(validStdin))
parseStdin(bs)
p := config.Get()
fmt.Println(p)
assert.NotNil(t, p)
}
func TestStdout(t *testing.T) {
err := config.Parse([]byte(validConfigParam))
assert.NoError(t, err)
metrics, err := collector.Parse([]byte(validPromHistogramMetric))
assert.NoError(t, err)
assert.Len(t, metrics, 6)
printMetrics(metrics)
}
func TestGather(t *testing.T) {
err := config.Parse([]byte(validConfigParam))
assert.NoError(t, err)
metrics := collector.Gather()
assert.NotNil(t, metrics)
printMetrics(metrics)
}
const validIgnoreConfigParam = `{
"exporter_urls":["http://127.0.0.1:8080/metrics"],
"append_tags": ["region=bj", "dept=cloud"],
"endpoint": "127.0.0.1",
"ignore_metrics_prefix": ["go_"],
"timeout": 500
}
`
const validPromIgnoreMetric = `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 1.2099e-05
go_gc_duration_seconds{quantile="0.25"} 1.3161e-05
go_gc_duration_seconds{quantile="0.5"} 1.3841e-05
go_gc_duration_seconds{quantile="0.75"} 1.4729e-05
go_gc_duration_seconds{quantile="1"} 0.015064859
go_gc_duration_seconds_sum 266.170701672
go_gc_duration_seconds_count 84408
`
func TestIgnoreMetric(t *testing.T) {
err := config.Parse([]byte(validIgnoreConfigParam))
assert.NoError(t, err)
config.Config.IgnoreMetricsPrefix = []string{"mem"}
metrics, err := collector.Parse([]byte(validPromIgnoreMetric))
assert.NoError(t, err)
assert.Len(t, metrics, 7)
config.Config.IgnoreMetricsPrefix = []string{"go_"}
metrics, err = collector.Parse([]byte(validPromIgnoreMetric))
assert.NoError(t, err)
assert.Len(t, metrics, 0)
}