-
Notifications
You must be signed in to change notification settings - Fork 73
/
metrics_test.go
145 lines (118 loc) · 4.01 KB
/
metrics_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
// Copyright 2022 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0
package metrics_test
import (
"bufio"
"context"
"io"
"net/http"
"strconv"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spidernet-io/spiderpool/pkg/metrics"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
)
const (
OTEL_COUNTS_SIGNAL = "ippool=\"default_pool\""
)
var (
verifyCount int
MetricsTestServerAddr = ""
SpiderPoolMeter = "spider_pool_meter"
MetricNodeIPAllocationCountsName = "Node_IP_Allocation_Counts"
MetricNodeIPAllocationDurationName = "Node_IP_Allocation_Duration"
)
type IPAllocation struct {
NodeName string
PoolName string
// ....
MetricNodeIPAllocationCounts syncint64.Counter
MetricNodeIPAllocationDuration syncfloat64.Histogram
}
var _ = Describe("metrics", Label("unitest", "metrics_test"), Ordered, func() {
It("use prometheus as exporter", func() {
c := make(chan bool)
ctx := context.Background()
ipAllocation := IPAllocation{
NodeName: "node1",
PoolName: "default/poo1",
}
// register metrics
metricInt64Counter, err := metrics.NewMetricInt64Counter(MetricNodeIPAllocationCountsName, "The total counts of node IP allocations")
Expect(err).NotTo(HaveOccurred())
ipAllocation.MetricNodeIPAllocationCounts = metricInt64Counter
histogram, err := metrics.NewMetricFloat64Histogram(MetricNodeIPAllocationDurationName, "The duration of node IP allocations")
Expect(err).NotTo(HaveOccurred())
ipAllocation.MetricNodeIPAllocationDuration = histogram
// verifyRecord willverify whether the metrics record correctly
verifyRecord := func(duration float64) {
resp, err := http.Get(MetricsTestServerAddr)
Expect(err).NotTo(HaveOccurred())
defer func(Body io.ReadCloser) {
err = Body.Close()
Expect(err).NotTo(HaveOccurred())
}(resp.Body)
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadString('\n')
if nil != err || io.EOF == err {
if line == "" {
break
}
}
// verify counts instrument
if strings.Contains(line, MetricNodeIPAllocationCountsName) && strings.Contains(line, OTEL_COUNTS_SIGNAL) {
split := strings.Split(line, " ")
Expect(err).NotTo(HaveOccurred())
Expect(split[len(split)-1]).Should(Equal("2\n"))
verifyCount++
}
// verify histogram instrument
if strings.Contains(line, MetricNodeIPAllocationDurationName+"_sum") {
split := strings.Split(line, " ")
Expect(err).NotTo(HaveOccurred())
Expect(split[len(split)-1]).Should(Equal(strconv.FormatFloat(duration, 'f', -1, 64) + "\n"))
verifyCount++
}
}
Expect(verifyCount).Should(Equal(2))
}
// metrics record data
go func() {
defer GinkgoRecover()
// ip allocation logics....
timeRecorder := metrics.NewTimeRecorder()
// ....
// ip allocation succeed
// record the counter metric without labels.
ipAllocation.MetricNodeIPAllocationCounts.Add(ctx, 1, attribute.Key("ippool").String("default_pool"))
time.Sleep(time.Second * 5)
ipAllocation.MetricNodeIPAllocationCounts.Add(ctx, 1, attribute.Key("ippool").String("default_pool"))
// record histogram metric with labels.
duration := timeRecorder.SinceInSeconds()
ipAllocation.MetricNodeIPAllocationDuration.Record(ctx, duration,
attribute.Key("hostname").String("node1"),
attribute.Key("type").String("total"))
time.Sleep(time.Second * 1)
verifyRecord(duration)
close(c)
}()
Eventually(c, "20s").Should(BeClosed())
})
It("test empty counter metric name", func() {
_, err := metrics.NewMetricInt64Counter("", "")
Expect(err).To(HaveOccurred())
})
It("test empty histogram metric name", func() {
_, err := metrics.NewMetricFloat64Histogram("", "")
Expect(err).To(HaveOccurred())
})
It("test InitMetricController with empty meter name", func() {
_, err := metrics.InitMetricController("")
Expect(err).To(HaveOccurred())
})
})