diff --git a/go.mod b/go.mod index 5e6e50f..c22b0d3 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,6 @@ require ( github.com/prometheus/client_golang v1.9.0 github.com/prometheus/procfs v0.6.0 // indirect github.com/sirupsen/logrus v1.7.0 - golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/protobuf v1.25.0 // indirect gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index d398f68..cbf4e4a 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -github.com/Azure/azure-sdk-for-go v50.2.0+incompatible h1:w9EF1btRfLLWbNEp6XvkMjeA6nQ3e1GZ2KNDqB/SjOQ= -github.com/Azure/azure-sdk-for-go v50.2.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/azure-sdk-for-go v51.1.0+incompatible h1:7uk6GWtUqKg6weLv2dbKnzwb0ml1Qn70AdtRccZ543w= github.com/Azure/azure-sdk-for-go v51.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs= @@ -11,13 +9,9 @@ github.com/Azure/go-autorest/autorest v0.11.17/go.mod h1:eipySxLmqSyC5s5k1CLupqe github.com/Azure/go-autorest/autorest v0.11.18 h1:90Y4srNYrwOtAgVo3ndrQkTYn6kf1Eg/AjTFJ8Is2aM= github.com/Azure/go-autorest/autorest v0.11.18/go.mod h1:dSiJPy22c3u0OtOKDNttNgqpNFY/GeWa7GH/Pz56QRA= github.com/Azure/go-autorest/autorest/adal v0.9.5/go.mod h1:B7KF7jKIeC9Mct5spmyCB/A8CG/sEz1vwIRGv/bbw7A= -github.com/Azure/go-autorest/autorest/adal v0.9.10 h1:r6fZHMaHD8B6LDCn0o5vyBFHIHrM6Ywwx7mb49lPItI= -github.com/Azure/go-autorest/autorest/adal v0.9.10/go.mod h1:B7KF7jKIeC9Mct5spmyCB/A8CG/sEz1vwIRGv/bbw7A= github.com/Azure/go-autorest/autorest/adal v0.9.11/go.mod h1:nBKAnTomx8gDtl+3ZCJv2v0KACFHWTB2drffI1B68Pk= github.com/Azure/go-autorest/autorest/adal v0.9.13 h1:Mp5hbtOePIzM8pJVRa3YLrWWmZtoxRXqUEzCfJt3+/Q= github.com/Azure/go-autorest/autorest/adal v0.9.13/go.mod h1:W/MM4U6nLxnIskrw4UwWzlHfGjwUS50aOsc/I3yuU8M= -github.com/Azure/go-autorest/autorest/azure/auth v0.5.6 h1:cgiBtUxatlt/e3qY6fQJioqbocWHr5osz259MomF5M0= -github.com/Azure/go-autorest/autorest/azure/auth v0.5.6/go.mod h1:nYlP+G+n8MhD5CjIi6W8nFTIJn/PnTHes5nUbK6BxD0= github.com/Azure/go-autorest/autorest/azure/auth v0.5.7 h1:8DQB8yl7aLQuP+nuR5e2RO6454OvFlSTXXaNHshc16s= github.com/Azure/go-autorest/autorest/azure/auth v0.5.7/go.mod h1:AkzUsqkrdmNhfP2i54HqINVQopw0CLDnvHpJ88Zz1eI= github.com/Azure/go-autorest/autorest/azure/cli v0.4.2 h1:dMOmEJfkLKW/7JsokJqkyoYSgmR08hi9KrhjZb+JALY= @@ -133,8 +127,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k= -github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= @@ -282,8 +275,6 @@ github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+Gx github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.2.0 h1:wH4vA7pcjKuZzjF7lM8awk4fnuJO6idemZXoKnULUx4= github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= -github.com/prometheus/procfs v0.3.0 h1:Uehi/mxLK0eiUc0H0++5tpMGTexB8wZ598MIgU8VpDM= -github.com/prometheus/procfs v0.3.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= diff --git a/probe.go b/probe.go index a3c6f31..ee8a294 100644 --- a/probe.go +++ b/probe.go @@ -170,21 +170,22 @@ func respondDecorator() autorest.RespondDecorator { func buildPrometheusMetricList(name string, metricConfig config.ConfigQueryMetric, row map[string]interface{}) (list map[string][]MetricRow) { list = map[string][]MetricRow{} - - fieldConfigMap := metricConfig.GetFieldConfigMap() + idFieldList := map[string]string{} mainMetrics := map[string]*MetricRow{} mainMetrics[name] = NewMetricRow() + fieldConfigMap := metricConfig.GetFieldConfigMap() + + // add default value to main metric (if set) if metricConfig.Value != nil { mainMetrics[name].Value = *metricConfig.Value } - idFieldList := map[string]string{} - // main metric for fieldName, rowValue := range row { if fieldConfList, ok := fieldConfigMap[fieldName]; ok { + // field configuration available for _, fieldConfig := range fieldConfList { if fieldConfig.IsTypeIgnore() { continue @@ -202,7 +203,7 @@ func buildPrometheusMetricList(name string, metricConfig config.ConfigQueryMetri fieldConfig.Metric = name } - processField(fieldName, rowValue, fieldConfig, mainMetrics[fieldConfig.Metric]) + processFieldAndAddToMetric(fieldName, rowValue, fieldConfig, mainMetrics[fieldConfig.Metric]) if fieldConfig.IsTypeId() { labelName := fieldConfig.GetTargetFieldName(fieldName) @@ -212,9 +213,10 @@ func buildPrometheusMetricList(name string, metricConfig config.ConfigQueryMetri } } } else { + // no field config, fall back to "defaultField" fieldConfig := metricConfig.DefaultField if !fieldConfig.IsTypeIgnore() { - processField(fieldName, rowValue, fieldConfig, mainMetrics[name]) + processFieldAndAddToMetric(fieldName, rowValue, fieldConfig, mainMetrics[name]) } } } @@ -225,16 +227,7 @@ func buildPrometheusMetricList(name string, metricConfig config.ConfigQueryMetri continue } - // convert to array even if not array - rowValueList := []interface{}{} - switch v := rowValue.(type) { - case map[string]interface{}: - rowValueList = append(rowValueList, v) - case []interface{}: - rowValueList = v - } - - for _, rowValue := range rowValueList { + for _, rowValue := range convertSubMetricInterfaceToArray(rowValue) { if v, ok := rowValue.(map[string]interface{}); ok { if fieldConfList, ok := fieldConfigMap[fieldName]; ok { for _, fieldConfig := range fieldConfList { @@ -242,6 +235,7 @@ func buildPrometheusMetricList(name string, metricConfig config.ConfigQueryMetri continue } + // add fieldname to metric if no custom metric is set if fieldConfig.Metric == "" { fieldConfig.Metric = fmt.Sprintf("%s_%s", name, fieldName) } @@ -257,7 +251,6 @@ func buildPrometheusMetricList(name string, metricConfig config.ConfigQueryMetri if _, ok := list[subMetricName]; !ok { list[subMetricName] = []MetricRow{} } - list[subMetricName] = append(list[subMetricName], subMetricList...) } } @@ -272,7 +265,6 @@ func buildPrometheusMetricList(name string, metricConfig config.ConfigQueryMetri list[metricName] = []MetricRow{} } list[metricName] = append(list[metricName], *metricRow) - } // add id labels @@ -286,48 +278,3 @@ func buildPrometheusMetricList(name string, metricConfig config.ConfigQueryMetri return } - -func processField(fieldName string, value interface{}, fieldConfig config.ConfigQueryMetricField, metric *MetricRow) { - labelName := fieldConfig.GetTargetFieldName(fieldName) - - switch v := value.(type) { - case string: - v = fieldConfig.TransformString(v) - if fieldConfig.IsTypeValue() { - if value, err := strconv.ParseFloat(v, 64); err == nil { - metric.Value = value - } else { - metric.Value = 0 - } - } else { - metric.Labels[labelName] = v - } - case int64: - fieldValue := fieldConfig.TransformFloat64(float64(v)) - - if fieldConfig.IsTypeValue() { - metric.Value = float64(v) - } else { - metric.Labels[labelName] = fieldValue - } - case float64: - fieldValue := fieldConfig.TransformFloat64(v) - - if fieldConfig.IsTypeValue() { - metric.Value = v - } else { - metric.Labels[labelName] = fieldValue - } - case bool: - fieldValue := fieldConfig.TransformBool(v) - if fieldConfig.IsTypeValue() { - if v { - metric.Value = 1 - } else { - metric.Value = 0 - } - } else { - metric.Labels[labelName] = fieldValue - } - } -} diff --git a/process.go b/process.go new file mode 100644 index 0000000..3f771da --- /dev/null +++ b/process.go @@ -0,0 +1,64 @@ +package main + +import ( + "github.com/webdevops/azure-resourcegraph-exporter/config" + "strconv" +) + +func convertSubMetricInterfaceToArray(val interface{}) []interface{} { + // convert to array even if not array + ret := []interface{}{} + switch v := val.(type) { + case map[string]interface{}: + ret = append(ret, v) + case []interface{}: + ret = v + } + + return ret +} + +func processFieldAndAddToMetric(fieldName string, value interface{}, fieldConfig config.ConfigQueryMetricField, metric *MetricRow) { + labelName := fieldConfig.GetTargetFieldName(fieldName) + + switch v := value.(type) { + case string: + v = fieldConfig.TransformString(v) + if fieldConfig.IsTypeValue() { + if value, err := strconv.ParseFloat(v, 64); err == nil { + metric.Value = value + } else { + metric.Value = 0 + } + } else { + metric.Labels[labelName] = v + } + case int64: + fieldValue := fieldConfig.TransformFloat64(float64(v)) + + if fieldConfig.IsTypeValue() { + metric.Value = float64(v) + } else { + metric.Labels[labelName] = fieldValue + } + case float64: + fieldValue := fieldConfig.TransformFloat64(v) + + if fieldConfig.IsTypeValue() { + metric.Value = v + } else { + metric.Labels[labelName] = fieldValue + } + case bool: + fieldValue := fieldConfig.TransformBool(v) + if fieldConfig.IsTypeValue() { + if v { + metric.Value = 1 + } else { + metric.Value = 0 + } + } else { + metric.Labels[labelName] = fieldValue + } + } +}