From 98f912235e75aba752bc9d13ff777efe3d1216ca Mon Sep 17 00:00:00 2001 From: root Date: Fri, 17 Nov 2017 11:02:13 +0000 Subject: [PATCH 01/14] Change Metrics name to Unique --- main.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 29398ab..d8bbbc9 100644 --- a/main.go +++ b/main.go @@ -44,11 +44,15 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { for _, target := range sc.C.Targets { for _, metric := range target.Metrics { metricValueData = ac.getMetricValue(metric.Name, target.Resource) + //fmt.Printf(metric.Name) metricName := ToSnakeCase(metricValueData.Value[0].Name.Value) metricValue := metricValueData.Value[0].Data[len(metricValueData.Value[0].Data)-1] labels := CreateResourceLabels(metricValueData.Value[0].ID) + //fmt.Printf(metricValueData.Value[0].ID) + splitres := strings.Split(target.Resource, "/") + sname := (splitres[len(splitres)-1]) ch <- prometheus.MustNewConstMetric( - prometheus.NewDesc(metricName, "", nil, labels), + prometheus.NewDesc(sname+"_"+metricName, "", nil, labels), prometheus.GaugeValue, metricValue.Total, ) From 6038db3399b22b67d157740a44590d6bcd6a89f9 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 17 Nov 2017 11:48:19 +0000 Subject: [PATCH 02/14] Change Metrics name to Unique + ToSnakeCase --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index d8bbbc9..5b937fd 100644 --- a/main.go +++ b/main.go @@ -50,7 +50,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { labels := CreateResourceLabels(metricValueData.Value[0].ID) //fmt.Printf(metricValueData.Value[0].ID) splitres := strings.Split(target.Resource, "/") - sname := (splitres[len(splitres)-1]) + sname := ToSnakeCase((splitres[len(splitres)-1])) ch <- prometheus.MustNewConstMetric( prometheus.NewDesc(sname+"_"+metricName, "", nil, labels), prometheus.GaugeValue, From d9c669224c11e20c2806e73889ce3a31208d9243 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 17 Nov 2017 12:10:14 +0000 Subject: [PATCH 03/14] Replace strange characters ...prometheus token error ... --- main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 5b937fd..3f6852d 100644 --- a/main.go +++ b/main.go @@ -43,14 +43,15 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { var metricValueData AzureMetricValiueResponse for _, target := range sc.C.Targets { for _, metric := range target.Metrics { + var replacer = strings.NewReplacer("-", "_", " ", "", "/", "") metricValueData = ac.getMetricValue(metric.Name, target.Resource) //fmt.Printf(metric.Name) - metricName := ToSnakeCase(metricValueData.Value[0].Name.Value) + metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) metricValue := metricValueData.Value[0].Data[len(metricValueData.Value[0].Data)-1] labels := CreateResourceLabels(metricValueData.Value[0].ID) //fmt.Printf(metricValueData.Value[0].ID) splitres := strings.Split(target.Resource, "/") - sname := ToSnakeCase((splitres[len(splitres)-1])) + sname := replacer.Replace((splitres[len(splitres)-1])) ch <- prometheus.MustNewConstMetric( prometheus.NewDesc(sname+"_"+metricName, "", nil, labels), prometheus.GaugeValue, From 58df011ecf0d5694a69a23aaf547176bc8603506 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 21 Nov 2017 16:28:53 +0000 Subject: [PATCH 04/14] Check null metrics .... --- main.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 3f6852d..4799e56 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/version" + "github.com/davecgh/go-spew/spew" kingpin "gopkg.in/alecthomas/kingpin.v2" ) @@ -45,20 +46,23 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { for _, metric := range target.Metrics { var replacer = strings.NewReplacer("-", "_", " ", "", "/", "") metricValueData = ac.getMetricValue(metric.Name, target.Resource) - //fmt.Printf(metric.Name) + //fmt.Printf(len(metricValueData.Value[0].Data)) + spew.Dump(len(metricValueData.Value[0].Data)) metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) - metricValue := metricValueData.Value[0].Data[len(metricValueData.Value[0].Data)-1] + //vals := make([]int, 0) + if len(metricValueData.Value[0].Data) != 0 { + metricValue := metricValueData.Value[0].Data[len(metricValueData.Value[0].Data)-1] labels := CreateResourceLabels(metricValueData.Value[0].ID) - //fmt.Printf(metricValueData.Value[0].ID) splitres := strings.Split(target.Resource, "/") - sname := replacer.Replace((splitres[len(splitres)-1])) + sname := replacer.Replace(splitres[len(splitres)-2]+"_"+splitres[len(splitres)-1]) + //fmt.Printf(sname) ch <- prometheus.MustNewConstMetric( prometheus.NewDesc(sname+"_"+metricName, "", nil, labels), prometheus.GaugeValue, metricValue.Total, ) } - } + } } } func handler(w http.ResponseWriter, r *http.Request) { From 23834e9ca26254f3c3fd96e8f39dc8f2ad8a9d7f Mon Sep 17 00:00:00 2001 From: root Date: Tue, 21 Nov 2017 16:29:14 +0000 Subject: [PATCH 05/14] Check null metrics .... --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 4799e56..d623df9 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { var replacer = strings.NewReplacer("-", "_", " ", "", "/", "") metricValueData = ac.getMetricValue(metric.Name, target.Resource) //fmt.Printf(len(metricValueData.Value[0].Data)) - spew.Dump(len(metricValueData.Value[0].Data)) + //spew.Dump(len(metricValueData.Value[0].Data)) metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) //vals := make([]int, 0) if len(metricValueData.Value[0].Data) != 0 { From bb8a3608ad4d00ad418e12f99f20db28ae1451ae Mon Sep 17 00:00:00 2001 From: root Date: Tue, 21 Nov 2017 17:05:10 +0000 Subject: [PATCH 06/14] Check null metrics 2 .... --- main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index d623df9..40d8e13 100644 --- a/main.go +++ b/main.go @@ -45,12 +45,15 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { for _, target := range sc.C.Targets { for _, metric := range target.Metrics { var replacer = strings.NewReplacer("-", "_", " ", "", "/", "") + spew.Dump(target.Resource) metricValueData = ac.getMetricValue(metric.Name, target.Resource) + if metricValueData.Value != nil { //fmt.Printf(len(metricValueData.Value[0].Data)) - //spew.Dump(len(metricValueData.Value[0].Data)) - metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) + //spew.Dump(metricValueData.Value) + //metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) //vals := make([]int, 0) if len(metricValueData.Value[0].Data) != 0 { + metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) metricValue := metricValueData.Value[0].Data[len(metricValueData.Value[0].Data)-1] labels := CreateResourceLabels(metricValueData.Value[0].ID) splitres := strings.Split(target.Resource, "/") @@ -62,6 +65,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { metricValue.Total, ) } + } } } } From 7355859bdc0a65a9a88398550bc5fdb97d361b53 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 22 Nov 2017 13:21:43 +0000 Subject: [PATCH 07/14] Fix labels .... --- main.go | 25 +++++++++++++++++++------ utils.go | 1 + 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 40d8e13..34bb9f9 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/version" - "github.com/davecgh/go-spew/spew" + //"github.com/davecgh/go-spew/spew" kingpin "gopkg.in/alecthomas/kingpin.v2" ) @@ -45,7 +45,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { for _, target := range sc.C.Targets { for _, metric := range target.Metrics { var replacer = strings.NewReplacer("-", "_", " ", "", "/", "") - spew.Dump(target.Resource) + //spew.Dump(target.Resource) metricValueData = ac.getMetricValue(metric.Name, target.Resource) if metricValueData.Value != nil { //fmt.Printf(len(metricValueData.Value[0].Data)) @@ -55,14 +55,27 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { if len(metricValueData.Value[0].Data) != 0 { metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) metricValue := metricValueData.Value[0].Data[len(metricValueData.Value[0].Data)-1] - labels := CreateResourceLabels(metricValueData.Value[0].ID) - splitres := strings.Split(target.Resource, "/") - sname := replacer.Replace(splitres[len(splitres)-2]+"_"+splitres[len(splitres)-1]) + //labels := CreateResourceLabels(metricValueData.Value[0].ID) + //spew.Dump(metricName) + //spew.Dump(labels) + //spew.Dump(metricValueData.Value[0].ID) + //splitres := strings.Split(target.Resource, "/") + //sname := replacer.Replace(splitres[len(splitres)-2]+"_"+splitres[len(splitres)-1]) //fmt.Printf(sname) + resource_type := strings.Split(metricValueData.Value[0].ID, "/")[6] + restype := strings.Split(resource_type, ".")[1] + fmt.Printf(restype) + resource_group := strings.Split(metricValueData.Value[0].ID, "/")[4] + resource_name := strings.Split(metricValueData.Value[0].ID, "/")[8] ch <- prometheus.MustNewConstMetric( - prometheus.NewDesc(sname+"_"+metricName, "", nil, labels), + //prometheus.NewDesc(sname+"_"+metricName, "", nil, labels), + //prometheus.NewDesc(metricName, "", nil, prometheus.Labels{"resource_type": resource_type, "resource_group": resource_group, "resource_name": resource_name},), + prometheus.NewDesc(restype+"."+metricName, "", []string{"resource_type", "resource_group", "resource_name"}, nil), prometheus.GaugeValue, metricValue.Total, + resource_type, + resource_group, + resource_name, ) } } diff --git a/utils.go b/utils.go index 02d0ba6..c4c6600 100644 --- a/utils.go +++ b/utils.go @@ -29,6 +29,7 @@ func GetTimes() (string, string) { // CreateResourceLabels - Returns resource labels for a give resource ID. func CreateResourceLabels(resourceID string) map[string]string { labels := make(map[string]string) + labels["resource_type"] = strings.Split(resourceID, "/")[6] labels["resource_group"] = strings.Split(resourceID, "/")[4] labels["resource_name"] = strings.Split(resourceID, "/")[8] return labels From 2cced5a8f147df4698d48fcaf0b8182f9fffc3cb Mon Sep 17 00:00:00 2001 From: root Date: Wed, 22 Nov 2017 13:22:39 +0000 Subject: [PATCH 08/14] Fix labels .... --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 34bb9f9..15fa389 100644 --- a/main.go +++ b/main.go @@ -64,7 +64,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { //fmt.Printf(sname) resource_type := strings.Split(metricValueData.Value[0].ID, "/")[6] restype := strings.Split(resource_type, ".")[1] - fmt.Printf(restype) + //fmt.Printf(restype) resource_group := strings.Split(metricValueData.Value[0].ID, "/")[4] resource_name := strings.Split(metricValueData.Value[0].ID, "/")[8] ch <- prometheus.MustNewConstMetric( From 85dc2dc49f5790fe1d0290b733800a37ee02fdc9 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 22 Nov 2017 13:23:55 +0000 Subject: [PATCH 09/14] Fix labels .... --- main.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/main.go b/main.go index 15fa389..449d414 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/version" - //"github.com/davecgh/go-spew/spew" kingpin "gopkg.in/alecthomas/kingpin.v2" ) @@ -45,31 +44,16 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { for _, target := range sc.C.Targets { for _, metric := range target.Metrics { var replacer = strings.NewReplacer("-", "_", " ", "", "/", "") - //spew.Dump(target.Resource) metricValueData = ac.getMetricValue(metric.Name, target.Resource) if metricValueData.Value != nil { - //fmt.Printf(len(metricValueData.Value[0].Data)) - //spew.Dump(metricValueData.Value) - //metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) - //vals := make([]int, 0) if len(metricValueData.Value[0].Data) != 0 { metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) metricValue := metricValueData.Value[0].Data[len(metricValueData.Value[0].Data)-1] - //labels := CreateResourceLabels(metricValueData.Value[0].ID) - //spew.Dump(metricName) - //spew.Dump(labels) - //spew.Dump(metricValueData.Value[0].ID) - //splitres := strings.Split(target.Resource, "/") - //sname := replacer.Replace(splitres[len(splitres)-2]+"_"+splitres[len(splitres)-1]) - //fmt.Printf(sname) resource_type := strings.Split(metricValueData.Value[0].ID, "/")[6] restype := strings.Split(resource_type, ".")[1] - //fmt.Printf(restype) resource_group := strings.Split(metricValueData.Value[0].ID, "/")[4] resource_name := strings.Split(metricValueData.Value[0].ID, "/")[8] ch <- prometheus.MustNewConstMetric( - //prometheus.NewDesc(sname+"_"+metricName, "", nil, labels), - //prometheus.NewDesc(metricName, "", nil, prometheus.Labels{"resource_type": resource_type, "resource_group": resource_group, "resource_name": resource_name},), prometheus.NewDesc(restype+"."+metricName, "", []string{"resource_type", "resource_group", "resource_name"}, nil), prometheus.GaugeValue, metricValue.Total, From 6560f4f2cc92b521f5b1673fba855acff0ec587e Mon Sep 17 00:00:00 2001 From: root Date: Wed, 22 Nov 2017 13:30:29 +0000 Subject: [PATCH 10/14] Fix labels .... --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 449d414..90c68e1 100644 --- a/main.go +++ b/main.go @@ -54,7 +54,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { resource_group := strings.Split(metricValueData.Value[0].ID, "/")[4] resource_name := strings.Split(metricValueData.Value[0].ID, "/")[8] ch <- prometheus.MustNewConstMetric( - prometheus.NewDesc(restype+"."+metricName, "", []string{"resource_type", "resource_group", "resource_name"}, nil), + prometheus.NewDesc(restype+"_"+metricName, "", []string{"resource_type", "resource_group", "resource_name"}, nil), prometheus.GaugeValue, metricValue.Total, resource_type, From fc433a71f73ed9db61c6f6aab0e87b4755b88508 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 22 Nov 2017 13:55:06 +0000 Subject: [PATCH 11/14] Ident ...:) --- main.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 90c68e1..d6be0d7 100644 --- a/main.go +++ b/main.go @@ -46,23 +46,23 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { var replacer = strings.NewReplacer("-", "_", " ", "", "/", "") metricValueData = ac.getMetricValue(metric.Name, target.Resource) if metricValueData.Value != nil { - if len(metricValueData.Value[0].Data) != 0 { - metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) - metricValue := metricValueData.Value[0].Data[len(metricValueData.Value[0].Data)-1] - resource_type := strings.Split(metricValueData.Value[0].ID, "/")[6] - restype := strings.Split(resource_type, ".")[1] - resource_group := strings.Split(metricValueData.Value[0].ID, "/")[4] - resource_name := strings.Split(metricValueData.Value[0].ID, "/")[8] - ch <- prometheus.MustNewConstMetric( - prometheus.NewDesc(restype+"_"+metricName, "", []string{"resource_type", "resource_group", "resource_name"}, nil), - prometheus.GaugeValue, - metricValue.Total, - resource_type, - resource_group, - resource_name, - ) - } - } + if len(metricValueData.Value[0].Data) != 0 { + metricName := ToSnakeCase(replacer.Replace(metricValueData.Value[0].Name.Value)) + metricValue := metricValueData.Value[0].Data[len(metricValueData.Value[0].Data)-1] + resource_type := strings.Split(metricValueData.Value[0].ID, "/")[6] + restype := strings.Split(resource_type, ".")[1] + resource_group := strings.Split(metricValueData.Value[0].ID, "/")[4] + resource_name := strings.Split(metricValueData.Value[0].ID, "/")[8] + ch <- prometheus.MustNewConstMetric( + prometheus.NewDesc(restype+"_"+metricName, "", []string{"resource_type", "resource_group", "resource_name"}, nil), + prometheus.GaugeValue, + metricValue.Total, + resource_type, + resource_group, + resource_name, + ) + } + } } } } From e8675c8ad0d0656165b042876f7e8e741bcfff6c Mon Sep 17 00:00:00 2001 From: root Date: Wed, 22 Nov 2017 16:51:35 +0000 Subject: [PATCH 12/14] Add docker support ... --- Dockerfile | 13 +++++++++++++ docker-build.sh | 4 ++++ entrypoint.sh | 3 +++ 3 files changed, 20 insertions(+) create mode 100644 Dockerfile create mode 100755 docker-build.sh create mode 100755 entrypoint.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3e15265 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM alpine + +RUN apk update +RUN apk add bash go git musl-dev +RUN mkdir -p /opt/go +ENV GOPATH=/opt/go +RUN go get github.com/carlozleite/azure-metrics-exporter +ADD entrypoint.sh /entrypoint.sh +RUN chmod 755 /entrypoint.sh +RUN mkdir /config + +ENTRYPOINT ["/entrypoint.sh"] + diff --git a/docker-build.sh b/docker-build.sh new file mode 100755 index 0000000..d3daee0 --- /dev/null +++ b/docker-build.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +docker build -t carlozleite/azure-metrics-exporter . --no-cache +docker push carlozleite/azure-metrics-exporter diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..f1bde8e --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +/opt/go/bin/azure-metrics-exporter --web.listen-address=":9090" --config.file="/config/${CONFIG_FILE}" From 6fa201e3e87266ffc2c71fd73ff63f8fbb092611 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 22 Nov 2017 17:09:51 +0000 Subject: [PATCH 13/14] Add docker readme ... --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 2c3b7b2..8bf30d0 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,13 @@ scrape_configs: static_configs: - targets: ['localhost:9276'] ``` + +# Docker Usage + + +``` +docker run -it -e CONFIG_FILE=Compute.yml -v /devops/azure-exporter/config:/config carlozleite/azure-metrics-exporter:latest + +``` + +You can create different services in docker swarm one for each type of resource provider in azure just swap the YML file and then create a Prometheus service and point the targets to the service names on port 9090. From 4249309bcdfbde94a847e96b3c2e42c37e684114 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 23 Nov 2017 10:00:18 +0000 Subject: [PATCH 14/14] =?UTF-8?q?Add=20ac.getAccessToken()=20inside=20coll?= =?UTF-8?q?ector=20...=20because=20credential=20session=20exp=C3=ADres=20a?= =?UTF-8?q?fter=20some=20minutes=20...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index d6be0d7..067ae52 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { for _, target := range sc.C.Targets { for _, metric := range target.Metrics { var replacer = strings.NewReplacer("-", "_", " ", "", "/", "") + ac.getAccessToken() metricValueData = ac.getMetricValue(metric.Name, target.Resource) if metricValueData.Value != nil { if len(metricValueData.Value[0].Data) != 0 {