|
| 1 | +package gh |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/m4ns0ur/httpcache" |
| 10 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 11 | +) |
| 12 | + |
| 13 | +type stubTransport struct { |
| 14 | + resp *http.Response |
| 15 | + err error |
| 16 | +} |
| 17 | + |
| 18 | +func (s *stubTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 19 | + if s.resp != nil && s.resp.Request == nil { |
| 20 | + s.resp.Request = req |
| 21 | + } |
| 22 | + return s.resp, s.err |
| 23 | +} |
| 24 | + |
| 25 | +type timeoutErr struct{} |
| 26 | + |
| 27 | +func (timeoutErr) Error() string { return "timeout" } |
| 28 | +func (timeoutErr) Timeout() bool { return true } |
| 29 | +func (timeoutErr) Temporary() bool { return true } |
| 30 | + |
| 31 | +func TestInstrumentedTransportMetrics(t *testing.T) { |
| 32 | + req, err := http.NewRequest(http.MethodGet, "https://api.github.com/repos/org/repo", nil) |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("failed to create request: %v", err) |
| 35 | + } |
| 36 | + path := req.URL.Path |
| 37 | + method := req.Method |
| 38 | + |
| 39 | + resp := &http.Response{ |
| 40 | + StatusCode: http.StatusOK, |
| 41 | + Header: make(http.Header), |
| 42 | + Body: io.NopCloser(bytes.NewBufferString("ok")), |
| 43 | + } |
| 44 | + |
| 45 | + transport := newInstrumentedTransport(&stubTransport{resp: resp}) |
| 46 | + |
| 47 | + baseReq := testutil.ToFloat64(githubAPIRequestsTotal.WithLabelValues(path, method, "2xx")) |
| 48 | + baseCache := testutil.ToFloat64(githubAPICacheTotal.WithLabelValues(path, method, "miss")) |
| 49 | + baseInflight := testutil.ToFloat64(githubAPIInflight.WithLabelValues(path, method)) |
| 50 | + |
| 51 | + if _, err := transport.RoundTrip(req); err != nil { |
| 52 | + t.Fatalf("RoundTrip error: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + if got := testutil.ToFloat64(githubAPIRequestsTotal.WithLabelValues(path, method, "2xx")); got != baseReq+1 { |
| 56 | + t.Fatalf("requests_total mismatch: got=%v want=%v", got, baseReq+1) |
| 57 | + } |
| 58 | + if got := testutil.ToFloat64(githubAPICacheTotal.WithLabelValues(path, method, "miss")); got != baseCache+1 { |
| 59 | + t.Fatalf("cache_total miss mismatch: got=%v want=%v", got, baseCache+1) |
| 60 | + } |
| 61 | + if got := testutil.ToFloat64(githubAPIInflight.WithLabelValues(path, method)); got != baseInflight { |
| 62 | + t.Fatalf("inflight mismatch: got=%v want=%v", got, baseInflight) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestInstrumentedTransportCacheHit(t *testing.T) { |
| 67 | + req, err := http.NewRequest(http.MethodGet, "https://api.github.com/repos/org/repo", nil) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("failed to create request: %v", err) |
| 70 | + } |
| 71 | + path := req.URL.Path |
| 72 | + method := req.Method |
| 73 | + |
| 74 | + resp := &http.Response{ |
| 75 | + StatusCode: http.StatusOK, |
| 76 | + Header: make(http.Header), |
| 77 | + Body: io.NopCloser(bytes.NewBufferString("cached")), |
| 78 | + } |
| 79 | + resp.Header.Set(httpcache.XFromCache, "1") |
| 80 | + |
| 81 | + transport := newInstrumentedTransport(&stubTransport{resp: resp}) |
| 82 | + |
| 83 | + baseCache := testutil.ToFloat64(githubAPICacheTotal.WithLabelValues(path, method, "hit")) |
| 84 | + |
| 85 | + if _, err := transport.RoundTrip(req); err != nil { |
| 86 | + t.Fatalf("RoundTrip error: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + if got := testutil.ToFloat64(githubAPICacheTotal.WithLabelValues(path, method, "hit")); got != baseCache+1 { |
| 90 | + t.Fatalf("cache_total hit mismatch: got=%v want=%v", got, baseCache+1) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestInstrumentedTransportErrorMetrics(t *testing.T) { |
| 95 | + req, err := http.NewRequest(http.MethodGet, "https://api.github.com/repos/org/repo", nil) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("failed to create request: %v", err) |
| 98 | + } |
| 99 | + path := req.URL.Path |
| 100 | + method := req.Method |
| 101 | + |
| 102 | + transport := newInstrumentedTransport(&stubTransport{err: timeoutErr{}}) |
| 103 | + |
| 104 | + baseReq := testutil.ToFloat64(githubAPIRequestsTotal.WithLabelValues(path, method, "error")) |
| 105 | + baseErr := testutil.ToFloat64(githubAPIErrorsTotal.WithLabelValues(path, method, "timeout")) |
| 106 | + |
| 107 | + if _, err := transport.RoundTrip(req); err == nil { |
| 108 | + t.Fatal("expected error, got nil") |
| 109 | + } |
| 110 | + |
| 111 | + if got := testutil.ToFloat64(githubAPIRequestsTotal.WithLabelValues(path, method, "error")); got != baseReq+1 { |
| 112 | + t.Fatalf("requests_total error mismatch: got=%v want=%v", got, baseReq+1) |
| 113 | + } |
| 114 | + if got := testutil.ToFloat64(githubAPIErrorsTotal.WithLabelValues(path, method, "timeout")); got != baseErr+1 { |
| 115 | + t.Fatalf("errors_total mismatch: got=%v want=%v", got, baseErr+1) |
| 116 | + } |
| 117 | +} |
0 commit comments