Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prometheus input: Remove credentials from URL before setting url tag #3743

Merged
merged 5 commits into from
Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/inputs/prometheus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Measurement names are based on the Metric Family and tags are created for each
label. The value is added to a field named based on the metric type.

All metrics receive the `url` tag indicating the related URL specified in the
Telegraf configuration. If using Kubernetes service discovery the `address`
Telegraf configuration. Existing username and password for basic authentication will be removed before. If using Kubernetes service discovery the `address`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this, we shouldn't have to say this because doing it the other way would be bad. BTW, using the userinfo section of the url is not basic auth.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, you are right, this is not basic auth! I red it in the referenced issue and wasn't think about it when writing the commit message and Readme.

I'll remove the sentence in Readme.md with next commit.

tag is also added indicating the discovered ip address.

### Example Output:
Expand Down
68 changes: 38 additions & 30 deletions plugins/inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const acceptHeader = `application/vnd.google.protobuf;proto=io.prometheus.client

type Prometheus struct {
// An array of urls to scrape metrics from.
Urls []string
URLs []string `toml:"urls"`

// An array of Kubernetes services to scrape metrics from.
KubernetesServices []string
Expand Down Expand Up @@ -73,12 +73,12 @@ func (p *Prometheus) Description() string {

var ErrProtocolError = errors.New("prometheus protocol error")

func (p *Prometheus) AddressToURL(u *url.URL, address string) string {
func (p *Prometheus) AddressToURL(u *url.URL, address string) *url.URL {
host := address
if u.Port() != "" {
host = address + ":" + u.Port()
}
reconstructedUrl := url.URL{
reconstructedURL := &url.URL{
Scheme: u.Scheme,
Opaque: u.Opaque,
User: u.User,
Expand All @@ -89,36 +89,42 @@ func (p *Prometheus) AddressToURL(u *url.URL, address string) string {
Fragment: u.Fragment,
Host: host,
}
return reconstructedUrl.String()
return reconstructedURL
}

type UrlAndAddress struct {
OriginalUrl string
Url string
type URLAndAddress struct {
OriginalURL *url.URL
URL *url.URL
Address string
}

func (p *Prometheus) GetAllURLs() ([]UrlAndAddress, error) {
allUrls := make([]UrlAndAddress, 0)
for _, url := range p.Urls {
allUrls = append(allUrls, UrlAndAddress{Url: url, OriginalUrl: url})
func (p *Prometheus) GetAllURLs() ([]URLAndAddress, error) {
allURLs := make([]URLAndAddress, 0)
for _, u := range p.URLs {
URL, err := url.Parse(u)
if err != nil {
log.Printf("prometheus: Could not parse %s, skipping it. Error: %s", u, err)
continue
}

allURLs = append(allURLs, URLAndAddress{URL: URL, OriginalURL: URL})
}
for _, service := range p.KubernetesServices {
u, err := url.Parse(service)
URL, err := url.Parse(service)
if err != nil {
return nil, err
}
resolvedAddresses, err := net.LookupHost(u.Hostname())
resolvedAddresses, err := net.LookupHost(URL.Hostname())
if err != nil {
log.Printf("prometheus: Could not resolve %s, skipping it. Error: %s", u.Host, err)
log.Printf("prometheus: Could not resolve %s, skipping it. Error: %s", URL.Host, err)
continue
}
for _, resolved := range resolvedAddresses {
serviceUrl := p.AddressToURL(u, resolved)
allUrls = append(allUrls, UrlAndAddress{Url: serviceUrl, Address: resolved, OriginalUrl: service})
serviceURL := p.AddressToURL(URL, resolved)
allURLs = append(allURLs, URLAndAddress{URL: serviceURL, Address: resolved, OriginalURL: URL})
}
}
return allUrls, nil
return allURLs, nil
}

// Reads stats from all configured servers accumulates stats.
Expand All @@ -134,16 +140,16 @@ func (p *Prometheus) Gather(acc telegraf.Accumulator) error {

var wg sync.WaitGroup

allUrls, err := p.GetAllURLs()
allURLs, err := p.GetAllURLs()
if err != nil {
return err
}
for _, url := range allUrls {
for _, URL := range allURLs {
wg.Add(1)
go func(serviceUrl UrlAndAddress) {
go func(serviceURL URLAndAddress) {
defer wg.Done()
acc.AddError(p.gatherURL(serviceUrl, acc))
}(url)
acc.AddError(p.gatherURL(serviceURL, acc))
}(URL)
}

wg.Wait()
Expand Down Expand Up @@ -178,8 +184,8 @@ func (p *Prometheus) createHttpClient() (*http.Client, error) {
return client, nil
}

func (p *Prometheus) gatherURL(url UrlAndAddress, acc telegraf.Accumulator) error {
var req, err = http.NewRequest("GET", url.Url, nil)
func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) error {
var req, err = http.NewRequest("GET", u.URL.String(), nil)
req.Header.Add("Accept", acceptHeader)
var token []byte
var resp *http.Response
Expand All @@ -194,11 +200,11 @@ func (p *Prometheus) gatherURL(url UrlAndAddress, acc telegraf.Accumulator) erro

resp, err = p.client.Do(req)
if err != nil {
return fmt.Errorf("error making HTTP request to %s: %s", url.Url, err)
return fmt.Errorf("error making HTTP request to %s: %s", u.URL, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s returned HTTP status %s", url.Url, resp.Status)
return fmt.Errorf("%s returned HTTP status %s", u.URL, resp.Status)
}

body, err := ioutil.ReadAll(resp.Body)
Expand All @@ -209,14 +215,16 @@ func (p *Prometheus) gatherURL(url UrlAndAddress, acc telegraf.Accumulator) erro
metrics, err := Parse(body, resp.Header)
if err != nil {
return fmt.Errorf("error reading metrics for %s: %s",
url.Url, err)
u.URL, err)
}
// Add (or not) collected metrics
for _, metric := range metrics {
tags := metric.Tags()
tags["url"] = url.OriginalUrl
if url.Address != "" {
tags["address"] = url.Address
// strip user and password from URL
u.OriginalURL.User = nil
tags["url"] = u.OriginalURL.String()
if u.Address != "" {
tags["address"] = u.Address
}

switch metric.Type() {
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestPrometheusGeneratesMetrics(t *testing.T) {
defer ts.Close()

p := &Prometheus{
Urls: []string{ts.URL},
URLs: []string{ts.URL},
}

var acc testutil.Accumulator
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestPrometheusGeneratesMetricsAlthoughFirstDNSFails(t *testing.T) {
defer ts.Close()

p := &Prometheus{
Urls: []string{ts.URL},
URLs: []string{ts.URL},
KubernetesServices: []string{"http://random.telegraf.local:88/metrics"},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func setupPrometheus() (*PrometheusClient, *prometheus_input.Prometheus, error)
time.Sleep(time.Millisecond * 200)

p := &prometheus_input.Prometheus{
Urls: []string{"http://localhost:9127/metrics"},
URLs: []string{"http://localhost:9127/metrics"},
}

return pTesting, p, nil
Expand Down