Tag original URL for k8s services in prometheus input (#3257)

This commit is contained in:
Christian Meilke 2017-09-23 02:26:19 +02:00 committed by Daniel Nelson
parent 53522f8eb2
commit b63f2a1acf
2 changed files with 8 additions and 5 deletions

View File

@ -93,14 +93,15 @@ func (p *Prometheus) AddressToURL(u *url.URL, address string) string {
} }
type UrlAndAddress struct { type UrlAndAddress struct {
Url string OriginalUrl string
Address string Url string
Address string
} }
func (p *Prometheus) GetAllURLs() ([]UrlAndAddress, error) { func (p *Prometheus) GetAllURLs() ([]UrlAndAddress, error) {
allUrls := make([]UrlAndAddress, 0) allUrls := make([]UrlAndAddress, 0)
for _, url := range p.Urls { for _, url := range p.Urls {
allUrls = append(allUrls, UrlAndAddress{Url: url}) allUrls = append(allUrls, UrlAndAddress{Url: url, OriginalUrl: url})
} }
for _, service := range p.KubernetesServices { for _, service := range p.KubernetesServices {
u, err := url.Parse(service) u, err := url.Parse(service)
@ -114,7 +115,7 @@ func (p *Prometheus) GetAllURLs() ([]UrlAndAddress, error) {
} }
for _, resolved := range resolvedAddresses { for _, resolved := range resolvedAddresses {
serviceUrl := p.AddressToURL(u, resolved) serviceUrl := p.AddressToURL(u, resolved)
allUrls = append(allUrls, UrlAndAddress{Url: serviceUrl, Address: resolved}) allUrls = append(allUrls, UrlAndAddress{Url: serviceUrl, Address: resolved, OriginalUrl: service})
} }
} }
return allUrls, nil return allUrls, nil
@ -213,7 +214,7 @@ func (p *Prometheus) gatherURL(url UrlAndAddress, acc telegraf.Accumulator) erro
// Add (or not) collected metrics // Add (or not) collected metrics
for _, metric := range metrics { for _, metric := range metrics {
tags := metric.Tags() tags := metric.Tags()
tags["url"] = url.Url tags["url"] = url.OriginalUrl
if url.Address != "" { if url.Address != "" {
tags["address"] = url.Address tags["address"] = url.Address
} }

View File

@ -50,6 +50,7 @@ func TestPrometheusGeneratesMetrics(t *testing.T) {
assert.True(t, acc.HasFloatField("test_metric", "value")) assert.True(t, acc.HasFloatField("test_metric", "value"))
assert.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0))) assert.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
assert.False(t, acc.HasTag("test_metric", "address")) assert.False(t, acc.HasTag("test_metric", "address"))
assert.True(t, acc.TagValue("test_metric", "url") == ts.URL)
} }
func TestPrometheusGeneratesMetricsWithHostNameTag(t *testing.T) { func TestPrometheusGeneratesMetricsWithHostNameTag(t *testing.T) {
@ -74,6 +75,7 @@ func TestPrometheusGeneratesMetricsWithHostNameTag(t *testing.T) {
assert.True(t, acc.HasFloatField("test_metric", "value")) assert.True(t, acc.HasFloatField("test_metric", "value"))
assert.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0))) assert.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
assert.True(t, acc.TagValue("test_metric", "address") == tsAddress) assert.True(t, acc.TagValue("test_metric", "address") == tsAddress)
assert.True(t, acc.TagValue("test_metric", "url") == ts.URL)
} }
func TestPrometheusGeneratesMetricsAlthoughFirstDNSFails(t *testing.T) { func TestPrometheusGeneratesMetricsAlthoughFirstDNSFails(t *testing.T) {