Skip to content

Commit

Permalink
fix: request metrics (#1007)
Browse files Browse the repository at this point in the history
http_request_* metrics contain data related only to /metrics/prometheus endpoint.
This commit adds endpoints from non-monitoring routers.

* fix: register read and write routers with PrometheusManager

* fix: register read and write routers with PrometheusManager p2

* fix: register read and write routers with PrometheusManager p3

* feat: grpc request metrics

* chore: add test

* chore: revert gRPC metric test

* chore: re-trigger ci

* chore: re-trigger ci

* chore: re-trigger ci

* fix: lint

* fix: cve
  • Loading branch information
nipsufn committed Sep 13, 2022
1 parent 9f983e6 commit 96ff767
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ require (
go.opentelemetry.io/proto/otlp v0.18.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.11 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,8 @@ golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su
golang.org/x/net v0.0.0-20220622184535-263ec571b305/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220708220712-1185a9018129 h1:vucSRfWwTsoXro7P+3Cjlr6flUMtzCwzlvkxEQtHHB0=
golang.org/x/net v0.0.0-20220708220712-1185a9018129/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 h1:D0B/7al0LLrVC8aWF4+oxpv/m8bc7ViFfVS8/gXGdqI=
golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -1261,6 +1263,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
6 changes: 6 additions & 0 deletions internal/driver/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ func (r *RegistryDefault) ReadRouter(ctx context.Context) http.Handler {
n.Use(reqlog.NewMiddlewareFromLogger(r.l, "read#Ory Keto").ExcludePaths(healthx.AliveCheckPath, healthx.ReadyCheckPath))

br := &x.ReadRouter{Router: httprouter.New()}
r.PrometheusManager().RegisterRouter(br.Router)
r.MetricsHandler().SetRoutes(br.Router)

r.HealthHandler().SetHealthRoutes(br.Router, false)
r.HealthHandler().SetVersionRoutes(br.Router)
Expand All @@ -303,6 +305,7 @@ func (r *RegistryDefault) ReadRouter(ctx context.Context) http.Handler {
}

n.UseHandler(br)
n.Use(r.PrometheusManager())

if r.sqaService != nil {
n.Use(r.sqaService)
Expand All @@ -325,6 +328,8 @@ func (r *RegistryDefault) WriteRouter(ctx context.Context) http.Handler {
n.Use(reqlog.NewMiddlewareFromLogger(r.l, "write#Ory Keto").ExcludePaths(healthx.AliveCheckPath, healthx.ReadyCheckPath))

pr := &x.WriteRouter{Router: httprouter.New()}
r.PrometheusManager().RegisterRouter(pr.Router)
r.MetricsHandler().SetRoutes(pr.Router)

r.HealthHandler().SetHealthRoutes(pr.Router, false)
r.HealthHandler().SetVersionRoutes(pr.Router)
Expand All @@ -334,6 +339,7 @@ func (r *RegistryDefault) WriteRouter(ctx context.Context) http.Handler {
}

n.UseHandler(pr)
n.Use(r.PrometheusManager())

if r.sqaService != nil {
n.Use(r.sqaService)
Expand Down
36 changes: 36 additions & 0 deletions internal/driver/daemon_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package driver

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"context"

prometheus "github.com/ory/x/prometheusx"
"github.com/stretchr/testify/require"

"github.com/ory/keto/internal/x/dbx"
)

const (
promLogLine = "promhttp_metric_handler_requests_total"
)

func TestMetricsHandler(t *testing.T) {
for _, dsn := range dbx.GetDSNs(t, false) {
r := NewTestRegistry(t, dsn)
handler := r.metricsRouter(context.Background())
server := httptest.NewServer(handler)
defer server.Close()

resp, err := http.Get(server.URL + prometheus.MetricsPrometheusPath)
require.NoError(t, err)
require.Equal(t, resp.StatusCode, http.StatusOK)

body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), promLogLine)
}
}

0 comments on commit 96ff767

Please sign in to comment.