Skip to content

Commit 9f26d0b

Browse files
committed
test(target allocator): cover /metrics gatherer option and handler
1 parent 1287c22 commit 9f26d0b

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

cmd/otel-allocator/internal/server/server_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"testing"
1515
"time"
1616

17+
"github.com/prometheus/client_golang/prometheus"
1718
"github.com/prometheus/common/config"
1819
"github.com/prometheus/common/model"
1920
promconfig "github.com/prometheus/prometheus/config"
@@ -1367,3 +1368,27 @@ func TestServer_EmptyTargetReturnsEmptyList(t *testing.T) {
13671368
require.NoError(t, err)
13681369
assert.Empty(t, items)
13691370
}
1371+
1372+
func TestServer_MetricsHandler(t *testing.T) {
1373+
// With a custom gatherer, /metrics serves it (covers WithMetricsGatherer + the
1374+
// gatherer branch of metricsHandler).
1375+
reg := prometheus.NewRegistry()
1376+
counter := prometheus.NewCounter(prometheus.CounterOpts{Name: "ta_test_metric_total", Help: "test"})
1377+
reg.MustRegister(counter)
1378+
counter.Inc()
1379+
1380+
s, err := NewServer(logger, nil, "", WithMetricsGatherer(reg))
1381+
require.NoError(t, err)
1382+
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/metrics", http.NoBody)
1383+
w := httptest.NewRecorder()
1384+
s.server.Handler.ServeHTTP(w, req)
1385+
require.Equal(t, http.StatusOK, w.Code)
1386+
assert.Contains(t, w.Body.String(), "ta_test_metric_total")
1387+
1388+
// Without the option, /metrics falls back to the default gatherer (covers the nil branch).
1389+
sd, err := NewServer(logger, nil, "")
1390+
require.NoError(t, err)
1391+
wd := httptest.NewRecorder()
1392+
sd.server.Handler.ServeHTTP(wd, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/metrics", http.NoBody))
1393+
require.Equal(t, http.StatusOK, wd.Code)
1394+
}

0 commit comments

Comments
 (0)