Change logging of registered v2 resource endpoints to add /api prefix (#20352)

* Change logging of registered v2 resource endpoints to add /api prefix

Previous:

    agent.http: Registered resource endpoint: endpoint=/demo/v1/executive

New:

    agent.http: Registered resource endpoint: endpoint=/api/demo/v1/executive

This reduces confusion when attempting to call the APIs after looking at
the logs.
This commit is contained in:
Luke Kysow 2024-01-25 14:18:54 -08:00 committed by GitHub
parent 91a783a980
commit 840f11a0c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 7 deletions

3
.changelog/20352.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
logging: add /api prefix to v2 resource endpoint logs
```

View File

@ -260,9 +260,11 @@ func (s *HTTPHandlers) handler() http.Handler {
handlePProf("/debug/pprof/symbol", pprof.Symbol) handlePProf("/debug/pprof/symbol", pprof.Symbol)
handlePProf("/debug/pprof/trace", pprof.Trace) handlePProf("/debug/pprof/trace", pprof.Trace)
mux.Handle("/api/", resourceAPIPrefix := "/api"
http.StripPrefix("/api", mux.Handle(resourceAPIPrefix+"/",
http.StripPrefix(resourceAPIPrefix,
resourcehttp.NewHandler( resourcehttp.NewHandler(
resourceAPIPrefix,
s.agent.delegate.ResourceServiceClient(), s.agent.delegate.ResourceServiceClient(),
s.agent.baseDeps.Registry, s.agent.baseDeps.Registry,
s.parseToken, s.parseToken,

View File

@ -28,7 +28,14 @@ const (
HeaderConsistencyMode = "x-consul-consistency-mode" HeaderConsistencyMode = "x-consul-consistency-mode"
) )
// NewHandler creates a new HTTP handler for the resource service.
// httpPathPrefix is the prefix to be used for all HTTP endpoints. Should start with "/" and
// end without a trailing "/".
// client is the gRPC client to be used to communicate with the resource service.
// registry is the resource registry to be used to determine the resource types.
// parseToken is a function that will be called to parse the Consul token from the request.
func NewHandler( func NewHandler(
httpPathPrefix string,
client pbresource.ResourceServiceClient, client pbresource.ResourceServiceClient,
registry resource.Registry, registry resource.Registry,
parseToken func(req *http.Request, token *string), parseToken func(req *http.Request, token *string),
@ -38,10 +45,10 @@ func NewHandler(
// List Endpoint // List Endpoint
base := strings.ToLower(fmt.Sprintf("/%s/%s/%s", t.Type.Group, t.Type.GroupVersion, t.Type.Kind)) base := strings.ToLower(fmt.Sprintf("/%s/%s/%s", t.Type.Group, t.Type.GroupVersion, t.Type.Kind))
mux.Handle(base, http.StripPrefix(base, &listHandler{t, client, parseToken, logger})) mux.Handle(base, http.StripPrefix(base, &listHandler{t, client, parseToken, logger}))
logger.Info("Registered resource endpoint", "endpoint", fmt.Sprintf("%s%s", httpPathPrefix, base))
// Individual Resource Endpoints // Individual Resource Endpoints
prefix := strings.ToLower(fmt.Sprintf("%s/", base)) prefix := strings.ToLower(fmt.Sprintf("%s/", base))
logger.Info("Registered resource endpoint", "endpoint", prefix)
mux.Handle(prefix, http.StripPrefix(prefix, &resourceHandler{t, client, parseToken, logger})) mux.Handle(prefix, http.StripPrefix(prefix, &resourceHandler{t, client, parseToken, logger}))
} }

View File

@ -139,7 +139,7 @@ func TestResourceWriteHandler(t *testing.T) {
WithACLResolver(aclResolver). WithACLResolver(aclResolver).
WithRegisterFns(demo.RegisterTypes) WithRegisterFns(demo.RegisterTypes)
client := builder.Run(t) client := builder.Run(t)
handler := NewHandler(client, builder.Registry(), parseToken, hclog.NewNullLogger()) handler := NewHandler("/api", client, builder.Registry(), parseToken, hclog.NewNullLogger())
t.Run("should be blocked if the token is not authorized", func(t *testing.T) { t.Run("should be blocked if the token is not authorized", func(t *testing.T) {
rsp := httptest.NewRecorder() rsp := httptest.NewRecorder()
@ -369,7 +369,7 @@ func TestResourceReadHandler(t *testing.T) {
WithRegisterFns(demo.RegisterTypes). WithRegisterFns(demo.RegisterTypes).
WithACLResolver(aclResolver) WithACLResolver(aclResolver)
client := builder.Run(t) client := builder.Run(t)
handler := NewHandler(client, builder.Registry(), parseToken, hclog.NewNullLogger()) handler := NewHandler("/api", client, builder.Registry(), parseToken, hclog.NewNullLogger())
createdResource := createResource(t, handler, nil) createdResource := createResource(t, handler, nil)
@ -422,7 +422,7 @@ func TestResourceDeleteHandler(t *testing.T) {
WithRegisterFns(demo.RegisterTypes). WithRegisterFns(demo.RegisterTypes).
WithACLResolver(aclResolver) WithACLResolver(aclResolver)
client := builder.Run(t) client := builder.Run(t)
handler := NewHandler(client, builder.Registry(), parseToken, hclog.NewNullLogger()) handler := NewHandler("/api", client, builder.Registry(), parseToken, hclog.NewNullLogger())
t.Run("should surface PermissionDenied error from resource service", func(t *testing.T) { t.Run("should surface PermissionDenied error from resource service", func(t *testing.T) {
createResource(t, handler, nil) createResource(t, handler, nil)
@ -498,7 +498,7 @@ func TestResourceListHandler(t *testing.T) {
WithRegisterFns(demo.RegisterTypes). WithRegisterFns(demo.RegisterTypes).
WithACLResolver(aclResolver) WithACLResolver(aclResolver)
client := builder.Run(t) client := builder.Run(t)
handler := NewHandler(client, builder.Registry(), parseToken, hclog.NewNullLogger()) handler := NewHandler("/api", client, builder.Registry(), parseToken, hclog.NewNullLogger())
t.Run("should return MethodNotAllowed", func(t *testing.T) { t.Run("should return MethodNotAllowed", func(t *testing.T) {
rsp := httptest.NewRecorder() rsp := httptest.NewRecorder()