-
Notifications
You must be signed in to change notification settings - Fork 820
Introduce query-tee tool #2203
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
Merged
Merged
Introduce query-tee tool #2203
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
34ccbc5
Experimental query-tee tool
pracucci dd7fcc3
Moved query-tee to cmd
pracucci ff14e44
Massive refactoring in the query-tee
pracucci 4190eae
Changed route name in metrics for /api/prom/api/v1/label/{name}/value…
pracucci 68f309c
Prepend endpoint path to original request path in order to allow for …
pracucci 0fbb53f
Refactored proxy backend response code to have a more convenient way …
pracucci d4a2495
Keep keep-alive enabled for connections between the proxy and backends
pracucci eb81d80
Join URL paths using path.Join()
pracucci 99f3a49
Fixed linter
pracucci c8eb4f8
Lowered the hard requirement to have at least 1 backend
pracucci 81086d4
Added query-tee tests
pracucci 4a6dc44
Fixed linter
pracucci eed362b
Added CHANGELOG entry
pracucci a089982
Documented query-tee
pracucci 0b19c53
Tested ProxyBackend auth
pracucci 924445b
Added a query-tee test covering backends with different path prefixes
pracucci c86fd4e
Fixed linter
pracucci 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
cmd/test-exporter/test-exporter | ||
cmd/cortex/cortex | ||
cmd/query-tee/query-tee | ||
.uptodate | ||
.pkg | ||
.cache | ||
|
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FROM alpine:3.11 | ||
RUN apk add --no-cache ca-certificates | ||
COPY query-tee / | ||
ENTRYPOINT ["/query-tee"] | ||
|
||
ARG revision | ||
LABEL org.opencontainers.image.title="query-tee" \ | ||
org.opencontainers.image.source="https://github.com/cortexproject/cortex/tree/master/tools/query-tee" \ | ||
org.opencontainers.image.revision="${revision}" |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
|
||
"github.com/go-kit/kit/log/level" | ||
"github.com/gorilla/mux" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
|
||
"github.com/cortexproject/cortex/pkg/util" | ||
) | ||
|
||
type InstrumentationServer struct { | ||
port int | ||
registry *prometheus.Registry | ||
srv *http.Server | ||
} | ||
|
||
// NewInstrumentationServer returns a server exposing Prometheus metrics. | ||
func NewInstrumentationServer(port int, registry *prometheus.Registry) *InstrumentationServer { | ||
return &InstrumentationServer{ | ||
port: port, | ||
registry: registry, | ||
} | ||
} | ||
|
||
// Start the instrumentation server. | ||
func (s *InstrumentationServer) Start() error { | ||
// Setup listener first, so we can fail early if the port is in use. | ||
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", s.port)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
router := mux.NewRouter() | ||
router.Handle("/metrics", promhttp.HandlerFor(s.registry, promhttp.HandlerOpts{})) | ||
|
||
s.srv = &http.Server{ | ||
Handler: router, | ||
} | ||
|
||
go func() { | ||
if err := s.srv.Serve(listener); err != nil { | ||
level.Error(util.Logger).Log("msg", "metrics server terminated", "err", err) | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
// Stop closes the instrumentation server. | ||
func (s *InstrumentationServer) Stop() { | ||
if s.srv != nil { | ||
s.srv.Close() | ||
s.srv = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"time" | ||
|
||
"github.com/go-kit/kit/log/level" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/weaveworks/common/logging" | ||
"github.com/weaveworks/common/server" | ||
|
||
"github.com/cortexproject/cortex/pkg/util" | ||
) | ||
|
||
type Config struct { | ||
ServerServicePort int | ||
ServerMetricsPort int | ||
BackendEndpoints string | ||
PreferredBackend string | ||
BackendReadTimeout time.Duration | ||
LogLevel logging.Level | ||
} | ||
|
||
func main() { | ||
// Parse CLI flags. | ||
cfg := Config{} | ||
flag.IntVar(&cfg.ServerServicePort, "server.service-port", 80, "The port where the query-tee service listens to.") | ||
flag.IntVar(&cfg.ServerMetricsPort, "server.metrics-port", 9900, "The port where metrics are exposed.") | ||
flag.StringVar(&cfg.BackendEndpoints, "backend.endpoints", "", "Comma separated list of backend endpoints to query.") | ||
pstibrany marked this conversation as resolved.
Show resolved
Hide resolved
|
||
flag.StringVar(&cfg.PreferredBackend, "backend.preferred", "", "The hostname of the preferred backend when selecting the response to send back to the client.") | ||
flag.DurationVar(&cfg.BackendReadTimeout, "backend.read-timeout", 90*time.Second, "The timeout when reading the response from a backend.") | ||
cfg.LogLevel.RegisterFlags(flag.CommandLine) | ||
flag.Parse() | ||
|
||
util.InitLogger(&server.Config{ | ||
LogLevel: cfg.LogLevel, | ||
}) | ||
|
||
// Run the instrumentation server. | ||
registry := prometheus.NewRegistry() | ||
registry.MustRegister(prometheus.NewGoCollector()) | ||
|
||
i := NewInstrumentationServer(cfg.ServerMetricsPort, registry) | ||
if err := i.Start(); err != nil { | ||
level.Error(util.Logger).Log("msg", "Unable to start instrumentation server", "err", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
// Run the proxy. | ||
proxy, err := NewProxy(cfg, util.Logger, registry) | ||
if err != nil { | ||
level.Error(util.Logger).Log("msg", "Unable to initialize the proxy", "err", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
if err := proxy.Start(); err != nil { | ||
level.Error(util.Logger).Log("msg", "Unable to start the proxy", "err", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
proxy.Await() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
"github.com/gorilla/mux" | ||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var ( | ||
errMinBackends = errors.New("at least 1 backend is required") | ||
) | ||
|
||
type Proxy struct { | ||
cfg Config | ||
backends []*ProxyBackend | ||
logger log.Logger | ||
metrics *ProxyMetrics | ||
|
||
// The HTTP server used to run the proxy service. | ||
srv *http.Server | ||
srvListener net.Listener | ||
|
||
// Wait group used to wait until the server has done. | ||
done sync.WaitGroup | ||
} | ||
|
||
func NewProxy(cfg Config, logger log.Logger, registerer prometheus.Registerer) (*Proxy, error) { | ||
p := &Proxy{ | ||
cfg: cfg, | ||
logger: logger, | ||
metrics: NewProxyMetrics(registerer), | ||
} | ||
|
||
// Parse the backend endpoints (comma separated). | ||
parts := strings.Split(cfg.BackendEndpoints, ",") | ||
|
||
for idx, part := range parts { | ||
// Skip empty ones. | ||
part = strings.TrimSpace(part) | ||
if part == "" { | ||
continue | ||
} | ||
|
||
u, err := url.Parse(part) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "invalid backend endpoint %s", part) | ||
} | ||
|
||
// The backend name is hardcoded as the backend hostname. | ||
name := u.Hostname() | ||
preferred := name == cfg.PreferredBackend | ||
|
||
// In tests we have the same hostname for all backends, so we also | ||
// support a numeric preferred backend which is the index in the list | ||
// of backends. | ||
if preferredIdx, err := strconv.Atoi(cfg.PreferredBackend); err == nil { | ||
preferred = preferredIdx == idx | ||
} | ||
|
||
p.backends = append(p.backends, NewProxyBackend(name, u, cfg.BackendReadTimeout, preferred)) | ||
} | ||
|
||
// At least 1 backend is required | ||
if len(p.backends) < 1 { | ||
return nil, errMinBackends | ||
} | ||
|
||
// At least 2 backends are suggested | ||
if len(p.backends) < 2 { | ||
pstibrany marked this conversation as resolved.
Show resolved
Hide resolved
|
||
level.Warn(p.logger).Log("msg", "The proxy is running with only 1 backend. At least 2 backends are required to fulfil the purpose of the proxy and compare results.") | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
func (p *Proxy) Start() error { | ||
// Setup listener first, so we can fail early if the port is in use. | ||
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", p.cfg.ServerServicePort)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Read endpoints. | ||
router := mux.NewRouter() | ||
router.Path("/api/v1/query").Methods("GET").Handler(NewProxyEndpoint(p.backends, "api_v1_query", p.metrics, p.logger)) | ||
router.Path("/api/v1/query_range").Methods("GET").Handler(NewProxyEndpoint(p.backends, "api_v1_query_range", p.metrics, p.logger)) | ||
router.Path("/api/v1/labels").Methods("GET").Handler(NewProxyEndpoint(p.backends, "api_v1_labels", p.metrics, p.logger)) | ||
router.Path("/api/v1/label/{name}/values").Methods("GET").Handler(NewProxyEndpoint(p.backends, "api_v1_label_name_values", p.metrics, p.logger)) | ||
router.Path("/api/v1/series").Methods("GET").Handler(NewProxyEndpoint(p.backends, "api_v1_series", p.metrics, p.logger)) | ||
|
||
p.srvListener = listener | ||
p.srv = &http.Server{ | ||
ReadTimeout: 1 * time.Minute, | ||
WriteTimeout: 2 * time.Minute, | ||
Handler: router, | ||
} | ||
|
||
// Run in a dedicated goroutine. | ||
p.done.Add(1) | ||
go func() { | ||
defer p.done.Done() | ||
|
||
if err := p.srv.Serve(p.srvListener); err != nil { | ||
level.Error(p.logger).Log("msg", "Proxy server failed", "err", err) | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func (p *Proxy) Stop() error { | ||
if p.srv == nil { | ||
return nil | ||
} | ||
|
||
return p.srv.Shutdown(context.Background()) | ||
} | ||
|
||
func (p *Proxy) Await() { | ||
// Wait until terminated. | ||
p.done.Wait() | ||
} | ||
|
||
func (p *Proxy) Endpoint() string { | ||
if p.srvListener == nil { | ||
return "" | ||
} | ||
|
||
return p.srvListener.Addr().String() | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.