forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery_hints.ts
372 lines (336 loc) · 11 KB
/
query_hints.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Core Grafana history https://github.com/grafana/grafana/blob/v11.0.0-preview/public/app/plugins/datasource/prometheus/query_hints.ts
import { size } from 'lodash';
import { QueryFix, QueryHint } from '@grafana/data';
import { PrometheusDatasource } from './datasource';
import { buildVisualQueryFromString } from './querybuilder/parsing';
import { QueryBuilderLabelFilter } from './querybuilder/shared/types';
import { PromVisualQuery } from './querybuilder/types';
import { PromMetricsMetadata, RecordingRuleIdentifier, RuleQueryMapping } from './types';
/**
* Number of time series results needed before starting to suggest sum aggregation hints
*/
export const SUM_HINT_THRESHOLD_COUNT = 20;
export function getQueryHints(query: string, series?: unknown[], datasource?: PrometheusDatasource): QueryHint[] {
const hints = [];
const metricsMetadata = datasource?.languageProvider?.metricsMetadata;
// ..._bucket metric needs a histogram_quantile()
// this regex also prevents hints from being shown when a query already has a function
const oldHistogramMetric = query.trim().match(/^\w+_bucket$|^\w+_bucket{.*}$/);
if (oldHistogramMetric) {
const label = 'Selected metric has buckets.';
hints.push({
type: 'HISTOGRAM_QUANTILE',
label,
fix: {
label: 'Consider calculating aggregated quantile by adding histogram_quantile().',
action: {
type: 'ADD_HISTOGRAM_QUANTILE',
query,
},
},
});
} else if (metricsMetadata && simpleQueryCheck(query)) {
// having migrated to native histograms
// there will be no more old histograms (no buckets)
// and we can identify a native histogram by the following
// type === 'histogram'
// metric name does not include '_bucket'
const queryTokens = getQueryTokens(query);
// Determine whether any of the query identifier tokens refers to a native histogram metric
const { nameMetric } = checkMetricType(queryTokens, 'histogram', metricsMetadata, false);
const nativeHistogramNameMetric = nameMetric;
if (nativeHistogramNameMetric) {
// add hints:
// histogram_avg, histogram_count, histogram_sum, histogram_fraction, histogram_stddev, histogram_stdvar
const label = 'Selected metric is a native histogram.';
hints.push(
{
type: 'HISTOGRAM_AVG',
label,
fix: {
label: 'Consider calculating the arithmetic average of observed values by adding histogram_avg().',
action: {
type: 'ADD_HISTOGRAM_AVG',
query,
},
},
},
{
type: 'HISTOGRAM_COUNT',
label,
fix: {
label: 'Consider calculating the count of observations by adding histogram_count().',
action: {
type: 'ADD_HISTOGRAM_COUNT',
query,
},
},
},
{
type: 'HISTOGRAM_SUM',
label,
fix: {
label: 'Consider calculating the sum of observations by adding histogram_sum().',
action: {
type: 'ADD_HISTOGRAM_SUM',
query,
},
},
},
{
type: 'HISTOGRAM_FRACTION',
label,
fix: {
label:
'Consider calculating the estimated fraction of observations between the provided lower and upper values by adding histogram_fraction().',
action: {
type: 'ADD_HISTOGRAM_FRACTION',
query,
},
},
},
{
type: 'HISTOGRAM_STDDEV',
label,
fix: {
label:
'Consider calculating the estimated standard deviation of observations by adding histogram_stddev().',
action: {
type: 'ADD_HISTOGRAM_STDDEV',
query,
},
},
},
{
type: 'HISTOGRAM_STDVAR',
label,
fix: {
label: 'Consider calculating the estimated standard variance of observations by adding histogram_stdvar().',
action: {
type: 'ADD_HISTOGRAM_STDVAR',
query,
},
},
}
);
}
}
// Check for need of rate()
if (query.indexOf('rate(') === -1 && query.indexOf('increase(') === -1) {
// Use metric metadata for exact types
const nameMatch = query.match(/\b((?<!:)\w+_(total|sum|count)(?!:))\b/);
let counterNameMetric = nameMatch ? nameMatch[1] : '';
let certain = false;
if (metricsMetadata) {
// Tokenize the query into its identifiers (see https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels)
const queryTokens = getQueryTokens(query);
// Determine whether any of the query identifier tokens refers to a counter metric
const metricTypeChecked = checkMetricType(queryTokens, 'counter', metricsMetadata, certain);
counterNameMetric = metricTypeChecked.nameMetric;
certain = metricTypeChecked.certain;
}
if (counterNameMetric) {
// FixableQuery consists of metric name and optionally label-value pairs. We are not offering fix for complex queries yet.
const fixableQuery = simpleQueryCheck(query);
const verb = certain ? 'is' : 'looks like';
let label = `Selected metric ${verb} a counter.`;
let fix: QueryFix | undefined;
if (fixableQuery) {
fix = {
label: 'Consider calculating rate of counter by adding rate().',
action: {
type: 'ADD_RATE',
query,
},
};
} else {
label = `${label} Consider calculating rate of counter by adding rate().`;
}
hints.push({
type: 'APPLY_RATE',
label,
fix,
});
}
}
// Check for recording rules expansion
if (datasource && datasource.ruleMappings) {
const expandQueryHints = getExpandRulesHints(query, datasource.ruleMappings);
hints.push(...expandQueryHints);
}
if (series && series.length >= SUM_HINT_THRESHOLD_COUNT) {
const simpleMetric = query.trim().match(/^\w+$/);
if (simpleMetric) {
hints.push({
type: 'ADD_SUM',
label: 'Many time series results returned.',
fix: {
label: 'Consider aggregating with sum().',
action: {
type: 'ADD_SUM',
query: query,
preventSubmit: true,
},
},
});
}
}
return hints;
}
export function getInitHints(datasource: PrometheusDatasource): QueryHint[] {
const hints = [];
// Hint for big disabled lookups
if (datasource.lookupsDisabled) {
hints.push({
label: `Labels and metrics lookup was disabled in data source settings.`,
type: 'INFO',
});
}
return hints;
}
export function getExpandRulesHints(query: string, mapping: RuleQueryMapping): QueryHint[] {
const hints: QueryHint[] = [];
const mappingForQuery = Object.keys(mapping).reduce((acc, ruleName) => {
if (query.search(ruleName) === -1) {
return acc;
}
if (mapping[ruleName].length > 1) {
const { idx, expandedQuery, identifier, identifierValue } = getRecordingRuleIdentifierIdx(
query,
ruleName,
mapping[ruleName]
);
// No identifier detected add warning
if (idx === -1) {
hints.push({
type: 'EXPAND_RULES_WARNING',
label:
'We found multiple recording rules that match in this query. To expand the recording rule, add an identifier label/value.',
});
return acc;
} else {
// Identifier found.
return {
...acc,
[ruleName]: {
expandedQuery,
identifier,
identifierValue,
},
};
}
} else {
return {
...acc,
[ruleName]: {
expandedQuery: mapping[ruleName][0].query,
},
};
}
}, {});
if (size(mappingForQuery) > 0) {
const label = 'Query contains recording rules.';
hints.push({
type: 'EXPAND_RULES',
label,
fix: {
label: 'Expand rules',
action: {
type: 'EXPAND_RULES',
query,
options: mappingForQuery,
},
},
});
}
return hints;
}
export function getRecordingRuleIdentifierIdx(
queryStr: string,
ruleName: string,
mapping: RuleQueryMapping[string]
): RecordingRuleIdentifier & { idx: number } {
const { query } = buildVisualQueryFromString(queryStr);
const queryMetricLabels: QueryBuilderLabelFilter[] = getQueryLabelsForRuleName(ruleName, query);
if (queryMetricLabels.length === 0) {
return { idx: -1, identifier: '', identifierValue: '', expandedQuery: '' };
}
let uuidLabel = '';
let uuidLabelValue = '';
let uuidLabelIdx = -1;
queryMetricLabels.forEach((qml) => {
if (uuidLabelIdx === -1 && qml.label.search('uuid') !== -1) {
uuidLabel = qml.label;
uuidLabelValue = qml.value;
}
});
mapping.forEach((mp, idx) => {
if (mp.labels) {
Object.entries(mp.labels).forEach(([key, value]) => {
if (uuidLabelIdx === -1 && key === uuidLabel && value === uuidLabelValue) {
uuidLabelIdx = idx;
}
});
}
});
return {
idx: uuidLabelIdx,
identifier: uuidLabel,
identifierValue: uuidLabelValue,
expandedQuery: mapping[uuidLabelIdx]?.query ?? '',
};
}
// returns the labels of matching metric
// metricName is the ruleName in query
export function getQueryLabelsForRuleName(metricName: string, query: PromVisualQuery): QueryBuilderLabelFilter[] {
if (query.metric === metricName) {
return query.labels;
} else {
if (query.binaryQueries) {
for (let i = 0; i < query.binaryQueries.length; i++) {
const labels = getQueryLabelsForRuleName(metricName, query.binaryQueries[i].query);
if (labels && labels.length > 0) {
return labels;
}
}
}
return [];
}
}
function getQueryTokens(query: string) {
return (
Array.from(query.matchAll(/\$?[a-zA-Z_:][a-zA-Z0-9_:]*/g))
.map(([match]) => match)
// Exclude variable identifiers
.filter((token) => !token.startsWith('$'))
// Split composite keys to match the tokens returned by the language provider
.flatMap((token) => token.split(':'))
);
}
function checkMetricType(
queryTokens: string[],
metricType: string,
metricsMetadata: PromMetricsMetadata,
certain: boolean
) {
// update certain to change language for counters
const nameMetric =
queryTokens.find((metricName) => {
// Only considering first type information, could be non-deterministic
const metadata = metricsMetadata[metricName];
if (metadata && metadata.type.toLowerCase() === metricType) {
certain = true;
return true;
} else {
return false;
}
}) ?? '';
return { nameMetric, certain };
}
/**
* This regex check looks for only metric name and label filters.
* This prevents hints from being shown when a query already has a functions or is complex.
* */
function simpleQueryCheck(query: string) {
return query.trim().match(/^\w+$|^\w+{.*}$/);
}