@@ -11,27 +11,33 @@ function (_) {
11
11
}
12
12
13
13
PrometheusMetricFindQuery . prototype . process = function ( ) {
14
- var label_values_regex = / ^ l a b e l _ v a l u e s \( (?: ( .+ ) , \s * ) ? ( [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] + ) \) $ / ;
15
- var metric_names_regex = / ^ m e t r i c s \( ( .+ ) \) $ / ;
16
- var query_result_regex = / ^ q u e r y _ r e s u l t \( ( .+ ) \) $ / ;
17
-
18
- var label_values_query = this . query . match ( label_values_regex ) ;
19
- if ( label_values_query ) {
20
- if ( label_values_query [ 1 ] ) {
21
- return this . labelValuesQuery ( label_values_query [ 2 ] , label_values_query [ 1 ] ) ;
14
+ var labelValuesRegex = / ^ l a b e l _ v a l u e s \( (?: ( .+ ) , \s * ) ? ( [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] + ) \) $ / ;
15
+ var metricNamesRegex = / ^ m e t r i c s \( ( .+ ) \) $ / ;
16
+ var labelsRegex = / ^ l a b e l s \( ( .+ ) \) $ / ;
17
+ var queryResultRegex = / ^ q u e r y _ r e s u l t \( ( .+ ) \) $ / ;
18
+
19
+ var labelsQuery = this . query . match ( labelsRegex ) ;
20
+ if ( labelsQuery ) {
21
+ return this . labelsQuery ( labelsQuery [ 1 ] ) ;
22
+ }
23
+
24
+ var labelValuesQuery = this . query . match ( labelValuesRegex ) ;
25
+ if ( labelValuesQuery ) {
26
+ if ( labelValuesQuery [ 1 ] ) {
27
+ return this . labelValuesQuery ( labelValuesQuery [ 2 ] , labelValuesQuery [ 1 ] ) ;
22
28
} else {
23
- return this . labelValuesQuery ( label_values_query [ 2 ] , null ) ;
29
+ return this . labelValuesQuery ( labelValuesQuery [ 2 ] , null ) ;
24
30
}
25
31
}
26
32
27
- var metric_names_query = this . query . match ( metric_names_regex ) ;
28
- if ( metric_names_query ) {
29
- return this . metricNameQuery ( metric_names_query [ 1 ] ) ;
33
+ var metricNamesQuery = this . query . match ( metricNamesRegex ) ;
34
+ if ( metricNamesQuery ) {
35
+ return this . metricNameQuery ( metricNamesQuery [ 1 ] ) ;
30
36
}
31
37
32
- var query_result_query = this . query . match ( query_result_regex ) ;
33
- if ( query_result_query ) {
34
- return this . queryResultQuery ( query_result_query [ 1 ] ) ;
38
+ var queryResultQuery = this . query . match ( queryResultRegex ) ;
39
+ if ( queryResultQuery ) {
40
+ return this . queryResultQuery ( queryResultQuery [ 1 ] ) ;
35
41
}
36
42
37
43
// if query contains full metric name, return metric name and label list
@@ -67,45 +73,71 @@ function (_) {
67
73
}
68
74
} ;
69
75
76
+ PrometheusMetricFindQuery . prototype . labelsQuery = function ( metric ) {
77
+ var url ;
78
+
79
+ url = '/api/v1/series?match[]=' + encodeURIComponent ( metric )
80
+ + '&start=' + ( this . range . from . valueOf ( ) / 1000 )
81
+ + '&end=' + ( this . range . to . valueOf ( ) / 1000 ) ;
82
+
83
+ return this . datasource . _request ( 'GET' , url )
84
+ . then ( function ( result ) {
85
+ var tags = { } ;
86
+ _ . each ( result . data . data , function ( metric ) {
87
+ _ . each ( metric , function ( value , key ) {
88
+ if ( key === "__name__" ) {
89
+ return ;
90
+ }
91
+
92
+ tags [ key ] = key ;
93
+ } ) ;
94
+ } ) ;
95
+
96
+ return _ . map ( tags , function ( value ) {
97
+ return { text : value , value : value } ;
98
+ } ) ;
99
+ } ) ;
100
+ } ;
101
+
70
102
PrometheusMetricFindQuery . prototype . metricNameQuery = function ( metricFilterPattern ) {
71
103
var url = '/api/v1/label/__name__/values' ;
72
104
73
105
return this . datasource . _request ( 'GET' , url )
74
- . then ( function ( result ) {
75
- return _ . chain ( result . data . data )
76
- . filter ( function ( metricName ) {
77
- var r = new RegExp ( metricFilterPattern ) ;
78
- return r . test ( metricName ) ;
79
- } )
80
- . map ( function ( matchedMetricName ) {
81
- return {
82
- text : matchedMetricName ,
83
- expandable : true
84
- } ;
85
- } )
86
- . value ( ) ;
87
- } ) ;
106
+ . then ( function ( result ) {
107
+ return _ . chain ( result . data . data )
108
+ . filter ( function ( metricName ) {
109
+ var r = new RegExp ( metricFilterPattern ) ;
110
+ return r . test ( metricName ) ;
111
+ } )
112
+ . map ( function ( matchedMetricName ) {
113
+ return {
114
+ text : matchedMetricName ,
115
+ expandable : true
116
+ } ;
117
+ } )
118
+ . value ( ) ;
119
+ } ) ;
88
120
} ;
89
121
90
122
PrometheusMetricFindQuery . prototype . queryResultQuery = function ( query ) {
91
123
var url = '/api/v1/query?query=' + encodeURIComponent ( query ) + '&time=' + ( this . range . to . valueOf ( ) / 1000 ) ;
92
124
93
125
return this . datasource . _request ( 'GET' , url )
94
- . then ( function ( result ) {
95
- return _ . map ( result . data . data . result , function ( metricData ) {
96
- var text = metricData . metric . __name__ || '' ;
97
- delete metricData . metric . __name__ ;
98
- text += '{' +
99
- _ . map ( metricData . metric , function ( v , k ) { return k + '="' + v + '"' ; } ) . join ( ',' ) +
100
- '}' ;
101
- text += ' ' + metricData . value [ 1 ] + ' ' + metricData . value [ 0 ] * 1000 ;
102
-
103
- return {
104
- text : text ,
105
- expandable : true
106
- } ;
126
+ . then ( function ( result ) {
127
+ return _ . map ( result . data . data . result , function ( metricData ) {
128
+ var text = metricData . metric . __name__ || '' ;
129
+ delete metricData . metric . __name__ ;
130
+ text += '{' +
131
+ _ . map ( metricData . metric , function ( v , k ) { return k + '="' + v + '"' ; } ) . join ( ',' ) +
132
+ '}' ;
133
+ text += ' ' + metricData . value [ 1 ] + ' ' + metricData . value [ 0 ] * 1000 ;
134
+
135
+ return {
136
+ text : text ,
137
+ expandable : true
138
+ } ;
139
+ } ) ;
107
140
} ) ;
108
- } ) ;
109
141
} ;
110
142
111
143
PrometheusMetricFindQuery . prototype . metricNameAndLabelsQuery = function ( query ) {
@@ -115,14 +147,14 @@ function (_) {
115
147
116
148
var self = this ;
117
149
return this . datasource . _request ( 'GET' , url )
118
- . then ( function ( result ) {
119
- return _ . map ( result . data . data , function ( metric ) {
120
- return {
121
- text : self . datasource . getOriginalMetricName ( metric ) ,
122
- expandable : true
123
- } ;
150
+ . then ( function ( result ) {
151
+ return _ . map ( result . data . data , function ( metric ) {
152
+ return {
153
+ text : self . datasource . getOriginalMetricName ( metric ) ,
154
+ expandable : true
155
+ } ;
156
+ } ) ;
124
157
} ) ;
125
- } ) ;
126
158
} ;
127
159
128
160
return PrometheusMetricFindQuery ;
0 commit comments