From d9a202eaf6feda6e8eccb0a3c2d397a0a9e8caae Mon Sep 17 00:00:00 2001 From: Hwanjin Jeong Date: Fri, 20 Nov 2020 12:50:46 +0900 Subject: [PATCH 1/2] feat: add landing page in prometheus output plugin --- plugins/outputs/prometheus_client/prometheus_client.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/outputs/prometheus_client/prometheus_client.go b/plugins/outputs/prometheus_client/prometheus_client.go index 53713a02ba4e6..32abe8aa3c668 100644 --- a/plugins/outputs/prometheus_client/prometheus_client.go +++ b/plugins/outputs/prometheus_client/prometheus_client.go @@ -159,12 +159,16 @@ func (p *PrometheusClient) Init() error { authHandler := internal.AuthHandler(p.BasicUsername, p.BasicPassword, "prometheus", onAuthError) rangeHandler := internal.IPRangeHandler(ipRange, onError) promHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}) + landingPageHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Telegraf Output Plugin: Prometheus Client ")) + }) mux := http.NewServeMux() if p.Path == "" { - p.Path = "/" + p.Path = "/metrics" } mux.Handle(p.Path, authHandler(rangeHandler(promHandler))) + mux.Handle("/", authHandler(rangeHandler(landingPageHandler))) tlsConfig, err := p.TLSConfig() if err != nil { From 76e9ba175b781a2d799ecc0c7306986a3322a68a Mon Sep 17 00:00:00 2001 From: Hwanjin Jeong Date: Sun, 3 Jan 2021 21:40:46 +0900 Subject: [PATCH 2/2] test: add landing page test --- .../prometheus_client_v1_test.go | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/plugins/outputs/prometheus_client/prometheus_client_v1_test.go b/plugins/outputs/prometheus_client/prometheus_client_v1_test.go index adf18c9f0f076..39b8fec262095 100644 --- a/plugins/outputs/prometheus_client/prometheus_client_v1_test.go +++ b/plugins/outputs/prometheus_client/prometheus_client_v1_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "net/url" "strings" "testing" "time" @@ -400,3 +401,29 @@ rpc_duration_seconds_count 2693 }) } } + +func TestLandingPage(t *testing.T) { + Logger := testutil.Logger{Name: "outputs.prometheus_client"} + output := PrometheusClient{ + Listen: ":0", + CollectorsExclude: []string{"process"}, + MetricVersion: 1, + Log: Logger, + } + expected := "Telegraf Output Plugin: Prometheus Client" + + err := output.Init() + require.NoError(t, err) + + err = output.Connect() + require.NoError(t, err) + + u, err := url.Parse(fmt.Sprintf("http://%s/", output.url.Host)) + resp, err := http.Get(u.String()) + require.NoError(t, err) + + actual, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + + require.Equal(t, expected, strings.TrimSpace(string(actual))) +}