Skip to content

Commit

Permalink
Upgrade Cortex into Thanos (#3432)
Browse files Browse the repository at this point in the history
* Upgrade Cortex into Thanos

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Removed unused CortexFrontendConfig

Signed-off-by: Marco Pracucci <marco@pracucci.com>
  • Loading branch information
pracucci authored Nov 12, 2020
1 parent 5035096 commit 82e7da7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
33 changes: 25 additions & 8 deletions cmd/thanos/query_frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"time"

"github.com/NYTimes/gziphandler"
cortexfrontend "github.com/cortexproject/cortex/pkg/querier/frontend"
cortexfrontend "github.com/cortexproject/cortex/pkg/frontend"
"github.com/cortexproject/cortex/pkg/frontend/transport"
cortexfrontendv1 "github.com/cortexproject/cortex/pkg/frontend/v1"
"github.com/cortexproject/cortex/pkg/querier/queryrange"
cortexvalidation "github.com/cortexproject/cortex/pkg/util/validation"
"github.com/go-kit/kit/log"
Expand Down Expand Up @@ -45,7 +47,9 @@ func registerQueryFrontend(app *extkingpin.App) {
cfg := &queryFrontendConfig{
Config: queryfrontend.Config{
// Max body size is 10 MiB.
CortexFrontendConfig: &cortexfrontend.Config{MaxBodySize: 10 * 1024 * 1024},
CortexHandlerConfig: &transport.HandlerConfig{
MaxBodySize: 10 * 1024 * 1024,
},
QueryRangeConfig: queryfrontend.QueryRangeConfig{
Limits: &cortexvalidation.Limits{},
ResultsCacheConfig: &queryrange.ResultsCacheConfig{},
Expand Down Expand Up @@ -108,13 +112,13 @@ func registerQueryFrontend(app *extkingpin.App) {
Default("").StringVar(&cfg.CacheCompression)

cmd.Flag("query-frontend.downstream-url", "URL of downstream Prometheus Query compatible API.").
Default("http://localhost:9090").StringVar(&cfg.CortexFrontendConfig.DownstreamURL)
Default("http://localhost:9090").StringVar(&cfg.DownstreamURL)

cmd.Flag("query-frontend.compress-responses", "Compress HTTP responses.").
Default("false").BoolVar(&cfg.CortexFrontendConfig.CompressResponses)
Default("false").BoolVar(&cfg.CompressResponses)

cmd.Flag("query-frontend.log-queries-longer-than", "Log queries that are slower than the specified duration. "+
"Set to 0 to disable. Set to < 0 to enable on all queries.").Default("0").DurationVar(&cfg.CortexFrontendConfig.LogQueriesLongerThan)
"Set to 0 to disable. Set to < 0 to enable on all queries.").Default("0").DurationVar(&cfg.CortexHandlerConfig.LogQueriesLongerThan)

cmd.Flag("query-frontend.org-id-header", "Request header names used to identify the source of slow queries (repeated flag). "+
"The values of the header will be added to the org id field in the slow query log. "+
Expand Down Expand Up @@ -170,7 +174,7 @@ func runQueryFrontend(
return errors.Wrap(err, "error validating the config")
}

fe, err := cortexfrontend.New(*cfg.CortexFrontendConfig, nil, logger, reg)
fe, err := cortexfrontendv1.New(cortexfrontendv1.Config{}, nil, logger, reg)
if err != nil {
return errors.Wrap(err, "setup query frontend")
}
Expand All @@ -181,7 +185,20 @@ func runQueryFrontend(
return errors.Wrap(err, "setup tripperwares")
}

fe.Wrap(tripperWare)
// Create a downstream roundtripper.
roundTripper, err := cortexfrontend.NewDownstreamRoundTripper(cfg.DownstreamURL)
if err != nil {
return errors.Wrap(err, "setup downstream roundtripper")
}

// Wrap the downstream RoundTripper into query frontend Tripperware.
roundTripper = tripperWare(roundTripper)

// Create the query frontend transport.
handler := transport.NewHandler(*cfg.CortexHandlerConfig, roundTripper, logger)
if cfg.CompressResponses {
handler = gziphandler.GzipHandler(handler)
}

httpProbe := prober.NewHTTP()
statusProber := prober.Combine(
Expand Down Expand Up @@ -224,7 +241,7 @@ func runQueryFrontend(
})
return hf
}
srv.Handle("/", instr(fe.Handler().ServeHTTP))
srv.Handle("/", instr(handler.ServeHTTP))

g.Add(func() error {
statusProber.Healthy()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/cespare/xxhash v1.1.0
github.com/chromedp/cdproto v0.0.0-20200424080200-0de008e41fa0
github.com/chromedp/chromedp v0.5.3
github.com/cortexproject/cortex v1.4.1-0.20201030080541-83ad6df2abea
github.com/cortexproject/cortex v1.5.1-0.20201111110551-ba512881b076
github.com/davecgh/go-spew v1.1.1
github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb
github.com/fatih/structtag v1.1.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ github.com/cortexproject/cortex v1.2.1-0.20200805064754-d8edc95e2c91/go.mod h1:P
github.com/cortexproject/cortex v1.3.1-0.20200923145333-8587ea61fe17/go.mod h1:dJ9gpW7dzQ7z09cKtNN9PfebumgyO4dtNdFQ6eQEed0=
github.com/cortexproject/cortex v1.4.1-0.20201030080541-83ad6df2abea h1:AT8j9cojU1ena5hSyjdgAShH4Ut3JY7HLMKurR7RHm4=
github.com/cortexproject/cortex v1.4.1-0.20201030080541-83ad6df2abea/go.mod h1:kXo5F3jlF7Ky3+I31jt/bXTzOlQjl2X/vGDpy0RY1gU=
github.com/cortexproject/cortex v1.5.1-0.20201111110551-ba512881b076 h1:SxwQL+Tdo2RlSOZUJVvR5jXuprDRglABtjbHnNA8y9M=
github.com/cortexproject/cortex v1.5.1-0.20201111110551-ba512881b076/go.mod h1:zFBGVsvRBfVp6ARXZ7pmiLaGlbjda5ZnA4Y6qSJyrQg=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/cznic/b v0.0.0-20180115125044-35e9bbe41f07/go.mod h1:URriBxXwVq5ijiJ12C7iIZqlA69nTlI+LgI6/pwftG8=
Expand Down Expand Up @@ -1065,6 +1067,7 @@ github.com/thanos-io/thanos v0.8.1-0.20200109203923-552ffa4c1a0d/go.mod h1:usT/T
github.com/thanos-io/thanos v0.13.1-0.20200731083140-69b87607decf/go.mod h1:G8caR6G7pSDreRDvFm9wFuyjEBztmr8Ag3kBYpa/fEc=
github.com/thanos-io/thanos v0.13.1-0.20200807203500-9b578afb4763/go.mod h1:KyW0a93tsh7v4hXAwo2CVAIRYuZT1Kkf4e04gisQjAg=
github.com/thanos-io/thanos v0.13.1-0.20201019130456-f41940581d9a/go.mod h1:A3qUEEbsVkplJnxyDLwuIuvTDaJPByTH+hMdTl9ujAA=
github.com/thanos-io/thanos v0.13.1-0.20201030101306-47f9a225cc52/go.mod h1:OqqX4x21cg5N5MMHd/yGQAc/V3wg8a7Do4Jk8HfaFZQ=
github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab h1:7ZR3hmisBWw77ZpO1/o86g+JV3VKlk3d48jopJxzTjU=
github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab/go.mod h1:eheTFp954zcWZXCU8d0AT76ftsQOTo4DTqkN/h3k1MY=
github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
Expand Down
8 changes: 5 additions & 3 deletions pkg/queryfrontend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"time"

cortexcache "github.com/cortexproject/cortex/pkg/chunk/cache"
"github.com/cortexproject/cortex/pkg/frontend/transport"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/thanos-io/thanos/pkg/cacheutil"
"github.com/thanos-io/thanos/pkg/extflag"
"gopkg.in/yaml.v2"

cortexfrontend "github.com/cortexproject/cortex/pkg/querier/frontend"
"github.com/cortexproject/cortex/pkg/querier/queryrange"
cortexvalidation "github.com/cortexproject/cortex/pkg/util/validation"
"github.com/pkg/errors"
Expand Down Expand Up @@ -145,9 +145,11 @@ type Config struct {
QueryRangeConfig
LabelsConfig

CortexFrontendConfig *cortexfrontend.Config
CortexHandlerConfig *transport.HandlerConfig
CompressResponses bool
CacheCompression string
RequestLoggingDecision string
DownstreamURL string
}

// QueryRangeConfig holds the config for query range tripperware.
Expand Down Expand Up @@ -205,7 +207,7 @@ func (cfg *Config) Validate() error {
return errors.New("labels.default-time-range cannot be set to 0")
}

if len(cfg.CortexFrontendConfig.DownstreamURL) == 0 {
if len(cfg.DownstreamURL) == 0 {
return errors.New("downstream URL should be configured")
}

Expand Down
13 changes: 6 additions & 7 deletions pkg/queryfrontend/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/cortexproject/cortex/pkg/querier/frontend"
"github.com/cortexproject/cortex/pkg/querier/queryrange"
"github.com/cortexproject/cortex/pkg/util/validation"

Expand All @@ -29,7 +28,7 @@ const (
)

// NewTripperware returns a Tripperware which sends requests to different sub tripperwares based on the query type.
func NewTripperware(config Config, reg prometheus.Registerer, logger log.Logger) (frontend.Tripperware, error) {
func NewTripperware(config Config, reg prometheus.Registerer, logger log.Logger) (queryrange.Tripperware, error) {
var (
queryRangeLimits, labelsLimits queryrange.Limits
err error
Expand Down Expand Up @@ -139,8 +138,8 @@ func newQueryRangeTripperware(
codec *queryRangeCodec,
reg prometheus.Registerer,
logger log.Logger,
) (frontend.Tripperware, error) {
queryRangeMiddleware := []queryrange.Middleware{queryrange.LimitsMiddleware(limits)}
) (queryrange.Tripperware, error) {
queryRangeMiddleware := []queryrange.Middleware{queryrange.NewLimitsMiddleware(limits)}
m := queryrange.NewInstrumentMiddlewareMetrics(reg)

// step align middleware.
Expand Down Expand Up @@ -197,7 +196,7 @@ func newQueryRangeTripperware(

return func(next http.RoundTripper) http.RoundTripper {
rt := queryrange.NewRoundTripper(next, codec, queryRangeMiddleware...)
return frontend.RoundTripFunc(func(r *http.Request) (*http.Response, error) {
return queryrange.RoundTripFunc(func(r *http.Request) (*http.Response, error) {
return rt.RoundTrip(r)
})
}, nil
Expand All @@ -211,7 +210,7 @@ func newLabelsTripperware(
codec *labelsCodec,
reg prometheus.Registerer,
logger log.Logger,
) (frontend.Tripperware, error) {
) (queryrange.Tripperware, error) {
labelsMiddleware := []queryrange.Middleware{}
m := queryrange.NewInstrumentMiddlewareMetrics(reg)

Expand Down Expand Up @@ -259,7 +258,7 @@ func newLabelsTripperware(
}
return func(next http.RoundTripper) http.RoundTripper {
rt := queryrange.NewRoundTripper(next, codec, labelsMiddleware...)
return frontend.RoundTripFunc(func(r *http.Request) (*http.Response, error) {
return queryrange.RoundTripFunc(func(r *http.Request) (*http.Response, error) {
return rt.RoundTrip(r)
})
}, nil
Expand Down

0 comments on commit 82e7da7

Please sign in to comment.