From 347261672d7f07b17b406bd5bdaca3bb30f14018 Mon Sep 17 00:00:00 2001 From: david amick Date: Mon, 25 Feb 2019 20:40:45 +0000 Subject: [PATCH 1/2] Allow override URL --- client/client.go | 15 ++++++++++++++- util/client.go | 18 ++++++++++++++++++ util/client_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 util/client.go create mode 100644 util/client_test.go diff --git a/client/client.go b/client/client.go index c471d44..bc94a48 100644 --- a/client/client.go +++ b/client/client.go @@ -24,7 +24,9 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/promlog" "github.com/prometheus/common/promlog/flag" - "github.com/robustperception/pushprox/util" + // TODO + // "github.com/robustperception/pushprox/util" + "github.com/snarlysodboxer/PushProx/util" ) var ( @@ -34,6 +36,7 @@ var ( tlsCert = kingpin.Flag("tls.cert", " Client certificate file").String() tlsKey = kingpin.Flag("tls.key", " Private key file").String() metricsAddr = kingpin.Flag("metrics-addr", "Serve Prometheus metrics at this address").Default(":9369").String() + overrideURL = kingpin.Flag("override-url", " Force the URL to scrape").String() ) var ( @@ -72,6 +75,16 @@ func (c *Coordinator) doScrape(request *http.Request, client *http.Client) { // We cannot handle https requests at the proxy, as we would only // see a CONNECT, so use a URL parameter to trigger it. params := request.URL.Query() + + // Force GET verb + request.Method = "GET" + + // Override + if err := util.OverrideURLIfDesired(overrideURL, request); err != nil { + level.Error(logger).Log("msg", "Failed to parse overrideURL:", "err", err) + return + } + if params.Get("_scheme") == "https" { request.URL.Scheme = "https" params.Del("_scheme") diff --git a/util/client.go b/util/client.go new file mode 100644 index 0000000..4efd8cc --- /dev/null +++ b/util/client.go @@ -0,0 +1,18 @@ +package util + +import ( + "net/http" + "net/url" +) + +func OverrideURLIfDesired(overrideURL *string, request *http.Request) error { + if *overrideURL != "" { + parsedURL, err := url.Parse(*overrideURL) + if err != nil { + return err + } + request.URL = parsedURL + } + + return nil +} diff --git a/util/client_test.go b/util/client_test.go new file mode 100644 index 0000000..edc0367 --- /dev/null +++ b/util/client_test.go @@ -0,0 +1,32 @@ +package util + +import ( + "net/http" + "net/url" + "testing" +) + +func TestOverrideURLIfDesired(t *testing.T) { + // With override + request := &http.Request{URL: &url.URL{Scheme: "http", Host: "wrong-service:9090", Path: "/v1/user/1"}} + overrideURL := "http://localhost:8080/metrics" + err := OverrideURLIfDesired(&overrideURL, request) + if err != nil { + t.Errorf("Expected no error, got one: %v", err) + } + if request.URL.String() != overrideURL { + t.Errorf("Expected %s, got %s", overrideURL, request.URL.String()) + } + + // Without override + request = &http.Request{URL: &url.URL{Scheme: "http", Host: "localhost:9090", Path: "/metrics"}} + overrideURL = "" + err = OverrideURLIfDesired(&overrideURL, request) + if err != nil { + t.Errorf("Expected no error, got one: %v", err) + } + nonOverride := "http://localhost:9090/metrics" + if request.URL.String() != nonOverride { + t.Errorf("Expected %s, got %s", nonOverride, request.URL.String()) + } +} From 298a3531346bb6d9fdde5dc359dabe01dbdc6e38 Mon Sep 17 00:00:00 2001 From: david amick Date: Wed, 27 Feb 2019 19:32:34 +0000 Subject: [PATCH 2/2] Update readme, put import back --- README.md | 6 +++++- client/client.go | 4 +--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2189c4c..cb7e852 100644 --- a/README.md +++ b/README.md @@ -74,4 +74,8 @@ There is no authentication or authorisation included, a reverse proxy can be put in front though to add these. Running the client allows those with access to the proxy or the client to access -all network services on the machine hosting the client. +all network services on the machine hosting the client, unless you specify the +`--override-url` setting. +``` +./client --proxy-url=http://proxy:8080/ --override-url=http://client:9100/metrics +``` diff --git a/client/client.go b/client/client.go index bc94a48..2fbf552 100644 --- a/client/client.go +++ b/client/client.go @@ -24,9 +24,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/promlog" "github.com/prometheus/common/promlog/flag" - // TODO - // "github.com/robustperception/pushprox/util" - "github.com/snarlysodboxer/PushProx/util" + "github.com/robustperception/pushprox/util" ) var (