forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 12
metric name tenant #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yuchen-db
wants to merge
3
commits into
db_main
Choose a base branch
from
yuchen-db/name-tenant
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
metric name tenant #213
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ import ( | |
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/cespare/xxhash/v2" | ||
| "github.com/go-kit/log" | ||
| "github.com/go-kit/log/level" | ||
| "github.com/gogo/protobuf/proto" | ||
|
|
@@ -117,6 +118,7 @@ type Options struct { | |
| Limiter *Limiter | ||
| AsyncForwardWorkerCount uint | ||
| ReplicationProtocol ReplicationProtocol | ||
| MetricNameShards int | ||
| } | ||
|
|
||
| // Handler serves a Prometheus remote write receiving HTTP endpoint. | ||
|
|
@@ -143,7 +145,9 @@ type Handler struct { | |
| writeTimeseriesError *prometheus.HistogramVec | ||
| writeE2eLatency *prometheus.HistogramVec | ||
|
|
||
| Limiter *Limiter | ||
| Limiter *Limiter | ||
| metricNameShards int | ||
| tenantErrorsTotal *prometheus.CounterVec | ||
| } | ||
|
|
||
| func NewHandler(logger log.Logger, o *Options) *Handler { | ||
|
|
@@ -186,8 +190,9 @@ func NewHandler(logger log.Logger, o *Options) *Handler { | |
| workers, | ||
| o.ReplicationProtocol, | ||
| o.DialOpts...), | ||
| receiverMode: o.ReceiverMode, | ||
| Limiter: o.Limiter, | ||
| receiverMode: o.ReceiverMode, | ||
| Limiter: o.Limiter, | ||
| metricNameShards: o.MetricNameShards, | ||
| forwardRequests: promauto.With(registerer).NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Name: "thanos_receive_forward_requests_total", | ||
|
|
@@ -248,6 +253,14 @@ func NewHandler(logger log.Logger, o *Options) *Handler { | |
| Buckets: []float64{1, 5, 10, 20, 30, 40, 50, 60, 90, 120, 300, 600, 900, 1200, 1800, 3600}, | ||
| }, []string{"code", "tenant", "rollup"}, | ||
| ), | ||
| tenantErrorsTotal: promauto.With(registerer).NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Namespace: "thanos", | ||
| Subsystem: "receive", | ||
| Name: "tenant_modification_errors_total", | ||
| Help: "The number of errors encountered while modifying tenant headers for metric name sharding.", | ||
| }, []string{"reason"}, | ||
| ), | ||
| } | ||
|
|
||
| h.forwardRequests.WithLabelValues(labelSuccess) | ||
|
|
@@ -521,6 +534,40 @@ func isPreAgged(ts prompb.TimeSeries) bool { | |
| return false | ||
| } | ||
|
|
||
| // getMetricName extracts the __name__ label value from a time series. | ||
| func getMetricName(ts *prompb.TimeSeries) string { | ||
| for _, l := range ts.Labels { | ||
| if l.Name == "__name__" { | ||
| return l.Value | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // modifyTenantForMetricSharding modifies the tenant string based on metric name sharding. | ||
| // Returns the modified tenant string and any error encountered. | ||
| func (h *Handler) modifyTenantForMetricSharding(originalTenant string, ts *prompb.TimeSeries, hashringName string) (string, error) { | ||
| if h.metricNameShards <= 0 || h.receiverMode != RouterOnly { | ||
| return originalTenant, nil | ||
| } | ||
|
|
||
| metricName := getMetricName(ts) | ||
| if metricName == "" { | ||
| h.tenantErrorsTotal.WithLabelValues("missing_metric_name").Inc() | ||
| return "", errors.New("time series missing __name__ label") | ||
| } | ||
|
|
||
| // Hash the metric name using xxhash | ||
| hasher := xxhash.New() | ||
| _, _ = hasher.WriteString(metricName) | ||
| hash := hasher.Sum64() | ||
|
|
||
| shard := hash % uint64(h.metricNameShards) | ||
| modifiedTenant := fmt.Sprintf("%s-%d", hashringName, shard) | ||
|
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. What semantics will |
||
|
|
||
| return modifiedTenant, nil | ||
| } | ||
|
|
||
| func (h *Handler) receiveHTTP(w http.ResponseWriter, r *http.Request) { | ||
| var err error | ||
| span, ctx := tracing.StartSpan(r.Context(), "receive_http") | ||
|
|
@@ -921,6 +968,16 @@ func (h *Handler) distributeTimeseriesToReplicas( | |
| } | ||
| } | ||
|
|
||
| // Apply metric name sharding if enabled | ||
| if h.metricNameShards > 0 && h.receiverMode == RouterOnly { | ||
| hashringName := h.hashring.GetHashringName(tenant) | ||
| modifiedTenant, err := h.modifyTenantForMetricSharding(tenant, &ts, hashringName) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| tenant = modifiedTenant | ||
| } | ||
|
|
||
| for _, rn := range replicas { | ||
| endpoint, err := h.hashring.GetN(tenant, &ts, rn) | ||
| if err != nil { | ||
|
|
||
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.
We should use a configmap for the sharding schema instead of a command flag. Thanos Router needs to support the schema defined in the V2 design doc. It has special metric groups besides the number of shards for each metric scope.