-
Notifications
You must be signed in to change notification settings - Fork 5
/
tracer_test.go
176 lines (134 loc) · 6.21 KB
/
tracer_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
168
169
170
171
172
173
174
175
176
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package prometheus
import (
"context"
"io"
"math/rand"
"net/http"
"strings"
"testing"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
)
// TestServerTracer test server tracer work with hertz.
func TestServerTracerWorkWithHertz(t *testing.T) {
h := server.Default(server.WithHostPorts("127.0.0.1:8888"), server.WithTracer(NewServerTracer(":8889", "/metrics", WithEnableGoCollector(true))))
h.GET("/metricGet", func(c context.Context, ctx *app.RequestContext) {
ctx.String(200, "hello get")
})
h.POST("/metricPost", func(c context.Context, ctx *app.RequestContext) {
rand.Seed(time.Now().UnixMilli())
// make sure the response time is greater than 50 milliseconds and less than around 151 milliseconds
time.Sleep(time.Duration(rand.Intn(100)+51) * time.Millisecond)
ctx.String(200, "hello post")
})
go h.Spin()
time.Sleep(time.Second) // wait server start
for i := 0; i < 10; i++ {
_, err := http.Get("http://127.0.0.1:8888/metricGet")
assert.Nil(t, err)
_, err = http.Post("http://127.0.0.1:8888/metricPost", "application/json", strings.NewReader(""))
assert.Nil(t, err)
}
metricsRes, err := http.Get("http://127.0.0.1:8889/metrics")
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, metricsRes.StatusCode)
defer metricsRes.Body.Close()
metricsResBytes, err := io.ReadAll(metricsRes.Body)
assert.Nil(t, err)
metricsResStr := string(metricsResBytes)
assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="GET",path="/metricGet",statusCode="200",le="+Inf"} 10`))
assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_count{method="GET",path="/metricGet",statusCode="200"} 10`))
assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="250000"} 10`))
// response time is always greater than 50000 microseconds
assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="50000"} 0`))
assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="5000"} 0`))
assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_count{method="POST",path="/metricPost",statusCode="200"} 10`))
assert.True(t, strings.Contains(metricsResStr, `hertz_server_throughput{method="GET",path="/metricGet",statusCode="200"} 10`))
assert.True(t, strings.Contains(metricsResStr, `hertz_server_throughput{method="POST",path="/metricPost",statusCode="200"} 10`))
}
// TestWithOption test server tracer with options
func TestWithOption(t *testing.T) {
registry := prom.NewRegistry()
// define your own vec
testCounter := prom.NewCounterVec(prom.CounterOpts{
Name: "test_with_option",
Help: "Use for test with option",
}, []string{
"test1", "test2",
})
registry.MustRegister(testCounter)
_ = counterAdd(testCounter, 1, prom.Labels{
"test1": "test1",
"test2": "test2",
})
h := server.Default(
server.WithHostPorts("127.0.0.1:8891"), server.WithTracer(
NewServerTracer(":8892", "/metrics-option",
WithRegistry(registry),
WithEnableGoCollector(true),
),
),
)
go h.Spin()
time.Sleep(time.Second) // wait server start
metricsRes, err := http.Get("http://127.0.0.1:8892/metrics-option")
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, metricsRes.StatusCode)
defer metricsRes.Body.Close()
metricsResBytes, err := io.ReadAll(metricsRes.Body)
assert.Nil(t, err)
metricsResStr := string(metricsResBytes)
assert.True(t, strings.Contains(metricsResStr, "test_with_option{test1=\"test1\",test2=\"test2\"} 1"))
assert.True(t, strings.Contains(metricsResStr, "# TYPE go_gc_duration_seconds summary"))
}
// TestWithBucketsOption test server tracer with buckets option
func TestWithBucketsOption(t *testing.T) {
customBuckets := []float64{500, 1000, 2500, 5000, 10000, 25000, 50000, 250000}
h := server.Default(server.WithHostPorts("127.0.0.1:8895"), server.WithTracer(NewServerTracer(":8896", "/metrics-buckets", WithHistogramBuckets(customBuckets))))
h.GET("/metricGet", func(c context.Context, ctx *app.RequestContext) {
ctx.String(200, "hello get")
})
h.POST("/metricPost", func(c context.Context, ctx *app.RequestContext) {
rand.Seed(time.Now().UnixMilli())
// make sure the response time is greater than 50 milliseconds and less than around 151 milliseconds
time.Sleep(time.Duration(rand.Intn(100)+51) * time.Millisecond)
ctx.String(200, "hello post")
})
go h.Spin()
time.Sleep(time.Second) // wait server start
for i := 0; i < 10; i++ {
_, err := http.Get("http://127.0.0.1:8895/metricGet")
assert.Nil(t, err)
_, err = http.Post("http://127.0.0.1:8895/metricPost", "application/json", strings.NewReader(""))
assert.Nil(t, err)
}
metricsRes, err := http.Get("http://127.0.0.1:8896/metrics-buckets")
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, metricsRes.StatusCode)
defer metricsRes.Body.Close()
metricsResBytes, err := io.ReadAll(metricsRes.Body)
assert.Nil(t, err)
metricsResStr := string(metricsResBytes)
assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="500"} 0`))
assert.True(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="250000"} 10`))
// must not contains values in defaultBuckets
assert.False(t, strings.Contains(metricsResStr, `hertz_server_latency_us_bucket{method="POST",path="/metricPost",statusCode="200",le="500000"} 10`))
}