Skip to content

Commit

Permalink
fix status count 0 (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
kqzh authored Jan 17, 2022
1 parent c2a7c56 commit 705b9d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
19 changes: 12 additions & 7 deletions exporter/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
23 changes: 11 additions & 12 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}()
}

Expand Down
16 changes: 5 additions & 11 deletions exporter/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"
}

0 comments on commit 705b9d2

Please sign in to comment.