Skip to content

Commit 484b299

Browse files
committed
Allow the query-frontend code to proxy queries to 'vanilla' Prometheus.
Signed-off-by: Tom Wilkie <[email protected]>
1 parent d548ff6 commit 484b299

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

pkg/querier/frontend/frontend.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"io/ioutil"
99
"math/rand"
1010
"net/http"
11+
"net/url"
12+
"path"
1113
"sync"
1214
"time"
1315

@@ -57,6 +59,7 @@ type Config struct {
5759
CacheResults bool `yaml:"cache_results"`
5860
CompressResponses bool `yaml:"compress_responses"`
5961
ResultsCacheConfig `yaml:"results_cache"`
62+
DownstreamURL string `yaml:"downstream"`
6063
}
6164

6265
// RegisterFlags adds the flags required to config this to the given FlagSet.
@@ -68,6 +71,7 @@ func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
6871
f.BoolVar(&cfg.CacheResults, "querier.cache-results", false, "Cache query results.")
6972
f.BoolVar(&cfg.CompressResponses, "querier.compress-http-responses", false, "Compress HTTP responses.")
7073
cfg.ResultsCacheConfig.RegisterFlags(f)
74+
f.StringVar(&cfg.DownstreamURL, "querier.downstream-url", "", "URL of downstream Prometheus.")
7175
}
7276

7377
// Frontend queues HTTP requests, dispatches them to backends, and handles retries
@@ -99,6 +103,7 @@ func New(cfg Config, log log.Logger, limits *validation.Overrides) (*Frontend, e
99103
log: log,
100104
queues: map[string]chan *request{},
101105
}
106+
f.cond = sync.NewCond(&f.mtx)
102107

103108
// Stack up the pipeline of various query range middlewares.
104109
queryRangeMiddleware := []queryRangeMiddleware{}
@@ -116,8 +121,24 @@ func New(cfg Config, log log.Logger, limits *validation.Overrides) (*Frontend, e
116121
queryRangeMiddleware = append(queryRangeMiddleware, instrument("results_cache"), queryCacheMiddleware)
117122
}
118123

119-
// Finally, if the user selected any query range middleware, stitch it in.
124+
// If the user has specified a downstream Prometheus, then we should
125+
// forward requests to that. Otherwise we will wait for queries to
126+
// contact us.
120127
var roundTripper http.RoundTripper = f
128+
if cfg.DownstreamURL != "" {
129+
u, err := url.Parse(cfg.DownstreamURL)
130+
if err != nil {
131+
return nil, err
132+
}
133+
134+
roundTripper = RoundTripFunc(func(r *http.Request) (*http.Response, error) {
135+
r.URL.Host = u.Host
136+
r.URL.Path = path.Join(u.Path, r.URL.Path)
137+
return http.DefaultTransport.RoundTrip(r)
138+
})
139+
}
140+
141+
// Finally, if the user selected any query range middleware, stitch it in.
121142
if len(queryRangeMiddleware) > 0 {
122143
roundTripper = &queryRangeRoundTripper{
123144
next: roundTripper,
@@ -128,10 +149,17 @@ func New(cfg Config, log log.Logger, limits *validation.Overrides) (*Frontend, e
128149
}
129150
}
130151
f.roundTripper = roundTripper
131-
f.cond = sync.NewCond(&f.mtx)
132152
return f, nil
133153
}
134154

155+
// RoundTripFunc is to http.RoundTripper what http.HandlerFunc is to http.Handler.
156+
type RoundTripFunc func(*http.Request) (*http.Response, error)
157+
158+
// RoundTrip implements http.RoundTripper.
159+
func (f RoundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
160+
return f(r)
161+
}
162+
135163
// Close stops new requests and errors out any pending requests.
136164
func (f *Frontend) Close() {
137165
f.mtx.Lock()

0 commit comments

Comments
 (0)