瀏覽代碼

cmd/geth: allow branching metric patterns

Péter Szilágyi 10 年之前
父節點
當前提交
302187ae39
共有 1 個文件被更改,包括 25 次插入18 次删除
  1. 25 18
      cmd/geth/monitorcmd.go

+ 25 - 18
cmd/geth/monitorcmd.go

@@ -149,30 +149,37 @@ func resolveMetrics(metrics map[string]interface{}, patterns []string) []string
 // resolveMetrics takes a single of input metric pattern, and resolves it to one
 // or more canonical metric names.
 func resolveMetric(metrics map[string]interface{}, pattern string, path string) []string {
-	var ok bool
-
-	// Build up the canonical metric path
-	parts := strings.Split(pattern, "/")
-	for len(parts) > 1 {
-		if metrics, ok = metrics[parts[0]].(map[string]interface{}); !ok {
-			utils.Fatalf("Failed to retrieve system metrics: %s", path+parts[0])
+	results := []string{}
+
+	// If a nested metric was requested, recurse optionally branching (via comma)
+	parts := strings.SplitN(pattern, "/", 2)
+	if len(parts) > 1 {
+		for _, variation := range strings.Split(parts[0], ",") {
+			if submetrics, ok := metrics[variation].(map[string]interface{}); !ok {
+				utils.Fatalf("Failed to retrieve system metrics: %s", path+variation)
+				return nil
+			} else {
+				results = append(results, resolveMetric(submetrics, parts[1], path+variation+"/")...)
+			}
 		}
-		path += parts[0] + "/"
-		parts = parts[1:]
+		return results
 	}
 	// Depending what the last link is, return or expand
-	switch metric := metrics[parts[0]].(type) {
-	case float64:
-		// Final metric value found, return as singleton
-		return []string{path + parts[0]}
+	for _, variation := range strings.Split(pattern, ",") {
+		switch metric := metrics[variation].(type) {
+		case float64:
+			// Final metric value found, return as singleton
+			results = append(results, path+variation)
 
-	case map[string]interface{}:
-		return expandMetrics(metric, path+parts[0]+"/")
+		case map[string]interface{}:
+			results = append(results, expandMetrics(metric, path+variation+"/")...)
 
-	default:
-		utils.Fatalf("Metric pattern resolved to unexpected type: %v", reflect.TypeOf(metric))
-		return nil
+		default:
+			utils.Fatalf("Metric pattern resolved to unexpected type: %v", reflect.TypeOf(metric))
+			return nil
+		}
 	}
+	return results
 }
 
 // expandMetrics expands the entire tree of metrics into a flat list of paths.