@@ -24,12 +24,18 @@ type DashboardMetrics struct {
24
24
UID string `json:"uid,omitempty"`
25
25
Title string `json:"title"`
26
26
Metrics []string `json:"metrics"`
27
+ LabelsByMetric map [string ][]Label `json:"labels_by_metric"`
27
28
ParseErrors []string `json:"parse_errors"`
28
29
}
29
30
31
+ type Label struct {
32
+ Name string `json:"name"`
33
+ Value string `json:"value"`
34
+ }
35
+
30
36
func ParseMetricsInBoard (mig * MetricsInGrafana , board sdk.Board ) {
31
37
var parseErrors []error
32
- metrics := make (map [string ]struct {} )
38
+ metrics := make (map [string ][] Label )
33
39
34
40
// Iterate through all the panels and collect metrics
35
41
for _ , panel := range board .Panels {
@@ -73,11 +79,12 @@ func ParseMetricsInBoard(mig *MetricsInGrafana, board sdk.Board) {
73
79
Title : board .Title ,
74
80
Metrics : metricsInBoard ,
75
81
ParseErrors : parseErrs ,
82
+ LabelsByMetric : metrics ,
76
83
})
77
84
78
85
}
79
86
80
- func metricsFromTemplating (templating sdk.Templating , metrics map [string ]struct {} ) []error {
87
+ func metricsFromTemplating (templating sdk.Templating , metrics map [string ][] Label ) []error {
81
88
parseErrors := []error {}
82
89
for _ , templateVar := range templating .List {
83
90
if templateVar .Type != "query" {
@@ -114,7 +121,7 @@ func metricsFromTemplating(templating sdk.Templating, metrics map[string]struct{
114
121
return parseErrors
115
122
}
116
123
117
- func metricsFromPanel (panel sdk.Panel , metrics map [string ]struct {} ) []error {
124
+ func metricsFromPanel (panel sdk.Panel , metrics map [string ][] Label ) []error {
118
125
var parseErrors []error
119
126
120
127
targets := panel .GetTargets ()
@@ -140,7 +147,7 @@ func metricsFromPanel(panel sdk.Panel, metrics map[string]struct{}) []error {
140
147
return parseErrors
141
148
}
142
149
143
- func parseQuery (query string , metrics map [string ]struct {} ) error {
150
+ func parseQuery (query string , metrics map [string ][] Label ) error {
144
151
query = strings .ReplaceAll (query , `$__interval` , "5m" )
145
152
query = strings .ReplaceAll (query , `$interval` , "5m" )
146
153
query = strings .ReplaceAll (query , `$resolution` , "5s" )
@@ -155,7 +162,15 @@ func parseQuery(query string, metrics map[string]struct{}) error {
155
162
156
163
parser .Inspect (expr , func (node parser.Node , path []parser.Node ) error {
157
164
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
+
159
174
}
160
175
161
176
return nil
0 commit comments