Files
Carles GarciaandGitHub e1c22c8315 frontend: unify tenant ID extraction across handlers (#5480)
* frontend: unify tenant ID extraction across handlers

There was some inconsistencies and duplication.
Now the tenant extraction must succeed for all endpoints, or else fail.

* Update changelog

* Add test for tenant extraction
2025-08-05 13:54:30 +00:00

39 lines
1.1 KiB
Go

package frontend
import (
"io"
"net/http"
"strings"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/user"
"github.com/grafana/tempo/tempodb/backend"
)
// extractTenant extracts tenant ID from request context and returns HTTP error response if extraction fails
func extractTenant(req *http.Request, logger log.Logger) (string, *http.Response) {
tenant, err := user.ExtractOrgID(req.Context())
if err != nil {
level.Error(logger).Log("msg", "failed to extract tenant id", "err", err)
return "", &http.Response{
StatusCode: http.StatusBadRequest,
Status: http.StatusText(http.StatusBadRequest),
Body: io.NopCloser(strings.NewReader(err.Error())),
}
}
return tenant, nil
}
func rf1FilterFn(rf1After time.Time) func(m *backend.BlockMeta) bool {
return func(m *backend.BlockMeta) bool {
if rf1After.IsZero() {
return m.ReplicationFactor == backend.DefaultReplicationFactor
}
return (m.ReplicationFactor == backend.DefaultReplicationFactor && m.StartTime.Before(rf1After)) ||
(m.ReplicationFactor == backend.MetricsGeneratorReplicationFactor && m.StartTime.After(rf1After))
}
}