Support Landing page on Prometheus landing page (#8641)

This commit is contained in:
Hwanjin Jeong 2021-07-28 06:17:42 +09:00 committed by GitHub
parent 3f9643dd7e
commit f241f91112
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -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 {

View File

@ -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)))
}