22217: Enable contrib suites with our own code, not add-apt-repository
[arvados.git] / services / keepstore / metrics_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package keepstore
6
7 import (
8         "context"
9         "encoding/json"
10         "net/http"
11
12         "git.arvados.org/arvados.git/sdk/go/arvados"
13         "git.arvados.org/arvados.git/sdk/go/arvadostest"
14         "git.arvados.org/arvados.git/sdk/go/ctxlog"
15         "git.arvados.org/arvados.git/sdk/go/httpserver"
16         "github.com/prometheus/client_golang/prometheus"
17         . "gopkg.in/check.v1"
18 )
19
20 func (s *routerSuite) TestMetrics(c *C) {
21         reg := prometheus.NewRegistry()
22         router, cancel := testRouter(c, s.cluster, reg)
23         defer cancel()
24         instrumented := httpserver.Instrument(reg, ctxlog.TestLogger(c), router)
25         handler := instrumented.ServeAPI(s.cluster.ManagementToken, instrumented)
26
27         router.keepstore.BlockWrite(context.Background(), arvados.BlockWriteOptions{
28                 Hash: fooHash,
29                 Data: []byte("foo"),
30         })
31         router.keepstore.BlockWrite(context.Background(), arvados.BlockWriteOptions{
32                 Hash: barHash,
33                 Data: []byte("bar"),
34         })
35
36         // prime the metrics by doing a no-op request
37         resp := call(handler, "GET", "/", "", nil, nil)
38
39         resp = call(handler, "GET", "/metrics.json", "", nil, nil)
40         c.Check(resp.Code, Equals, http.StatusUnauthorized)
41         resp = call(handler, "GET", "/metrics.json", "foobar", nil, nil)
42         c.Check(resp.Code, Equals, http.StatusForbidden)
43         resp = call(handler, "GET", "/metrics.json", arvadostest.ManagementToken, nil, nil)
44         c.Check(resp.Code, Equals, http.StatusOK)
45         var j []struct {
46                 Name   string
47                 Help   string
48                 Type   string
49                 Metric []struct {
50                         Label []struct {
51                                 Name  string
52                                 Value string
53                         }
54                         Summary struct {
55                                 SampleCount string
56                                 SampleSum   float64
57                         }
58                 }
59         }
60         json.NewDecoder(resp.Body).Decode(&j)
61         found := make(map[string]bool)
62         names := map[string]bool{}
63         for _, g := range j {
64                 names[g.Name] = true
65                 for _, m := range g.Metric {
66                         if len(m.Label) == 2 && m.Label[0].Name == "code" && m.Label[0].Value == "200" && m.Label[1].Name == "method" && m.Label[1].Value == "put" {
67                                 c.Check(m.Summary.SampleCount, Equals, "2")
68                                 found[g.Name] = true
69                         }
70                 }
71         }
72
73         metricsNames := []string{
74                 "arvados_keepstore_bufferpool_inuse_buffers",
75                 "arvados_keepstore_bufferpool_max_buffers",
76                 "arvados_keepstore_bufferpool_allocated_bytes",
77                 "arvados_keepstore_pull_queue_inprogress_entries",
78                 "arvados_keepstore_pull_queue_pending_entries",
79                 "arvados_keepstore_trash_queue_inprogress_entries",
80                 "arvados_keepstore_trash_queue_pending_entries",
81                 "request_duration_seconds",
82         }
83         for _, m := range metricsNames {
84                 _, ok := names[m]
85                 c.Check(ok, Equals, true, Commentf("checking metric %q", m))
86         }
87 }