diff --git a/exporter/convert.go b/exporter/convert.go index 4d865ac..4d11446 100644 --- a/exporter/convert.go +++ b/exporter/convert.go @@ -4,29 +4,34 @@ import ( "strings" ) -type MetricValue struct { +type BaseMetric struct { + Name string Value string Labels map[string]string } -func convertToMap(metrics []string) map[string]MetricValue { - matches := make(map[string]MetricValue, len(metrics)) +func convertToMetrics(originMetrics []string) []BaseMetric { + metrics := make([]BaseMetric, len(originMetrics)) /* match metric format slow_query_latency_us.p95.5=0 slow_query_latency_us{space=nba}.p95.5=0 */ - for _, metric := range metrics { - metric, label := splitMetric(metric) + for _, origin := range originMetrics { + metric, label := splitMetric(origin) s := strings.Split(metric, "=") if len(s) != 2 { continue } - matches[s[0]] = MetricValue{Value: s[1], Labels: label} + metrics = append(metrics, BaseMetric{ + Name: s[0], + Value: s[1], + Labels: label, + }) } - return matches + return metrics } // split slow_query_latency_us{space=nba}.p95.5=0 => slow_query_latency_us.p95.5=0, map[space:nba] diff --git a/exporter/exporter.go b/exporter/exporter.go index 03aa9fa..1b1f577 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -104,15 +104,15 @@ func (exporter *NebulaExporter) CollectMetrics( componentType string, namespace string, cluster string, - metrics []string, + originMetrics []string, ch chan<- prometheus.Metric) { - if len(metrics) == 0 { + if len(originMetrics) == 0 { return } - matches := convertToMap(metrics) - for metric, value := range matches { - v, err := strconv.ParseFloat(value.Value, 64) + metrics := convertToMetrics(originMetrics) + for _, matric := range metrics { + v, err := strconv.ParseFloat(matric.Value, 64) if err != nil { continue } @@ -125,14 +125,14 @@ func (exporter *NebulaExporter) CollectMetrics( labelValues = append(labelValues, namespace) } - for key, value := range value.Labels { + for key, value := range matric.Labels { labels = append(labels, key) labelValues = append(labelValues, value) } ch <- prometheus.MustNewConstMetric( prometheus.NewDesc( - fmt.Sprintf("%s_%s_%s", FQNamespace, componentType, strings.ReplaceAll(metric, ".", "_")), + fmt.Sprintf("%s_%s_%s", FQNamespace, componentType, strings.ReplaceAll(matric.Name, ".", "_")), "", labels, nil, @@ -169,12 +169,11 @@ func (exporter *NebulaExporter) collect(wg *sync.WaitGroup, namespace, clusterNa go func() { defer wg.Done() - statusMetrics, err := getNebulaComponentStatus(podIpAddress, podHttpPort) - if err != nil { - klog.Errorf("get status metrics from %s:%d failed: %v", podIpAddress, podHttpPort, err) - return + statusMetrics := "count=1" + if !isNebulaComponentRunning(podIpAddress, podHttpPort) { + statusMetrics = "count=0" } - exporter.CollectMetrics(instance.Name, instance.ComponentType, namespace, clusterName, statusMetrics, ch) + exporter.CollectMetrics(instance.Name, instance.ComponentType, namespace, clusterName, []string{statusMetrics}, ch) }() } diff --git a/exporter/metric.go b/exporter/metric.go index fd1b607..9babee8 100644 --- a/exporter/metric.go +++ b/exporter/metric.go @@ -30,20 +30,20 @@ func getNebulaMetrics(ipAddress string, port int32) ([]string, error) { return metrics, nil } -func getNebulaComponentStatus(ipAddress string, port int32) ([]string, error) { +func isNebulaComponentRunning(ipAddress string, port int32) bool { httpClient := http.Client{ Timeout: time.Second * 2, } resp, err := httpClient.Get(fmt.Sprintf("http://%s:%d/status", ipAddress, port)) if err != nil { - return nil, err + return false } defer resp.Body.Close() bytes, err := ioutil.ReadAll(resp.Body) if err != nil { - return nil, err + return false } type nebulaStatus struct { @@ -53,14 +53,8 @@ func getNebulaComponentStatus(ipAddress string, port int32) ([]string, error) { var status nebulaStatus if err := json.Unmarshal(bytes, &status); err != nil { - return nil, err + return false } - count := 0 - if status.Status == "running" { - count = 1 - } - statusMetrics := []string{fmt.Sprintf("count=%d", count)} - - return statusMetrics, nil + return status.Status == "running" }