From 73df179bd653bdde1ae26b392c1b387a29d7ded0 Mon Sep 17 00:00:00 2001
From: Christian Meilke <christian.meilke@googlemail.com>
Date: Sat, 23 Sep 2017 02:26:19 +0200
Subject: [PATCH] Tag original URL for k8s services in prometheus input 
 (#3257)

---
 plugins/inputs/prometheus/prometheus.go      | 11 ++++++-----
 plugins/inputs/prometheus/prometheus_test.go |  2 ++
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/plugins/inputs/prometheus/prometheus.go b/plugins/inputs/prometheus/prometheus.go
index a4f17e89..7c3943bd 100644
--- a/plugins/inputs/prometheus/prometheus.go
+++ b/plugins/inputs/prometheus/prometheus.go
@@ -93,14 +93,15 @@ func (p *Prometheus) AddressToURL(u *url.URL, address string) string {
 }
 
 type UrlAndAddress struct {
-	Url     string
-	Address string
+	OriginalUrl string
+	Url         string
+	Address     string
 }
 
 func (p *Prometheus) GetAllURLs() ([]UrlAndAddress, error) {
 	allUrls := make([]UrlAndAddress, 0)
 	for _, url := range p.Urls {
-		allUrls = append(allUrls, UrlAndAddress{Url: url})
+		allUrls = append(allUrls, UrlAndAddress{Url: url, OriginalUrl: url})
 	}
 	for _, service := range p.KubernetesServices {
 		u, err := url.Parse(service)
@@ -114,7 +115,7 @@ func (p *Prometheus) GetAllURLs() ([]UrlAndAddress, error) {
 		}
 		for _, resolved := range resolvedAddresses {
 			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
@@ -213,7 +214,7 @@ func (p *Prometheus) gatherURL(url UrlAndAddress, acc telegraf.Accumulator) erro
 	// Add (or not) collected metrics
 	for _, metric := range metrics {
 		tags := metric.Tags()
-		tags["url"] = url.Url
+		tags["url"] = url.OriginalUrl
 		if url.Address != "" {
 			tags["address"] = url.Address
 		}
diff --git a/plugins/inputs/prometheus/prometheus_test.go b/plugins/inputs/prometheus/prometheus_test.go
index 3cb93127..5bf07eb3 100644
--- a/plugins/inputs/prometheus/prometheus_test.go
+++ b/plugins/inputs/prometheus/prometheus_test.go
@@ -50,6 +50,7 @@ func TestPrometheusGeneratesMetrics(t *testing.T) {
 	assert.True(t, acc.HasFloatField("test_metric", "value"))
 	assert.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
 	assert.False(t, acc.HasTag("test_metric", "address"))
+	assert.True(t, acc.TagValue("test_metric", "url") == ts.URL)
 }
 
 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.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
 	assert.True(t, acc.TagValue("test_metric", "address") == tsAddress)
+	assert.True(t, acc.TagValue("test_metric", "url") == ts.URL)
 }
 
 func TestPrometheusGeneratesMetricsAlthoughFirstDNSFails(t *testing.T) {
-- 
GitLab