Support Landing page on Prometheus landing page (#8641)
This commit is contained in:
parent
3f9643dd7e
commit
f241f91112
|
|
@ -159,12 +159,16 @@ func (p *PrometheusClient) Init() error {
|
||||||
authHandler := internal.AuthHandler(p.BasicUsername, p.BasicPassword, "prometheus", onAuthError)
|
authHandler := internal.AuthHandler(p.BasicUsername, p.BasicPassword, "prometheus", onAuthError)
|
||||||
rangeHandler := internal.IPRangeHandler(ipRange, onError)
|
rangeHandler := internal.IPRangeHandler(ipRange, onError)
|
||||||
promHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError})
|
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()
|
mux := http.NewServeMux()
|
||||||
if p.Path == "" {
|
if p.Path == "" {
|
||||||
p.Path = "/"
|
p.Path = "/metrics"
|
||||||
}
|
}
|
||||||
mux.Handle(p.Path, authHandler(rangeHandler(promHandler)))
|
mux.Handle(p.Path, authHandler(rangeHandler(promHandler)))
|
||||||
|
mux.Handle("/", authHandler(rangeHandler(landingPageHandler)))
|
||||||
|
|
||||||
tlsConfig, err := p.TLSConfig()
|
tlsConfig, err := p.TLSConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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)))
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue