Skip to content

Commit

Permalink
feat: remove openapi and metrcs middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebianchi committed Jun 26, 2023
1 parent 614942e commit 87e8148
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 257 deletions.
5 changes: 2 additions & 3 deletions core/opa_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package core

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -24,11 +25,9 @@ import (
"strconv"
"testing"

"github.com/rond-authz/rond/internal/metrics"
"github.com/rond-authz/rond/internal/mocks"
"github.com/rond-authz/rond/internal/mongoclient"
"github.com/rond-authz/rond/internal/utils"
"github.com/rond-authz/rond/openapi"
"github.com/rond-authz/rond/types"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
Expand Down Expand Up @@ -281,7 +280,7 @@ func TestOPATransportRoundTrip(t *testing.T) {
}

logEntry := logrus.NewEntry(logger)
req = req.Clone(metrics.WithValue(openapi.WithRouterInfo(logEntry, req.Context(), req), metrics.SetupMetrics("")))
req = req.Clone(context.Background())

evaluator := getSdk(t, &sdkOptions{
oasFilePath: "../mocks/rondOasConfig.json",
Expand Down
29 changes: 0 additions & 29 deletions internal/metrics/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
package metrics

import (
"context"
"fmt"
"net/http"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -35,28 +31,3 @@ func MetricsRoute(r *mux.Router, registry *prometheus.Registry) {
}),
))
}

type metricsContextKey struct{}

// RequestMiddleware is a gorilla/mux middleware used to inject
// metrics struct into requests.
func RequestMiddleware(m Metrics) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := WithValue(r.Context(), m)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}

func WithValue(ctx context.Context, m Metrics) context.Context {
return context.WithValue(ctx, metricsContextKey{}, m)
}

func GetFromContext(ctx context.Context) (Metrics, error) {
m, ok := ctx.Value(metricsContextKey{}).(Metrics)
if !ok {
return Metrics{}, fmt.Errorf("invalid metrics in context")
}
return m, nil
}
42 changes: 10 additions & 32 deletions internal/metrics/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,26 @@
package metrics

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

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)

func TestRequestMiddleware(t *testing.T) {
expectedMetrics := SetupMetrics("test_prefix")

t.Run("set metrics in context", func(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m, err := GetFromContext(r.Context())
require.NoError(t, err)
require.Equal(t, expectedMetrics, m)

w.WriteHeader(202)
})

handlerToTest := RequestMiddleware(expectedMetrics).Middleware(handler)
func TestMetricsRoute(t *testing.T) {
t.Run("exposes metrics route", func(t *testing.T) {
router := mux.NewRouter()
registry := prometheus.NewRegistry()
MetricsRoute(router, registry)

req := httptest.NewRequest(http.MethodGet, MetricsRoutePath, nil)
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/path", nil)
handlerToTest.ServeHTTP(w, req)

require.Equal(t, http.StatusAccepted, w.Result().StatusCode)
})
}

func TestGetFromContext(t *testing.T) {
t.Run("ok", func(t *testing.T) {
expectedMetrics := SetupMetrics("test_prefix")
ctx := WithValue(context.Background(), expectedMetrics)
m, err := GetFromContext(ctx)
require.NoError(t, err)
require.Equal(t, expectedMetrics, m)
})
router.ServeHTTP(w, req)

t.Run("metrics not in context", func(t *testing.T) {
m, err := GetFromContext(context.Background())
require.EqualError(t, err, "invalid metrics in context")
require.Empty(t, m)
require.Equal(t, http.StatusOK, w.Result().StatusCode)
})
}
6 changes: 6 additions & 0 deletions openapi/openapi_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ var (

var ErrNotFoundOASDefinition = errors.New("not found oas definition")

type RouterInfo struct {
MatchedPath string
RequestedPath string
Method string
}

type XPermissionKey struct{}

type PermissionOptions struct {
Expand Down
65 changes: 0 additions & 65 deletions openapi/routerinfo_middleware.go

This file was deleted.

127 changes: 0 additions & 127 deletions openapi/routerinfo_middleware_test.go

This file was deleted.

1 change: 0 additions & 1 deletion service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func SetupRouter(
StatusRoutes(router, serviceName, env.ServiceVersion)

metrics.MetricsRoute(router, registry)
router.Use(metrics.RequestMiddleware(sdk.Metrics()))

router.Use(config.RequestMiddlewareEnvironments(env))

Expand Down

0 comments on commit 87e8148

Please sign in to comment.