Merge pull request 'fix(telemetry): name tenant sub-resource requests after their collection' (#107) from fix/telemetry-sub-route into main
ci / shared (push) Successful in 13s
ci / test (push) Successful in 25m10s
ci / image (push) Successful in 41s

This commit was merged in pull request #107.
This commit is contained in:
sharang committed 2026-10-30 07:31:48 +00:00
commit 9af8b283a2
3 files changed
+73 -1

No files matched your search

+8
View File
@@ -69,6 +69,14 @@ func (s *Server) tenantInScope(ctx context.Context, w http.ResponseWriter, r *ht
}
// listTenantMembers is GET /v1/tenants/{id}/members.
// tenantSubresources are the collections tenantSubresource serves (keep the
// two in step). A request's route name (span, access log) carries the one it
// asked for, never any other path segment.
var tenantSubresources = []string{
"members", "documents", "projects", "contracts",
"managed-services", "company-profile", "role-dataset", "store",
}
// tenantSubresource dispatches GET /v1/tenants/{id}/{sub}. Only the members
// collection exists; anything else is a 404 rather than a route that silently
// swallowed a typo.
+20 -1
View File
@@ -11,6 +11,8 @@ import (
"git.breakpilot.com/platform/tenant-registry/internal/telemetry"
"log/slog"
"net/http"
"slices"
"strings"
"time"
"git.breakpilot.com/platform/tenant-registry/internal/authn"
@@ -123,7 +125,7 @@ func NewRouter(s *Server) http.Handler {
// "/v1/tenants/by-slug/members" and neither is the more specific pattern.
// "/v1/tenants/{id}/{sub}" *is* less specific than the by-slug lookup, so
// the two coexist and the lookup keeps winning for its own prefix.
mux.HandleFunc("GET /v1/tenants/{id}/{sub}", s.tenantSubresource)
mux.HandleFunc(tenantSubPattern, s.tenantSubresource)
mux.HandleFunc("POST /v1/tenants/{id}/invites", s.inviteTenantMember)
// Team management (portal#74): role, block, remove/revoke, resend.
mux.HandleFunc("PATCH /v1/tenants/{id}/members/{uid}", s.patchTenantMember)
@@ -237,6 +239,9 @@ func NewRouter(s *Server) http.Handler {
return p
}
_, p := mux.Handler(r)
if p == tenantSubPattern {
return subRoute(r.URL.Path)
}
return p
}
return telemetry.Middleware(route, logRequest(s.Log, route)(root))
@@ -285,3 +290,17 @@ func (s *Server) readyz(w http.ResponseWriter, r *http.Request) {
}
writeJSON(w, http.StatusOK, map[string]string{"status": "ready"})
}
// tenantSubPattern is the wildcard route of tenantSubresource.
const tenantSubPattern = "GET /v1/tenants/{id}/{sub}"
// subRoute names a tenantSubresource request after the collection it asks for
// ("GET /v1/tenants/{id}/store"), so SigNoz can tell them apart. Only known
// collection names make it in; any other segment keeps the wildcard pattern.
func subRoute(path string) string {
parts := strings.Split(strings.TrimPrefix(path, "/"), "/")
if len(parts) == 4 && slices.Contains(tenantSubresources, parts[3]) {
return "GET /v1/tenants/{id}/" + parts[3]
}
return tenantSubPattern
}
+45
View File
@@ -0,0 +1,45 @@
package server
import (
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
)
func TestSubRoute(t *testing.T) {
for _, c := range []struct{ path, want string }{
{"/v1/tenants/0b7e/store", "GET /v1/tenants/{id}/store"},
{"/v1/tenants/0b7e/role-dataset", "GET /v1/tenants/{id}/role-dataset"},
{"/v1/tenants/0b7e/members", "GET /v1/tenants/{id}/members"},
{"/v1/tenants/0b7e/typo", tenantSubPattern}, // unknown: no free text in the name
{"/v1/tenants/0b7e/store/x", tenantSubPattern}, // deeper paths are other routes
{"/v1/tenants/0b7e/", tenantSubPattern},
} {
if got := subRoute(c.path); got != c.want {
t.Errorf("subRoute(%q) = %q, want %q", c.path, got, c.want)
}
}
}
// Every name in tenantSubresources reaches a handler; a name the dispatcher
// does not know answers "no such tenant sub-resource". Keeps list and switch
// in step.
func TestTenantSubresourcesMatchDispatcher(t *testing.T) {
s := &Server{}
for _, name := range append(slices.Clone(tenantSubresources), "no-such-thing") {
r := httptest.NewRequest(http.MethodGet, "/v1/tenants/x/"+name, nil)
r.SetPathValue("id", "x")
r.SetPathValue("sub", name)
w := httptest.NewRecorder()
func() {
defer func() { _ = recover() }() // a real handler may need more of Server than this test sets up
s.tenantSubresource(w, r)
}()
unknown := strings.Contains(w.Body.String(), "no such tenant sub-resource")
if want := name == "no-such-thing"; unknown != want {
t.Errorf("%q: dispatcher says unknown=%v", name, unknown)
}
}
}