From de3fe22815b78bbb62f0fb8f2bb163c1d009b264 Mon Sep 17 00:00:00 2001 From: Nikolay Date: Thu, 29 Oct 2020 08:39:42 +0300 Subject: [PATCH] adds leading forward slash check for scrapeURL path (#855) * fixes in-consistency with prometheus behaviour for scrape targets url path. https://github.com/VictoriaMetrics/VictoriaMetrics/issues/835 --- lib/promscrape/config.go | 3 +++ lib/promscrape/config_test.go | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/promscrape/config.go b/lib/promscrape/config.go index 9595e78335..c174414ce3 100644 --- a/lib/promscrape/config.go +++ b/lib/promscrape/config.go @@ -663,6 +663,9 @@ func appendScrapeWork(dst []ScrapeWork, swc *scrapeWorkConfig, target string, ex if metricsPathRelabeled == "" { metricsPathRelabeled = "/metrics" } + if !strings.HasPrefix(metricsPathRelabeled, "/") { + metricsPathRelabeled = "/" + metricsPathRelabeled + } paramsRelabeled := getParamsFromLabels(labels, swc.params) optionalQuestion := "?" if len(paramsRelabeled) == 0 || strings.Contains(metricsPathRelabeled, "?") { diff --git a/lib/promscrape/config_test.go b/lib/promscrape/config_test.go index e84567b404..d2834c1e7f 100644 --- a/lib/promscrape/config_test.go +++ b/lib/promscrape/config_test.go @@ -1331,6 +1331,45 @@ scrape_configs: jobNameOriginal: "snmp", }, }) + f(` +scrape_configs: +- job_name: path wo slash + static_configs: + - targets: ["foo.bar:1234"] + relabel_configs: + - replacement: metricspath + target_label: __metrics_path__ +`, []ScrapeWork{ + { + ScrapeURL: "http://foo.bar:1234/metricspath", + ScrapeInterval: defaultScrapeInterval, + ScrapeTimeout: defaultScrapeTimeout, + Labels: []prompbmarshal.Label{ + { + Name: "__address__", + Value: "foo.bar:1234", + }, + { + Name: "__metrics_path__", + Value: "metricspath", + }, + { + Name: "__scheme__", + Value: "http", + }, + { + Name: "instance", + Value: "foo.bar:1234", + }, + { + Name: "job", + Value: "path wo slash", + }, + }, + jobNameOriginal: "path wo slash", + AuthConfig: &promauth.Config{}, + }, + }) } var defaultRegexForRelabelConfig = regexp.MustCompile("^(.*)$")