|
|
@@ -68,6 +68,9 @@ func (auth *ClientAuth) Apply(req *http.Request) {
|
|
|
// during a retry. This is to prevent starvation on the request threads
|
|
|
const MaxRetryAfterDuration = 10 * time.Second
|
|
|
|
|
|
+// Default header key for Mimir/Cortex-Tenant API requests
|
|
|
+const HeaderXScopeOrgId = "X-Scope-OrgID"
|
|
|
+
|
|
|
// RateLimitRetryOpts contains retry options
|
|
|
type RateLimitRetryOpts struct {
|
|
|
MaxRetries int
|
|
|
@@ -114,14 +117,15 @@ func (rlre *RateLimitedResponseError) Error() string {
|
|
|
// RateLimitedPrometheusClient is a prometheus client which limits the total number of
|
|
|
// concurrent outbound requests allowed at a given moment.
|
|
|
type RateLimitedPrometheusClient struct {
|
|
|
- id string
|
|
|
- client prometheus.Client
|
|
|
- auth *ClientAuth
|
|
|
- queue collections.BlockingQueue[*workRequest]
|
|
|
- decorator QueryParamsDecorator
|
|
|
- rateLimitRetry *RateLimitRetryOpts
|
|
|
- outbound atomic.Int32
|
|
|
- fileLogger *golog.Logger
|
|
|
+ id string
|
|
|
+ client prometheus.Client
|
|
|
+ auth *ClientAuth
|
|
|
+ queue collections.BlockingQueue[*workRequest]
|
|
|
+ decorator QueryParamsDecorator
|
|
|
+ rateLimitRetry *RateLimitRetryOpts
|
|
|
+ outbound atomic.Int32
|
|
|
+ fileLogger *golog.Logger
|
|
|
+ headerXScopeOrgId string
|
|
|
}
|
|
|
|
|
|
// requestCounter is used to determine if the prometheus client keeps track of
|
|
|
@@ -140,7 +144,8 @@ func NewRateLimitedClient(
|
|
|
auth *ClientAuth,
|
|
|
decorator QueryParamsDecorator,
|
|
|
rateLimitRetryOpts *RateLimitRetryOpts,
|
|
|
- queryLogFile string) (prometheus.Client, error) {
|
|
|
+ queryLogFile string,
|
|
|
+ headerXScopeOrgId string) (prometheus.Client, error) {
|
|
|
|
|
|
queue := collections.NewBlockingQueue[*workRequest]()
|
|
|
|
|
|
@@ -169,13 +174,14 @@ func NewRateLimitedClient(
|
|
|
}
|
|
|
|
|
|
rlpc := &RateLimitedPrometheusClient{
|
|
|
- id: id,
|
|
|
- client: client,
|
|
|
- queue: queue,
|
|
|
- decorator: decorator,
|
|
|
- rateLimitRetry: rateLimitRetryOpts,
|
|
|
- auth: auth,
|
|
|
- fileLogger: logger,
|
|
|
+ id: id,
|
|
|
+ client: client,
|
|
|
+ queue: queue,
|
|
|
+ decorator: decorator,
|
|
|
+ rateLimitRetry: rateLimitRetryOpts,
|
|
|
+ auth: auth,
|
|
|
+ fileLogger: logger,
|
|
|
+ headerXScopeOrgId: headerXScopeOrgId,
|
|
|
}
|
|
|
|
|
|
// Start concurrent request processing
|
|
|
@@ -313,6 +319,10 @@ func (rlpc *RateLimitedPrometheusClient) worker() {
|
|
|
|
|
|
// Rate limit and passthrough to prometheus client API
|
|
|
func (rlpc *RateLimitedPrometheusClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
|
|
|
+ if rlpc.headerXScopeOrgId != "" {
|
|
|
+ req.Header.Set(HeaderXScopeOrgId, rlpc.headerXScopeOrgId)
|
|
|
+ }
|
|
|
+
|
|
|
rlpc.auth.Apply(req)
|
|
|
|
|
|
respChan := make(chan *workResponse)
|
|
|
@@ -353,6 +363,7 @@ type PrometheusClientConfig struct {
|
|
|
Auth *ClientAuth
|
|
|
QueryConcurrency int
|
|
|
QueryLogFile string
|
|
|
+ HeaderXScopeOrgId string
|
|
|
}
|
|
|
|
|
|
// NewPrometheusClient creates a new rate limited client which limits by outbound concurrent requests.
|
|
|
@@ -387,6 +398,7 @@ func NewPrometheusClient(address string, config *PrometheusClientConfig) (promet
|
|
|
nil,
|
|
|
config.RateLimitRetryOpts,
|
|
|
config.QueryLogFile,
|
|
|
+ config.HeaderXScopeOrgId,
|
|
|
)
|
|
|
}
|
|
|
|