Skip to content

Commit

Permalink
Support Landing page on Prometheus landing page (#8641)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwanjin-jeong authored Jul 27, 2021
1 parent 3f9643d commit f241f91
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions plugins/outputs/prometheus_client/prometheus_client_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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)))
}

0 comments on commit f241f91

Please sign in to comment.