metrics_test.go139 lines, 4.2 KB
| 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/fiston-user/murmur/internal/jobs" |
| 13 | "github.com/fiston-user/murmur/internal/platform/testdb" |
| 14 | "github.com/prometheus/client_golang/prometheus" |
| 15 | "github.com/prometheus/client_golang/prometheus/testutil" |
| 16 | ) |
| 17 | |
| 18 | // measured is a small API behind Wrap, the way cmd/api builds the real one. |
| 19 | func measured(t *testing.T) (*HTTP, *httptest.Server) { |
| 20 | m := NewHTTP(prometheus.NewRegistry()) |
| 21 | mux := http.NewServeMux() |
| 22 | mux.HandleFunc("POST /posts/{id}/like", func(w http.ResponseWriter, r *http.Request) { |
| 23 | w.WriteHeader(http.StatusNoContent) |
| 24 | }) |
| 25 | mux.HandleFunc("GET /feed", func(w http.ResponseWriter, r *http.Request) { |
| 26 | fmt.Fprint(w, "[]") // no WriteHeader: that's a 200 |
| 27 | }) |
| 28 | mux.HandleFunc("GET /live", func(w http.ResponseWriter, r *http.Request) { |
| 29 | w.Header().Set("Content-Type", "text/event-stream") |
| 30 | fmt.Fprint(w, ": connected\n\n") |
| 31 | if err := http.NewResponseController(w).Flush(); err != nil { |
| 32 | t.Errorf("the stream can't flush: %v", err) |
| 33 | return |
| 34 | } |
| 35 | <-r.Context().Done() |
| 36 | }) |
| 37 | srv := httptest.NewServer(m.Wrap(mux.ServeHTTP)) |
| 38 | t.Cleanup(srv.Close) |
| 39 | return m, srv |
| 40 | } |
| 41 | |
| 42 | // The route label is the pattern, never the path: a million posts are one series. |
| 43 | func TestRouteIsThePattern(t *testing.T) { |
| 44 | m, srv := measured(t) |
| 45 | for _, post := range []string{"7", "8", "2000001"} { |
| 46 | res, err := http.Post(srv.URL+"/posts/"+post+"/like", "", nil) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | res.Body.Close() |
| 51 | } |
| 52 | for _, path := range []string{"/feed", "/nothing/here"} { |
| 53 | res, err := http.Get(srv.URL + path) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | res.Body.Close() |
| 58 | } |
| 59 | for _, want := range []struct { |
| 60 | route, status string |
| 61 | n float64 |
| 62 | }{ |
| 63 | {"POST /posts/{id}/like", "204", 3}, |
| 64 | {"GET /feed", "200", 1}, |
| 65 | {"other", "404", 1}, |
| 66 | } { |
| 67 | got := testutil.ToFloat64(m.requests.WithLabelValues(want.route, want.status)) |
| 68 | if got != want.n { |
| 69 | t.Errorf("%s %s: counted %v requests, want %v", want.route, want.status, got, want.n) |
| 70 | } |
| 71 | } |
| 72 | if n := testutil.CollectAndCount(m.requests); n != 3 { |
| 73 | t.Errorf("the counter has %d series, want 3", n) |
| 74 | } |
| 75 | // one histogram for every route that answered |
| 76 | if n := testutil.CollectAndCount(m.duration); n != 3 { |
| 77 | t.Errorf("%d routes were timed, want 3", n) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // A stream goes through the middleware untouched: its first line arrives while |
| 82 | // the request is still open. And a stream is counted, but it is not timed. |
| 83 | func TestStreamStillFlushes(t *testing.T) { |
| 84 | m, srv := measured(t) |
| 85 | ctx, hangUp := context.WithCancel(context.Background()) |
| 86 | req, _ := http.NewRequestWithContext(ctx, "GET", srv.URL+"/live", nil) |
| 87 | res, err := http.DefaultClient.Do(req) |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | defer res.Body.Close() |
| 92 | line, _ := bufio.NewReader(res.Body).ReadString('\n') |
| 93 | if strings.TrimSpace(line) != ": connected" { |
| 94 | t.Fatalf("the stream said %q, want the connected comment", line) |
| 95 | } |
| 96 | hangUp() |
| 97 | srv.Close() // it waits until the handler has returned |
| 98 | if got := testutil.ToFloat64(m.requests.WithLabelValues("GET /live", "200")); got != 1 { |
| 99 | t.Errorf("counted %v streams, want 1", got) |
| 100 | } |
| 101 | if n := testutil.CollectAndCount(m.duration); n != 0 { |
| 102 | t.Errorf("%d routes were timed, want none: a stream is not a slow request", n) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // The queue's numbers come from Postgres at the moment somebody asks. |
| 107 | func TestQueueIsReadWhenAsked(t *testing.T) { |
| 108 | db := testdb.New(t) |
| 109 | ctx := context.Background() |
| 110 | q := NewQueue(db) |
| 111 | const waiting = ` |
| 112 | # HELP murmur_queue_jobs Jobs in the queue. |
| 113 | # TYPE murmur_queue_jobs gauge |
| 114 | murmur_queue_jobs{status="dead"} 0 |
| 115 | murmur_queue_jobs{status="running"} 0 |
| 116 | murmur_queue_jobs{status="waiting"} %d |
| 117 | # HELP murmur_like_deltas Likes not yet in a post's count. |
| 118 | # TYPE murmur_like_deltas gauge |
| 119 | murmur_like_deltas %d |
| 120 | ` |
| 121 | check := func(jobs, deltas int) { |
| 122 | t.Helper() |
| 123 | want := strings.NewReader(fmt.Sprintf(waiting, jobs, deltas)) |
| 124 | err := testutil.CollectAndCompare(q, want, "murmur_queue_jobs", "murmur_like_deltas") |
| 125 | if err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | } |
| 129 | check(0, 0) |
| 130 | for range 3 { |
| 131 | if err := jobs.Enqueue(ctx, db, "like", map[string]int{"post": 1}); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | } |
| 135 | if _, err := db.Exec(ctx, `insert into like_deltas (post_id, delta) values (1, 1), (1, 1)`); err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | check(3, 2) |
| 139 | } |