mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Prometheus: Improve autocomplete performance and remove disabling of dynamic label lookup (#30199)
* processLabels: Use Sets instead of Array * Add and update comment * Limit autocomplete items to 10000 * Remove lookup treshold, limit display of items * Update tests * Add test * Update public/app/plugins/datasource/prometheus/language_provider.ts
This commit is contained in:
@@ -11,6 +11,8 @@ import {
|
||||
processHistogramLabels,
|
||||
processLabels,
|
||||
roundSecToMin,
|
||||
addLimitInfo,
|
||||
limitSuggestions,
|
||||
} from './language_utils';
|
||||
import PromqlSyntax, { FUNCTIONS, RATE_RANGES } from './promql';
|
||||
|
||||
@@ -21,7 +23,8 @@ const DEFAULT_KEYS = ['job', 'instance'];
|
||||
const EMPTY_SELECTOR = '{}';
|
||||
const HISTORY_ITEM_COUNT = 5;
|
||||
const HISTORY_COUNT_CUTOFF = 1000 * 60 * 60 * 24; // 24h
|
||||
export const DEFAULT_LOOKUP_METRICS_THRESHOLD = 10000; // number of metrics defining an installation that's too big
|
||||
// Max number of items (metrics, labels, values) that we display as suggestions. Prevents from running out of memory.
|
||||
export const SUGGESTIONS_LIMIT = 10000;
|
||||
|
||||
const wrapLabel = (label: string): CompletionItem => ({ label });
|
||||
|
||||
@@ -66,8 +69,6 @@ export default class PromQlLanguageProvider extends LanguageProvider {
|
||||
metricsMetadata?: PromMetricsMetadata;
|
||||
startTask: Promise<any>;
|
||||
datasource: PrometheusDatasource;
|
||||
lookupMetricsThreshold: number;
|
||||
lookupsDisabled: boolean; // Dynamically set to true for big/slow instances
|
||||
|
||||
/**
|
||||
* Cache for labels of series. This is bit simplistic in the sense that it just counts responses each as a 1 and does
|
||||
@@ -83,9 +84,6 @@ export default class PromQlLanguageProvider extends LanguageProvider {
|
||||
this.histogramMetrics = [];
|
||||
this.timeRange = { start: 0, end: 0 };
|
||||
this.metrics = [];
|
||||
// Disable lookups until we know the instance is small enough
|
||||
this.lookupMetricsThreshold = DEFAULT_LOOKUP_METRICS_THRESHOLD;
|
||||
this.lookupsDisabled = true;
|
||||
|
||||
Object.assign(this, initialValues);
|
||||
}
|
||||
@@ -128,7 +126,6 @@ export default class PromQlLanguageProvider extends LanguageProvider {
|
||||
const url = `/api/v1/label/__name__/values?${params.toString()}`;
|
||||
|
||||
this.metrics = await this.request(url, []);
|
||||
this.lookupsDisabled = this.metrics.length > this.lookupMetricsThreshold;
|
||||
this.metricsMetadata = fixSummariesMetadata(await this.request('/api/v1/metadata', {}));
|
||||
this.processHistogramMetrics(this.metrics);
|
||||
|
||||
@@ -241,9 +238,10 @@ export default class PromQlLanguageProvider extends LanguageProvider {
|
||||
});
|
||||
|
||||
if (metrics && metrics.length) {
|
||||
const limitInfo = addLimitInfo(metrics);
|
||||
suggestions.push({
|
||||
label: 'Metrics',
|
||||
items: metrics.map(m => addMetricsMetadata(m, metricsMetadata)),
|
||||
label: `Metrics${limitInfo}`,
|
||||
items: limitSuggestions(metrics).map(m => addMetricsMetadata(m, metricsMetadata)),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -314,7 +312,11 @@ export default class PromQlLanguageProvider extends LanguageProvider {
|
||||
|
||||
const labelValues = await this.getLabelValues(selector);
|
||||
if (labelValues) {
|
||||
suggestions.push({ label: 'Labels', items: Object.keys(labelValues).map(wrapLabel) });
|
||||
const limitInfo = addLimitInfo(labelValues[0]);
|
||||
suggestions.push({
|
||||
label: `Labels${limitInfo}`,
|
||||
items: Object.keys(labelValues).map(wrapLabel),
|
||||
});
|
||||
}
|
||||
return result;
|
||||
};
|
||||
@@ -376,8 +378,9 @@ export default class PromQlLanguageProvider extends LanguageProvider {
|
||||
// Label values
|
||||
if (labelKey && labelValues[labelKey]) {
|
||||
context = 'context-label-values';
|
||||
const limitInfo = addLimitInfo(labelValues[labelKey]);
|
||||
suggestions.push({
|
||||
label: `Label values for "${labelKey}"`,
|
||||
label: `Label values for "${labelKey}"${limitInfo}`,
|
||||
items: labelValues[labelKey].map(wrapLabel),
|
||||
});
|
||||
}
|
||||
@@ -390,7 +393,8 @@ export default class PromQlLanguageProvider extends LanguageProvider {
|
||||
if (possibleKeys.length) {
|
||||
context = 'context-labels';
|
||||
const newItems = possibleKeys.map(key => ({ label: key }));
|
||||
const newSuggestion: CompletionItemGroup = { label: `Labels`, items: newItems };
|
||||
const limitInfo = addLimitInfo(newItems);
|
||||
const newSuggestion: CompletionItemGroup = { label: `Labels${limitInfo}`, items: newItems };
|
||||
suggestions.push(newSuggestion);
|
||||
}
|
||||
}
|
||||
@@ -400,7 +404,7 @@ export default class PromQlLanguageProvider extends LanguageProvider {
|
||||
};
|
||||
|
||||
async getLabelValues(selector: string, withName?: boolean) {
|
||||
if (this.lookupsDisabled) {
|
||||
if (this.datasource.lookupsDisabled) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user