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

Some improvements .... #1

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions docker-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

docker build -t carlozleite/azure-metrics-exporter . --no-cache
docker push carlozleite/azure-metrics-exporter
3 changes: 3 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

/opt/go/bin/azure-metrics-exporter --web.listen-address=":9090" --config.file="/config/${CONFIG_FILE}"
31 changes: 21 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,28 @@ 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("-", "_", " ", "", "/", "")
ac.getAccessToken()
metricValueData = ac.getMetricValue(metric.Name, target.Resource)
metricName := ToSnakeCase(metricValueData.Value[0].Name.Value)
metricValue := metricValueData.Value[0].Data[len(metricValueData.Value[0].Data)-1]
labels := CreateResourceLabels(metricValueData.Value[0].ID)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(metricName, "", nil, labels),
prometheus.GaugeValue,
metricValue.Total,
)
}
}
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,
)
}
}
} }
}

func handler(w http.ResponseWriter, r *http.Request) {
Expand Down
1 change: 1 addition & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down