-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add thanos query frontend sub command
Signed-off-by: Ben Ye <yb532204897@gmail.com>
- Loading branch information
Showing
14 changed files
with
487 additions
and
97 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cortexproject/cortex/pkg/querier/frontend" | ||
"github.com/cortexproject/cortex/pkg/querier/queryrange" | ||
"github.com/go-kit/kit/log" | ||
"github.com/oklog/run" | ||
"github.com/opentracing/opentracing-go" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/route" | ||
"gopkg.in/alecthomas/kingpin.v2" | ||
|
||
v1 "github.com/thanos-io/thanos/pkg/api/queryfrontend" | ||
"github.com/thanos-io/thanos/pkg/component" | ||
"github.com/thanos-io/thanos/pkg/extprom" | ||
"github.com/thanos-io/thanos/pkg/prober" | ||
"github.com/thanos-io/thanos/pkg/queryfrontend" | ||
httpserver "github.com/thanos-io/thanos/pkg/server/http" | ||
) | ||
|
||
type queryFrontendConfig struct { | ||
http httpConfig | ||
queryRangeConfig queryRangeConfig | ||
|
||
// TODO(yeya24): use this after promql engine is added | ||
defaultEvaluationInterval time.Duration | ||
downstreamURL string | ||
compressResponses bool | ||
} | ||
|
||
type queryRangeConfig struct { | ||
respCacheConfig responseCacheConfig | ||
cacheResults bool | ||
splitInterval time.Duration | ||
disableStepAlign bool | ||
maxRetries int | ||
maxQueryParallelism int | ||
maxQueryLength time.Duration | ||
} | ||
|
||
type responseCacheConfig struct { | ||
cacheMaxFreshness time.Duration | ||
} | ||
|
||
func (c *responseCacheConfig) registerFlag(cmd *kingpin.CmdClause) { | ||
cmd.Flag("query-range.response-cache-max-freshness", "Most recent allowed cacheable result per-tenant, to prevent caching very recent results that might still be in flux."). | ||
Default("1m").DurationVar(&c.cacheMaxFreshness) | ||
} | ||
|
||
func (c *queryRangeConfig) registerFlag(cmd *kingpin.CmdClause) { | ||
c.respCacheConfig.registerFlag(cmd) | ||
|
||
cmd.Flag("query-range.cache-results", "Cache query range results.").Default("false"). | ||
BoolVar(&c.cacheResults) | ||
|
||
cmd.Flag("query-range.split-interval", "Split queries by an interval and execute in parallel, 0 disables it."). | ||
Default("24h").DurationVar(&c.splitInterval) | ||
|
||
cmd.Flag("query-range.max-retries-per-request", "Maximum number of retries for a single request; beyond this, the downstream error is returned."). | ||
Default("5").IntVar(&c.maxRetries) | ||
|
||
cmd.Flag("query-range.disable-step-align", "Disable aligning incoming queries' start and end with their step."). | ||
Default("true").BoolVar(&c.disableStepAlign) | ||
} | ||
|
||
func (c *queryFrontendConfig) registerFlag(cmd *kingpin.CmdClause) { | ||
c.queryRangeConfig.registerFlag(cmd) | ||
c.http.registerFlag(cmd) | ||
|
||
cmd.Flag("query.default-evaluation-interval", "Set default evaluation interval for sub queries."). | ||
Default("1m").DurationVar(&c.defaultEvaluationInterval) | ||
|
||
cmd.Flag("query-frontend.downstream-url", "URL of downstream Prometheus Query compatible API."). | ||
Default("http://localhost:9090").StringVar(&c.downstreamURL) | ||
|
||
cmd.Flag("query-frontend.compress-responses", "Compress HTTP responses."). | ||
Default("false").BoolVar(&c.compressResponses) | ||
} | ||
|
||
func registerQueryFrontend(m map[string]setupFunc, app *kingpin.Application) { | ||
comp := component.QueryFrontend | ||
cmd := app.Command(comp.String(), "query frontend") | ||
conf := &queryFrontendConfig{} | ||
conf.registerFlag(cmd) | ||
|
||
m[comp.String()] = func(g *run.Group, logger log.Logger, reg *prometheus.Registry, _ opentracing.Tracer, _ <-chan struct{}, _ bool) error { | ||
|
||
return runQueryFrontend( | ||
g, | ||
logger, | ||
reg, | ||
conf, | ||
comp, | ||
) | ||
} | ||
} | ||
|
||
func runQueryFrontend( | ||
g *run.Group, | ||
logger log.Logger, | ||
reg *prometheus.Registry, | ||
conf *queryFrontendConfig, | ||
comp component.Component, | ||
) error { | ||
|
||
fe, err := frontend.New(frontend.Config{ | ||
DownstreamURL: conf.downstreamURL, | ||
CompressResponses: conf.compressResponses, | ||
}, logger, reg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
limiter := queryfrontend.NewLimiter( | ||
conf.queryRangeConfig.maxQueryParallelism, | ||
conf.queryRangeConfig.maxQueryLength, | ||
conf.queryRangeConfig.respCacheConfig.cacheMaxFreshness, | ||
) | ||
|
||
tripperWare, err := queryfrontend.NewTripperWare( | ||
limiter, | ||
queryrange.PrometheusCodec, | ||
queryrange.PrometheusResponseExtractor{}, | ||
conf.queryRangeConfig.disableStepAlign, | ||
conf.queryRangeConfig.cacheResults, | ||
conf.queryRangeConfig.splitInterval, | ||
conf.queryRangeConfig.maxRetries, | ||
reg, | ||
logger, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fe.Wrap(tripperWare) | ||
|
||
httpProbe := prober.NewHTTP() | ||
statusProber := prober.Combine( | ||
httpProbe, | ||
prober.NewInstrumentation(comp, logger, extprom.WrapRegistererWithPrefix("thanos_", reg)), | ||
) | ||
|
||
// Start metrics HTTP server. | ||
{ | ||
router := route.New() | ||
|
||
api := v1.NewAPI(logger) | ||
api.Register(router.WithPrefix("/api/v1"), fe.Handler().ServeHTTP) | ||
|
||
srv := httpserver.New(logger, reg, comp, httpProbe, | ||
httpserver.WithListen(conf.http.bindAddress), | ||
httpserver.WithGracePeriod(time.Duration(conf.http.gracePeriod)), | ||
) | ||
srv.Handle("/", router) | ||
|
||
g.Add(func() error { | ||
statusProber.Healthy() | ||
|
||
return srv.ListenAndServe() | ||
}, func(err error) { | ||
statusProber.NotReady(err) | ||
defer statusProber.NotHealthy(err) | ||
|
||
srv.Shutdown(err) | ||
}) | ||
} | ||
|
||
return nil | ||
} |
This file contains 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 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 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.