-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmerger_test.go
64 lines (53 loc) · 1.85 KB
/
merger_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
package metrics
import (
"testing"
"time"
"github.com/gatewayd-io/gatewayd/config"
"github.com/gatewayd-io/gatewayd/logging"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
func TestMerger(t *testing.T) {
// This runs inside a plugin and exposes metrics to GatewayD
// via HTTP on a unix socket. But we don't need to test that here,
// so we just expose the metrics via the same mechanism to the merger.
go exposeMetrics(t)
logger := logging.NewLogger(
t.Context(),
logging.LoggerConfig{
Output: []config.LogOutput{config.Console},
TimeFormat: zerolog.TimeFormatUnix,
ConsoleTimeFormat: time.RFC3339,
Level: zerolog.InfoLevel,
NoColor: true,
},
)
merger := NewMerger(t.Context(), Merger{
MetricsMergerPeriod: 1, Logger: logger,
})
merger.Add("test", "/tmp/test.sock")
// We need to give the merger some time to read the metrics.
// TODO: Find a better way to do this.
// Note: httptest.NewServer only creates a TCP server.
time.Sleep(time.Second)
metrics, err := merger.ReadMetrics()
assert.Nil(t, err)
err = merger.MergeMetrics(metrics)
assert.Nil(t, err)
// We expect the metrics to be merged into a single output.
// Also, we don't need to test the actual metrics from GatewayD,
// so the output only contains the metrics from the test plugin.
// All the plugin's metrics are prefixed with "gatewayd_" and labeled
// with the plugin name, i.e. "test" during merging.
want := `# HELP gatewayd_test_total Test counter
# TYPE gatewayd_test_total counter
gatewayd_test_total{plugin="test"} 1`
assert.Contains(t, string(merger.OutputMetrics), want)
// Remove the plugin from the merger, thus stopping the metrics
// collection from the plugin.
merger.Remove("test")
// Test start/stop of the merger scheduler.
go merger.Start()
time.Sleep(time.Second)
go merger.Stop()
}