-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_test.go
65 lines (53 loc) · 2.1 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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus/testutil"
)
func TestHistogramHttpHandler(t *testing.T) {
// Create a dummy handler to pass to HistogramHttpHandler
dummyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond) // Simulate some processing time
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
handler := HistogramHttpHandler(dummyHandler)
// Create a new HTTP request
req, err := http.NewRequest("GET", "/test/123", nil)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()
// Call the handler
handler.ServeHTTP(rr, req)
// Check the status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// Check the response body
expected := "OK"
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
const metadata = `
# HELP tdiscuss_http_request_duration_seconds Histogram of response latency (seconds) for HTTP requests.
# TYPE tdiscuss_http_request_duration_seconds histogram
`
// Check if the metric was recorded
_ = `
tdiscuss_http_request_duration_seconds{code="200",method="GET",path="/test/:id",le="0.005"} 0
tdiscuss_http_request_duration_seconds_bucket{code="200",method="GET",path="/test/:id",le="+Inf"} 0
tdiscuss_http_request_duration_seconds_sum{code="200",method="GET",path="/test/:id"} 0
tdiscuss_http_request_duration_seconds_count{code="200",method="GET",path="/test/:id"} 0
`
if count := testutil.CollectAndCount(httpRequestDuration); count == 0 {
t.Errorf("Expected metric to be recorded, but it was not")
}
// Check if the metric value is within expected range
// if err := testutil.CollectAndCompare(httpRequestDuration, strings.NewReader(metadata+metric), "tdiscuss_http_request_duration_seconds"); err != nil {
// t.Errorf("Unexpected metric value: %v", err)
// }
}