From 302187ae397c5e06a9f086183d55f591f5daf588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Wed, 24 Jun 2015 17:12:38 +0300 Subject: [PATCH] cmd/geth: allow branching metric patterns --- cmd/geth/monitorcmd.go | 43 ++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/cmd/geth/monitorcmd.go b/cmd/geth/monitorcmd.go index 06ccf90be..ec0dfb8f2 100644 --- a/cmd/geth/monitorcmd.go +++ b/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.