// Copyright © 2026 Ory Corp // SPDX-License-Identifier: Apache-2.0 package daemon import ( "fmt" "io" "net/http" "testing" "time" "github.com/phayes/freeport" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/sync/errgroup" "github.com/ory/x/configx" "github.com/ory/kratos/pkg" ) func TestMetricsRouterPaths(t *testing.T) { // TODO refactor this test to be parallelizable once we rewrite the server setup to be properly testable ports, err := freeport.GetFreePorts(2) require.NoError(t, err) adminPort, publicPort := ports[0], ports[1] cmd := &cobra.Command{} cmd.Flags().Bool("sqa-opt-out", true, "") _, reg := pkg.NewFastRegistryWithMocks(t, configx.WithValues(map[string]any{ "serve.admin.port": adminPort, "serve.public.port": publicPort, })) startAdmin, err := serveAdmin(t.Context(), reg, cmd) require.NoError(t, err) startPublic, err := servePublic(t.Context(), reg, cmd) require.NoError(t, err) eg, _ := errgroup.WithContext(t.Context()) eg.Go(startAdmin) eg.Go(startPublic) transport := http.DefaultTransport.(*http.Transport).Clone() t.Cleanup(transport.CloseIdleConnections) client := &http.Client{Transport: transport} require.EventuallyWithT(t, func(t *assert.CollectT) { resp, err := client.Get(fmt.Sprintf("http://127.0.0.1:%d/health/ready", publicPort)) require.NoError(t, err) body, err := io.ReadAll(resp.Body) require.NoError(t, err) assert.Equalf(t, http.StatusOK, resp.StatusCode, "%s", body) }, 2*time.Second, 10*time.Millisecond) // Make some requests that should be recorded in the metrics req, _ := http.NewRequest(http.MethodDelete, fmt.Sprintf("http://127.0.0.1:%d/sessions/session-id", publicPort), nil) resp, err := client.Do(req) require.NoError(t, err) require.Equal(t, http.StatusUnauthorized, resp.StatusCode) resp, err = client.Get(fmt.Sprintf("http://127.0.0.1:%d/admin/identities/8e5f29ec-db29-4e56-8517-75f7d4e2f1bc/sessions", adminPort)) require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) assert.EventuallyWithT(t, func(t *assert.CollectT) { res, err := client.Get(fmt.Sprintf("http://127.0.0.1:%d/admin/metrics/prometheus", adminPort)) require.NoError(t, err) require.EqualValues(t, http.StatusOK, res.StatusCode) respBody, err := io.ReadAll(res.Body) body := string(respBody) require.NoError(t, err) assert.Contains(t, body, `endpoint="/sessions/{param}"`) assert.Contains(t, body, `endpoint="/admin/identities/{param}/sessions"`) }, 5*time.Second, 100*time.Millisecond) }