Skip to content

Commit 900ef7b

Browse files
committed
List used labels by metric in grafana
1 parent 9403069 commit 900ef7b

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

pkg/analyse/grafana.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@ type DashboardMetrics struct {
2424
UID string `json:"uid,omitempty"`
2525
Title string `json:"title"`
2626
Metrics []string `json:"metrics"`
27+
LabelsByMetric map[string][]Label `json:"labels_by_metric"`
2728
ParseErrors []string `json:"parse_errors"`
2829
}
2930

31+
type Label struct {
32+
Name string
33+
Value string
34+
}
35+
3036
func ParseMetricsInBoard(mig *MetricsInGrafana, board sdk.Board) {
3137
var parseErrors []error
32-
metrics := make(map[string]struct{})
38+
metrics := make(map[string][]Label)
3339

3440
// Iterate through all the panels and collect metrics
3541
for _, panel := range board.Panels {
@@ -73,11 +79,12 @@ func ParseMetricsInBoard(mig *MetricsInGrafana, board sdk.Board) {
7379
Title: board.Title,
7480
Metrics: metricsInBoard,
7581
ParseErrors: parseErrs,
82+
LabelsByMetric: metrics,
7683
})
7784

7885
}
7986

80-
func metricsFromTemplating(templating sdk.Templating, metrics map[string]struct{}) []error {
87+
func metricsFromTemplating(templating sdk.Templating, metrics map[string][]Label) []error {
8188
parseErrors := []error{}
8289
for _, templateVar := range templating.List {
8390
if templateVar.Type != "query" {
@@ -114,7 +121,7 @@ func metricsFromTemplating(templating sdk.Templating, metrics map[string]struct{
114121
return parseErrors
115122
}
116123

117-
func metricsFromPanel(panel sdk.Panel, metrics map[string]struct{}) []error {
124+
func metricsFromPanel(panel sdk.Panel, metrics map[string][]Label) []error {
118125
var parseErrors []error
119126

120127
targets := panel.GetTargets()
@@ -140,7 +147,7 @@ func metricsFromPanel(panel sdk.Panel, metrics map[string]struct{}) []error {
140147
return parseErrors
141148
}
142149

143-
func parseQuery(query string, metrics map[string]struct{}) error {
150+
func parseQuery(query string, metrics map[string][]Label) error {
144151
query = strings.ReplaceAll(query, `$__interval`, "5m")
145152
query = strings.ReplaceAll(query, `$interval`, "5m")
146153
query = strings.ReplaceAll(query, `$resolution`, "5s")
@@ -155,7 +162,15 @@ func parseQuery(query string, metrics map[string]struct{}) error {
155162

156163
parser.Inspect(expr, func(node parser.Node, path []parser.Node) error {
157164
if n, ok := node.(*parser.VectorSelector); ok {
158-
metrics[n.Name] = struct{}{}
165+
var labels []Label
166+
for _, l := range n.LabelMatchers {
167+
labels = append(
168+
labels,
169+
Label{Name: l.Name, Value: l.Value},
170+
)
171+
}
172+
metrics[n.Name] = labels
173+
159174
}
160175

161176
return nil

0 commit comments

Comments
 (0)