Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func loop(c Coordinator, client *http.Client) error {
level.Error(c.logger).Log("msg", "Error parsing url:", "err", err)
return errors.Wrap(err, "error parsing url")
}
u, err := url.Parse("poll")
u, err := url.Parse("poll/" + *myFqdn)
if err != nil {
level.Error(c.logger).Log("msg", "Error parsing url:", "err", err)
return errors.Wrap(err, "error parsing url poll")
Expand Down
6 changes: 5 additions & 1 deletion cmd/proxy/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/google/uuid"
"github.com/prometheus-community/pushprox/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus-community/pushprox/util"
)

var (
Expand Down Expand Up @@ -73,6 +73,8 @@ func NewCoordinator(logger log.Logger) (*Coordinator, error) {
}

// Generate a unique ID
// It is important this ID is cryptographically unique to ensure clients can't
// be mixed up.
func (c *Coordinator) genID() (string, error) {
id, err := uuid.NewRandom()
return id.String(), err
Expand Down Expand Up @@ -114,6 +116,8 @@ func (c *Coordinator) DoScrape(ctx context.Context, r *http.Request) (*http.Resp
return nil, err
}
level.Info(c.logger).Log("msg", "DoScrape", "scrape_id", id, "url", r.URL.String())
// It is important this id is cryptographically generated as it is relied
// upon to match the request and the response.
r.Header.Add("Id", id)
select {
case <-ctx.Done():
Expand Down
13 changes: 12 additions & 1 deletion cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func newHTTPHandler(logger log.Logger, coordinator *Coordinator, mux *http.Serve
handlers := map[string]http.HandlerFunc{
"/push": h.handlePush,
"/poll": h.handlePoll,
"/poll/": h.handlePollWithPath,
"/clients": h.handleListClients,
"/metrics": promhttp.Handler().ServeHTTP,
}
Expand Down Expand Up @@ -144,7 +145,17 @@ func (h *httpHandler) handlePush(w http.ResponseWriter, r *http.Request) {
// handlePoll handles clients registering and asking for scrapes.
func (h *httpHandler) handlePoll(w http.ResponseWriter, r *http.Request) {
fqdn, _ := ioutil.ReadAll(r.Body)
request, err := h.coordinator.WaitForScrapeInstruction(strings.TrimSpace(string(fqdn)))
h.pollWithFQDN(string(fqdn), w)
}

// handlePoll handles clients registering and asking for scrapes.
func (h *httpHandler) handlePollWithPath(w http.ResponseWriter, r *http.Request) {
fqdn := r.URL.Path[len("/poll/"):]
h.pollWithFQDN(fqdn, w)
}

func (h *httpHandler) pollWithFQDN(fqdn string, w http.ResponseWriter) {
request, err := h.coordinator.WaitForScrapeInstruction(strings.TrimSpace(fqdn))
if err != nil {
level.Info(h.logger).Log("msg", "Error WaitForScrapeInstruction:", "err", err)
http.Error(w, fmt.Sprintf("Error WaitForScrapeInstruction: %s", err.Error()), 408)
Expand Down