-
Notifications
You must be signed in to change notification settings - Fork 73
/
metrics_test.go
94 lines (76 loc) · 2.55 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
// Copyright 2022 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0
package metrics_test
import (
"context"
"net/http"
"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"
)
var (
MetricsTestServerAddr = "127.0.0.1:2222"
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() {
var httpHandle http.Handler
var server http.Server
var err error
BeforeAll(func() {
httpHandle, err = metrics.InitMetricController(SpiderPoolMeter)
Expect(err).NotTo(HaveOccurred())
server.Addr = MetricsTestServerAddr
server.Handler = httpHandle
go func() {
_ = server.ListenAndServe()
}()
})
AfterAll(func() {
_ = server.Shutdown(context.TODO())
})
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
go func() {
// ip allocation logics....
timeRecorder := metrics.NewTimeRecorder()
// ....
// ip allocation succeed
// record the counter metric without labels.
ipAllocation.MetricNodeIPAllocationCounts.Add(ctx, 1)
time.Sleep(time.Second * 10)
ipAllocation.MetricNodeIPAllocationCounts.Add(ctx, 1)
// record histogram metric with labels.
duration := timeRecorder.SinceInSeconds()
ipAllocation.MetricNodeIPAllocationDuration.Record(ctx, duration,
attribute.Key("hostname").String("node1"),
attribute.Key("type").String("total"))
close(c)
}()
Eventually(c, "20s").Should(BeClosed())
})
})