Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Creasy <[email protected]>
  • Loading branch information
alexcreasy committed Nov 19, 2024
1 parent 90f7f9c commit 4178269
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion clients/ui/bff/internal/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ func (app *App) Routes() http.Handler {
router.GET(ModelRegistryListPath, app.ModelRegistryHandler)
router.PATCH(ModelRegistryPath, app.AttachRESTClient(app.UpdateModelVersionHandler))

return app.RecoverPanic(app.enableCORS(router))
return app.RecoverPanic(app.InitializeContext(app.enableCORS(router)))
}
24 changes: 23 additions & 1 deletion clients/ui/bff/internal/api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"fmt"
"net/http"

"github.com/google/uuid"
"github.com/julienschmidt/httprouter"
"github.com/kubeflow/model-registry/ui/bff/internal/config"
"github.com/kubeflow/model-registry/ui/bff/internal/integrations"
"log/slog"
)

type contextKey string

const requestIdKey contextKey = "requestIdKey"
const httpClientKey contextKey = "httpClientKey"
const userAccessToken = "x-forwarded-access-token"

Expand All @@ -38,6 +41,15 @@ func (app *App) enableCORS(next http.Handler) http.Handler {
})
}

func (app *App) InitializeContext(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Adds a unique id to the context to allow tracing of requests
ctx := context.WithValue(r.Context(), requestIdKey, uuid.NewString())

next.ServeHTTP(w, r.WithContext(ctx))
})
}

func (app *App) AttachRESTClient(handler func(http.ResponseWriter, *http.Request, httprouter.Params)) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

Expand All @@ -55,7 +67,17 @@ func (app *App) AttachRESTClient(handler func(http.ResponseWriter, *http.Request
return
}

client, err := integrations.NewHTTPClient(modelRegistryBaseURL, bearerToken, app.logger)
// Creates a child logger with
var restClientLogger *slog.Logger
requestId, ok := r.Context().Value(requestIdKey).(string)
if ok {
restClientLogger = app.logger.With(slog.String("request_id", requestId))
} else {
app.logger.Warn("Failed to set request_id for tracing")
restClientLogger = app.logger
}

client, err := integrations.NewHTTPClient(modelRegistryBaseURL, bearerToken, restClientLogger)
if err != nil {
app.serverErrorResponse(w, r, fmt.Errorf("failed to create Kubernetes client: %v", err))
return
Expand Down
10 changes: 9 additions & 1 deletion clients/ui/bff/internal/integrations/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func (c *HTTPClient) GET(url string) ([]byte, error) {
return nil, err
}

//c.logger.Debug("Making upstream request", "method", req.Method, "url", req.URL.String(), "body", nil)
logUpstreamReq(c.logger, req)
req.Header.Add("Authorization", "Bearer "+c.bearerToken)
c.logger.Debug("Upstream Request", "url")
response, err := c.client.Do(req)
if err != nil {
return nil, err
Expand All @@ -79,6 +80,8 @@ func (c *HTTPClient) POST(url string, body io.Reader) ([]byte, error) {
return nil, err
}

logUpstreamReq(c.logger, req)

req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+c.bearerToken)

Expand Down Expand Up @@ -154,3 +157,8 @@ func (c *HTTPClient) PATCH(url string, body io.Reader) ([]byte, error) {
}
return responseBody, nil
}

func logUpstreamReq(logger *slog.Logger, req *http.Request) {
//if logger.Enabled(logger.con)
logger.Debug("-> Upstream HTTP request", "method", req.Method, "url", req.URL.String(), "body", req.Body)
}

0 comments on commit 4178269

Please sign in to comment.