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

files formatting / fix logging and add helm chart #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
clouds.yml
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.13-alpine as build
RUN apk --no-cache add ca-certificates && \
rm -Rf /var/cache/apk/*
WORKDIR /app
COPY . .
RUN GOPROXY=https://goproxy.cn CGO_ENABLED=0 go build -o cloudeye-exporter main.go

FROM alpine:3.8
RUN addgroup -S app && adduser -S app -G app
USER app
EXPOSE 8080
ENTRYPOINT [ "/usr/local/bin/cloudeye-exporter" ]
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /app/cloudeye-exporter /usr/local/bin/cloudeye-exporter
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Prometheus cloudeye exporter for [Huaweicloud](https://www.huaweicloud.com/).
$ git clone https://github.com/huaweicloud/cloudeye-exporter
```

## (Option) Building The Discovery with Exact steps on clean Ubuntu 16.04
## Build

### (Option) Building The Discovery with Exact steps on clean Ubuntu 16.04
```
$ wget https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz
$ sudo tar -C /usr/local -xzf go1.12.5.linux-amd64.tar.gz
Expand All @@ -19,26 +21,30 @@ $ cd ~/go/src/github.com/huaweicloud/cloudeye-exporter
$ go build
```

### Build docker image
```
$ export DOCKER_USERNAME=xxx
$ docker build -t $DOCKER_USERNAME/cloudeye-exporter .
```

## Usage
```
./cloudeye-exporter -config=clouds.yml
./cloudeye-exporter -config=clouds.yml
```

The default port is 8087, default config file location is ./clouds.yml.

Visit metrics in http://localhost:8087/metrics?services=SYS.VPC,SYS.ELB


## Help
## Deploy to Kubernetes with helm
```
Usage of ./cloudeye-exporter:
-config string
Path to the cloud configuration file (default "./clouds.yml")
-debug
If debug the code.

$ # edit deploy/cloudeye-exporter/values.yaml
$ # then
$ helm install cloudeye-exporter deploy/cloudeye-exporter
```

---

## Example of config file(clouds.yml)
```
global:
Expand Down
Binary file removed cloudeye-exporter
Binary file not shown.
104 changes: 49 additions & 55 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,35 @@
package collector

import (
"encoding/json"
"fmt"
"time"
"strconv"
"strings"
"encoding/json"
"time"

"github.com/prometheus/common/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/huaweicloud/golangsdk/openstack/ces/v1/metricdata"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)


var defaultLabelsToResource = map[string]string{
"lbaas_listener_id": "listener",
"lb_instance_id": "lb",
"direct_connect_id": "direct",
"history_direct_connect_id": "history",
"virtual_interface_id": "virtual",
"bandwidth_id": "bandwidth",
"publicip_id": "eip",
"lbaas_listener_id": "listener",
"lb_instance_id": "lb",
"direct_connect_id": "direct",
"history_direct_connect_id": "history",
"virtual_interface_id": "virtual",
"bandwidth_id": "bandwidth",
"publicip_id": "eip",
}

var privateResourceFlag = map[string]string {
"kafka_broker": "broker",
"kafka_topics": "topics",
var privateResourceFlag = map[string]string{
"kafka_broker": "broker",
"kafka_topics": "topics",
"kafka_partitions": "partitions",
"kafka_groups": "groups",
"rabbitmq_node": "rabbitmq_node",
"kafka_groups": "groups",
"rabbitmq_node": "rabbitmq_node",
}


type BaseHuaweiCloudExporter struct {
From string
To string
Expand All @@ -57,8 +55,7 @@ type BaseHuaweiCloudExporter struct {
Region string
}


func replaceName(name string) (string) {
func replaceName(name string) string {
newName := strings.Replace(name, ".", "_", -1)
newName = strings.ToLower(newName)

Expand All @@ -82,7 +79,6 @@ func GetMonitoringCollector(configpath string, namespaces []string, debug bool)
return exporter, nil
}


func GetMetricPrefixName(prefix string, namespace string) string {
return fmt.Sprintf("%s_%s", prefix, replaceName(namespace))
}
Expand All @@ -92,7 +88,7 @@ func (exporter *BaseHuaweiCloudExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- prometheus.NewDesc("dummy", "dummy", nil, nil)
}

func (exporter *BaseHuaweiCloudExporter) collectMetricByNamespace(ch chan<- prometheus.Metric, namespace string) {
func (exporter *BaseHuaweiCloudExporter) collectMetricByNamespace(ch chan<- prometheus.Metric, namespace string) {
allMetrics, err := getAllMetric(exporter.ClientConfig, namespace)
if err != nil {
log.Fatal(err)
Expand All @@ -104,8 +100,8 @@ func (exporter *BaseHuaweiCloudExporter) collectMetricByNamespace(ch chan<- prom
}

if exporter.Debug == true {
metricsJson, _ := json.MarshalIndent(*allMetrics, "", " ")
fmt.Println("The all metrics are:", string(metricsJson))
metricsJSON, _ := json.MarshalIndent(*allMetrics, "", " ")
log.Debug("The all metrics are:", string(metricsJSON))
}

allResoucesInfo := exporter.getAllResource(namespace)
Expand All @@ -118,8 +114,8 @@ func (exporter *BaseHuaweiCloudExporter) collectMetricByNamespace(ch chan<- prom
count++

tmpMetrics = append(tmpMetrics, getDataMetric(metric))
if (0 == count % 10) || (count == end) {
mds, err:= getBatchMetricData(exporter.ClientConfig, &tmpMetrics, exporter.From, exporter.To)
if (0 == count%10) || (count == end) {
mds, err := getBatchMetricData(exporter.ClientConfig, &tmpMetrics, exporter.From, exporter.To)
tmpMetrics = []metricdata.Metric{}
if err != nil {
continue
Expand All @@ -129,23 +125,23 @@ func (exporter *BaseHuaweiCloudExporter) collectMetricByNamespace(ch chan<- prom
exporter.debugMetricInfo(md)
datapoint, err := getDatapoint(md.Datapoints)
if err != nil {
fmt.Printf("%s, the metric is:", err, md.MetricName)
log.Warnf("%s, the metric is: %s", err, md.MetricName)
continue
}

labels, values, preResourceName, privateFlag := getOriginalLabelInfo(&md.Dimensions)
if isResouceExist(&md.Dimensions, &allResoucesInfo)== true {
labels = exporter.getExtensionLabels(labels, preResourceName, namespace, privateFlag)
values = exporter.getExtensionLabelValues(values, &allResoucesInfo, getOriginalID(&md.Dimensions))
}
if isResouceExist(&md.Dimensions, &allResoucesInfo) == true {
labels = exporter.getExtensionLabels(labels, preResourceName, namespace, privateFlag)
values = exporter.getExtensionLabelValues(values, &allResoucesInfo, getOriginalID(&md.Dimensions))
}

newMetricName := prometheus.BuildFQName(GetMetricPrefixName(exporter.Prefix, namespace), preResourceName, md.MetricName)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(newMetricName, newMetricName, labels, nil),
prometheus.GaugeValue, datapoint, values...)
}

metricTimestamp, _ = getMetricDataTimestamp((*mds)[len(*mds) - 1].Datapoints)
metricTimestamp, _ = getMetricDataTimestamp((*mds)[len(*mds)-1].Datapoints)
}
}

Expand All @@ -156,18 +152,18 @@ func (exporter *BaseHuaweiCloudExporter) collectMetricByNamespace(ch chan<- prom
to64, _ := strconv.ParseFloat(exporter.To, 64)
stamp64 := float64(metricTimestamp)

sub_duration := (to64 - stamp64) / 1000
subDuration := (to64 - stamp64) / 1000
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(GetMetricPrefixName(exporter.Prefix, namespace) + "_duration_seconds",
namespace, nil, nil), prometheus.GaugeValue, sub_duration)
prometheus.NewDesc(GetMetricPrefixName(exporter.Prefix, namespace)+"_duration_seconds",
namespace, nil, nil), prometheus.GaugeValue, subDuration)
}

func (exporter *BaseHuaweiCloudExporter) Collect(ch chan<- prometheus.Metric) {
periodm, _ := time.ParseDuration("-5m")

now := time.Now()
from := strconv.FormatInt(int64(now.Add(periodm).UnixNano() / 1e6),10)
to := strconv.FormatInt(int64(now.UnixNano() / 1e6),10)
from := strconv.FormatInt(int64(now.Add(periodm).UnixNano()/1e6), 10)
to := strconv.FormatInt(int64(now.UnixNano()/1e6), 10)
exporter.From = from
exporter.To = to

Expand All @@ -176,18 +172,18 @@ func (exporter *BaseHuaweiCloudExporter) Collect(ch chan<- prometheus.Metric) {
}
}

func (exporter *BaseHuaweiCloudExporter) debugMetricInfo(md metricdata.MetricData) {
func (exporter *BaseHuaweiCloudExporter) debugMetricInfo(md metricdata.MetricData) {
if exporter.Debug == true {
fmt.Println("Get datapoints of metric begin... (from):", exporter.From)
dataJson, _ := json.MarshalIndent(md.Datapoints, "", " ")
metricJson, _ := json.MarshalIndent(md.Dimensions, "", " ")
fmt.Println("The datapoints of metric are:" + string(dataJson))
fmt.Println("The metric value is:", string(metricJson))
fmt.Println("Get datapoints of metric end. (to):", exporter.To)
log.Debug("Get datapoints of metric begin... (from):", exporter.From)
dataJSON, _ := json.MarshalIndent(md.Datapoints, "", " ")
metricJSON, _ := json.MarshalIndent(md.Dimensions, "", " ")
log.Debug("The datapoints of metric are:" + string(dataJSON))
log.Debug("The metric value is:", string(metricJSON))
log.Debug("Get datapoints of metric end. (to):", exporter.To)
}
}

func isResouceExist(dims *[]metricdata.Dimension, allResouceInfo *map[string][]string,) (bool) {
func isResouceExist(dims *[]metricdata.Dimension, allResouceInfo *map[string][]string) bool {
for _, dimension := range *dims {
if _, ok := (*allResouceInfo)[dimension.Value]; ok {
return true
Expand All @@ -200,7 +196,7 @@ func isResouceExist(dims *[]metricdata.Dimension, allResouceInfo *map[string][]s
func getDatapoint(datapoints []metricdata.Data) (float64, error) {
var datapoint float64
if len(datapoints) > 0 {
datapoint = (datapoints)[len(datapoints) - 1].Average
datapoint = (datapoints)[len(datapoints)-1].Average
} else {
return 0, fmt.Errorf("The data point of metric are not found")
}
Expand All @@ -211,15 +207,15 @@ func getDatapoint(datapoints []metricdata.Data) (float64, error) {
func getMetricDataTimestamp(datapoints []metricdata.Data) (int, error) {
var metricTimestamp int
if len(datapoints) > 0 {
metricTimestamp = (datapoints)[len(datapoints) - 1].Timestamp
metricTimestamp = (datapoints)[len(datapoints)-1].Timestamp
} else {
return 0, fmt.Errorf("The data point of metric are not found")
}

return metricTimestamp, nil
}

func getOriginalID(dimensions *[]metricdata.Dimension) (string) {
func getOriginalID(dimensions *[]metricdata.Dimension) string {
id := ""

if len(*dimensions) == 1 {
Expand All @@ -231,7 +227,7 @@ func getOriginalID(dimensions *[]metricdata.Dimension) (string) {
return id
}

func getOriginalLabelInfo(dims *[]metricdata.Dimension) ([]string, []string, string, string) {
func getOriginalLabelInfo(dims *[]metricdata.Dimension) ([]string, []string, string, string) {
labels := []string{}
dimensionValues := []string{}
preResourceName := ""
Expand All @@ -253,7 +249,7 @@ func getOriginalLabelInfo(dims *[]metricdata.Dimension) ([]string, []string, str
}

func (exporter *BaseHuaweiCloudExporter) getExtensionLabels(
lables []string, preResourceName string, namespace string, privateFlag string) ([]string) {
lables []string, preResourceName string, namespace string, privateFlag string) []string {

namespace = replaceName(namespace)
if preResourceName != "" {
Expand All @@ -269,13 +265,12 @@ func (exporter *BaseHuaweiCloudExporter) getExtensionLabels(
return newlabels
}


func (exporter *BaseHuaweiCloudExporter) getExtensionLabelValues(
dimensionValues []string,
allResouceInfo *map[string][]string,
originalID string) ([]string) {
originalID string) []string {

for lb := range *allResouceInfo{
for lb := range *allResouceInfo {
if lb == originalID {
dimensionValues = append(dimensionValues, (*allResouceInfo)[lb]...)
return dimensionValues
Expand All @@ -285,8 +280,7 @@ func (exporter *BaseHuaweiCloudExporter) getExtensionLabelValues(
return dimensionValues
}


func initClient(global_config *CloudConfig) (*Config) {
func initClient(global_config *CloudConfig) *Config {
c, err := InitConfig(global_config)
if err != nil {
log.Fatal(err)
Expand Down
15 changes: 7 additions & 8 deletions collector/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ import (
// 1. Added the new labels name to defaultExtensionLabels
// 2. Added the new labels values to getAllResource
var defaultExtensionLabels = map[string][]string{
"sys_elb": []string{"name", "vip_address"},
"sys_elb": []string{"name", "vip_address"},
"sys_elb_listener": []string{"name", "port"},
"sys_nat": []string{"name"},
"sys_rds": []string{"name", "role"},
"sys_dcs": []string{"name", "engine"},
"sys_dms": []string{"name"},
"sys_nat": []string{"name"},
"sys_rds": []string{"name", "role"},
"sys_dcs": []string{"name", "engine"},
"sys_dms": []string{"name"},
// Added extenstion labeles name for each service
// for example:
// "sys_vpc": []string{"name", "cidr"},
}


func (exporter *BaseHuaweiCloudExporter) getAllResource(namespace string) (map[string][]string) {
func (exporter *BaseHuaweiCloudExporter) getAllResource(namespace string) map[string][]string {
resourceInfos := map[string][]string{}
switch namespace {
case "SYS.ELB":
Expand All @@ -55,7 +54,7 @@ func (exporter *BaseHuaweiCloudExporter) getAllResource(namespace string) (map[s
resourceInfos[listener.ID] = values
}
case "SYS.NAT":
allnat, _ := getAllNat(exporter.ClientConfig)
allnat, _ := getAllNat(exporter.ClientConfig)
for _, nat := range *allnat {
values := []string{}
values = append(values, nat.Name)
Expand Down
Loading