Skip to content

Commit 1c98ca4

Browse files
authored
metrics: Use custom registry to avoid metric conflicts (#188)
* metrics: Use custom registry to avoid metric conflicts * api: Create a small test for the metrics * Revert "metrics: Use custom registry to avoid metric conflicts" This reverts commit fcd215b. * api: Add some failing tests * Revert "Revert "metrics: Use custom registry to avoid metric conflicts"" This reverts commit 0016b52.
1 parent b3466d2 commit 1c98ca4

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

api/handler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/livepeer/task-runner/clients"
1212
"github.com/livepeer/task-runner/metrics"
1313
"github.com/livepeer/task-runner/task"
14-
"github.com/prometheus/client_golang/prometheus/promhttp"
1514
)
1615

1716
type APIHandlerOptions struct {
@@ -32,7 +31,7 @@ func NewHandler(serverCtx context.Context, opts APIHandlerOptions, runner task.R
3231

3332
router.HandlerFunc("GET", "/_healthz", router.healthcheck)
3433
if opts.Prometheus {
35-
router.Handler("GET", "/metrics", promhttp.Handler())
34+
router.Handler("GET", "/metrics", metrics.ScrapeHandler())
3635
}
3736

3837
hookHandler := metrics.ObservedHandlerFunc("catalyst_hook", router.catalystHook)

api/handler_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package api
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/livepeer/task-runner/clients"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestMetricsEndpoint(t *testing.T) {
14+
require := require.New(t)
15+
ctx, cancel := context.WithCancel(context.Background())
16+
defer cancel()
17+
18+
handler := NewHandler(ctx, APIHandlerOptions{
19+
Catalyst: &clients.CatalystOptions{Secret: "🤫"},
20+
Prometheus: true,
21+
}, nil)
22+
23+
req, err := http.NewRequest("GET", "http://localhost/metrics", nil)
24+
require.NoError(err)
25+
26+
rw := httptest.NewRecorder()
27+
handler.ServeHTTP(rw, req)
28+
29+
res := rw.Result()
30+
require.Equal(http.StatusOK, res.StatusCode)
31+
require.Contains(res.Header.Get("Content-Type"), "text/plain")
32+
33+
body := rw.Body.String()
34+
require.NotEmpty(body)
35+
36+
// check that it contains some random metric
37+
require.Contains(body, "# TYPE livepeer_task_runner_http_requests_in_flight gauge\n")
38+
// check that it does NOT contain metrics from some libs we use
39+
require.NotContains(body, "catalyst-api")
40+
require.NotContains(body, "transcode_segment_duration_seconds")
41+
require.NotContains(body, "livepeer_analyzer")
42+
}

metrics/factory.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
package metrics
22

33
import (
4+
"net/http"
5+
46
"github.com/prometheus/client_golang/prometheus"
57
"github.com/prometheus/client_golang/prometheus/promauto"
8+
"github.com/prometheus/client_golang/prometheus/promhttp"
69
)
710

811
var (
912
Namespace = "livepeer"
1013
Subsystem = "task_runner"
11-
Factory = promauto.With(prometheus.DefaultRegisterer)
14+
// We use catalyst-api as a library which uses prometheus lib and default
15+
// registry. Avoid metric conflicts by using a custom registry.
16+
Registry = prometheus.NewRegistry()
17+
Factory = promauto.With(Registry)
1218
)
1319

1420
func FQName(name string) string {
1521
return prometheus.BuildFQName(Namespace, Subsystem, name)
1622
}
23+
24+
func ScrapeHandler() http.Handler {
25+
handler := promhttp.HandlerFor(Registry, promhttp.HandlerOpts{})
26+
handler = promhttp.InstrumentMetricHandler(Registry, handler)
27+
return handler
28+
}

0 commit comments

Comments
 (0)