This repository was archived by the owner on Jun 19, 2026. It is now read-only.
forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 14
add legacy tenant attribution in router #274
Open
yuchen-db
wants to merge
1
commit into
db_main
Choose a base branch
from
yuchen-db/legacy-tenant-attribution
base: db_main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,7 @@ type Options struct { | |
| Limiter *Limiter | ||
| AsyncForwardWorkerCount uint | ||
| ReplicationProtocol ReplicationProtocol | ||
| TenantAttributor *TenantAttributor | ||
| } | ||
|
|
||
| // Handler serves a Prometheus remote write receiving HTTP endpoint. | ||
|
|
@@ -529,6 +530,9 @@ func (h *Handler) receiveHTTP(w http.ResponseWriter, r *http.Request) { | |
| span.SetTag("receiver.mode", string(h.receiverMode)) | ||
| defer span.Finish() | ||
|
|
||
| // Check if tenant header is present before calling GetTenantFromHTTP | ||
| httpHeaderPresent := r.Header.Get(h.options.TenantHeader) != "" || r.Header.Get(tenancy.DefaultTenantHeader) != "" | ||
|
|
||
| tenantHTTP, err := tenancy.GetTenantFromHTTP(r, h.options.TenantHeader, h.options.DefaultTenantID, h.options.TenantField) | ||
| if err != nil { | ||
| level.Error(h.logger).Log("msg", "error getting tenant from HTTP", "err", err) | ||
|
|
@@ -644,7 +648,7 @@ func (h *Handler) receiveHTTP(w http.ResponseWriter, r *http.Request) { | |
| } | ||
|
|
||
| responseStatusCode := http.StatusOK | ||
| tenantStats, err := h.handleRequest(ctx, rep, tenantHTTP, &wreq) | ||
| tenantStats, err := h.handleRequest(ctx, rep, tenantHTTP, httpHeaderPresent, &wreq) | ||
| if err != nil { | ||
| level.Debug(tLogger).Log("msg", "failed to handle request", "err", err.Error()) | ||
| switch errors.Cause(err) { | ||
|
|
@@ -703,7 +707,7 @@ type requestStats struct { | |
|
|
||
| type tenantRequestStats map[string]requestStats | ||
|
|
||
| func (h *Handler) handleRequest(ctx context.Context, rep uint64, tenantHTTP string, wreq *prompb.WriteRequest) (tenantRequestStats, error) { | ||
| func (h *Handler) handleRequest(ctx context.Context, rep uint64, tenantHTTP string, httpHeaderPresent bool, wreq *prompb.WriteRequest) (tenantRequestStats, error) { | ||
| tLogger := log.With(h.logger, "tenantHTTP", tenantHTTP) | ||
|
|
||
| // This replica value is used to detect cycles in cyclic topologies. | ||
|
|
@@ -732,7 +736,7 @@ func (h *Handler) handleRequest(ctx context.Context, rep uint64, tenantHTTP stri | |
| // Forward any time series as necessary. All time series | ||
| // destined for the local node will be written to the receiver. | ||
| // Time series will be replicated as necessary. | ||
| return h.forward(ctx, tenantHTTP, r, wreq) | ||
| return h.forward(ctx, tenantHTTP, httpHeaderPresent, r, wreq) | ||
| } | ||
|
|
||
| // forward accepts a write request, batches its time series by | ||
|
|
@@ -743,7 +747,7 @@ func (h *Handler) handleRequest(ctx context.Context, rep uint64, tenantHTTP stri | |
| // unless the request needs to be replicated. | ||
| // The function only returns when all requests have finished | ||
| // or the context is canceled. | ||
| func (h *Handler) forward(ctx context.Context, tenantHTTP string, r replica, wreq *prompb.WriteRequest) (tenantRequestStats, error) { | ||
| func (h *Handler) forward(ctx context.Context, tenantHTTP string, httpHeaderPresent bool, r replica, wreq *prompb.WriteRequest) (tenantRequestStats, error) { | ||
| span, ctx := tracing.StartSpan(ctx, "receive_fanout_forward") | ||
| defer span.Finish() | ||
|
|
||
|
|
@@ -761,6 +765,7 @@ func (h *Handler) forward(ctx context.Context, tenantHTTP string, r replica, wre | |
| writeRequest: wreq, | ||
| replicas: replicas, | ||
| alreadyReplicated: r.replicated, | ||
| httpHeaderPresent: httpHeaderPresent, | ||
| } | ||
|
|
||
| return h.fanoutForward(ctx, params) | ||
|
|
@@ -771,6 +776,7 @@ type remoteWriteParams struct { | |
| writeRequest *prompb.WriteRequest | ||
| replicas []uint64 | ||
| alreadyReplicated bool | ||
| httpHeaderPresent bool // true if tenant came from HTTP header (not default) | ||
| } | ||
|
|
||
| func (h *Handler) gatherWriteStats(rf int, writes ...map[endpointReplica]map[string]trackedSeries) tenantRequestStats { | ||
|
|
@@ -831,7 +837,7 @@ func (h *Handler) fanoutForward(ctx context.Context, params remoteWriteParams) ( | |
| } | ||
| requestLogger := log.With(h.logger, logTags...) | ||
|
|
||
| localWrites, remoteWrites, err := h.distributeTimeseriesToReplicas(params.tenant, params.replicas, params.writeRequest.Timeseries) | ||
| localWrites, remoteWrites, err := h.distributeTimeseriesToReplicas(params.tenant, params.httpHeaderPresent, params.replicas, params.writeRequest.Timeseries) | ||
| if err != nil { | ||
| level.Error(requestLogger).Log("msg", "failed to distribute timeseries to replicas", "err", err) | ||
| return stats, err | ||
|
|
@@ -914,6 +920,7 @@ func (h *Handler) fanoutForward(ctx context.Context, params remoteWriteParams) ( | |
| // series that should be written to remote nodes. | ||
| func (h *Handler) distributeTimeseriesToReplicas( | ||
| tenantHTTP string, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wonder if we can use |
||
| httpHeaderPresent bool, | ||
| replicas []uint64, | ||
| timeseries []prompb.TimeSeries, | ||
| ) (map[endpointReplica]map[string]trackedSeries, map[endpointReplica]map[string]trackedSeries, error) { | ||
|
|
@@ -935,6 +942,25 @@ func (h *Handler) distributeTimeseriesToReplicas( | |
| } | ||
| } | ||
|
|
||
| // Tenant attribution logic | ||
| if h.options.TenantAttributor != nil { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also note this is exclusive to |
||
| lbls := labelpb.ZLabelsToPromLabels(ts.Labels) | ||
| attributedTenant := h.options.TenantAttributor.GetTenantFromLabels(lbls) | ||
|
|
||
| if h.options.TenantAttributor.IsVerifyMode() { | ||
| // VERIFICATION MODE: compute attribution for metrics only | ||
| // Compare attributed tenant with HTTP tenant | ||
| h.options.TenantAttributor.RecordVerification(attributedTenant, tenantHTTP) | ||
| // DO NOT modify tenant - keep using HTTP tenant for routing | ||
| } else { | ||
| // ATTRIBUTION MODE: only attribute if no HTTP header was present | ||
| if !httpHeaderPresent { | ||
| tenant = attributedTenant | ||
| } | ||
| // If HTTP header present, use that (tenant already set correctly) | ||
| } | ||
| } | ||
|
|
||
| for _, rn := range replicas { | ||
| endpoint, err := h.hashring.GetN(tenant, &ts, rn) | ||
| if err != nil { | ||
|
|
@@ -1125,7 +1151,8 @@ func (h *Handler) RemoteWrite(ctx context.Context, r *storepb.WriteRequest) (*st | |
| span, ctx := tracing.StartSpan(ctx, "receive_grpc") | ||
| defer span.Finish() | ||
|
|
||
| _, err := h.handleRequest(ctx, uint64(r.Replica), r.Tenant, &prompb.WriteRequest{Timeseries: r.Timeseries}) | ||
| // For gRPC requests, tenant is explicitly provided, treat as if HTTP header was present | ||
| _, err := h.handleRequest(ctx, uint64(r.Replica), r.Tenant, true, &prompb.WriteRequest{Timeseries: r.Timeseries}) | ||
| if err != nil { | ||
| level.Debug(h.logger).Log("msg", "failed to handle request", "err", err) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why introduce new variable, can
tenantHTTP stringtell if a header is present? I think if it isn't, it will be empty?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is obtained in
tenantHTTP, err := tenancy.GetTenantFromHTTP(r, h.options.TenantHeader, h.options.DefaultTenantID, h.options.TenantField)a few places might be broken for example,
rate limit logic: if !requestLimiter.AllowSizeBytes(tenantHTTP, int64(len(reqBuf))) {
e2e latency calculation: h.writeE2eLatency.WithLabelValues(strconv.Itoa(responseStatusCode), tenantHTTP, strconv.FormatBool(isPreAgged)).Observe(lat)
with new tenant attribution, we might need to fix how those work too, (especially for rate limit which is request scoped, it might be not possible to reject 1 remote write directly anymore)