diff --git a/public/app/plugins/datasource/loki/components/LokiQueryField.tsx b/public/app/plugins/datasource/loki/components/LokiQueryField.tsx index a8cf38d06bc..37946b487ce 100644 --- a/public/app/plugins/datasource/loki/components/LokiQueryField.tsx +++ b/public/app/plugins/datasource/loki/components/LokiQueryField.tsx @@ -3,7 +3,7 @@ import { LokiQueryFieldForm, LokiQueryFieldFormProps } from './LokiQueryFieldFor type LokiQueryFieldProps = Omit< LokiQueryFieldFormProps, - 'labelsLoaded' | 'onLoadOptions' | 'onLabelsRefresh' | 'logLabelOptions' | 'absoluteRange' + 'labelsLoaded' | 'onLoadOptions' | 'onLabelsRefresh' | 'absoluteRange' >; export const LokiQueryField: FunctionComponent = (props) => { diff --git a/public/app/plugins/datasource/loki/components/__snapshots__/LokiExploreQueryEditor.test.tsx.snap b/public/app/plugins/datasource/loki/components/__snapshots__/LokiExploreQueryEditor.test.tsx.snap index fab2267490a..5b3345352fd 100644 --- a/public/app/plugins/datasource/loki/components/__snapshots__/LokiExploreQueryEditor.test.tsx.snap +++ b/public/app/plugins/datasource/loki/components/__snapshots__/LokiExploreQueryEditor.test.tsx.snap @@ -40,7 +40,6 @@ exports[`LokiExploreQueryEditor should render component 1`] = ` Object { "getTimeRangeParams": [Function], "languageProvider": LokiLanguageProvider { - "addLabelValuesToOptions": [Function], "cleanText": [Function], "datasource": [Circular], "fetchSeries": [Function], diff --git a/public/app/plugins/datasource/loki/language_provider.ts b/public/app/plugins/datasource/loki/language_provider.ts index c0ad4c4f7bc..8b365e63e2c 100644 --- a/public/app/plugins/datasource/loki/language_provider.ts +++ b/public/app/plugins/datasource/loki/language_provider.ts @@ -56,7 +56,6 @@ export function addHistoryMetadata(item: CompletionItem, history: LokiHistoryIte export default class LokiLanguageProvider extends LanguageProvider { labelKeys: string[]; - logLabelOptions: any[]; logLabelFetchTs: number; started: boolean; datasource: LokiDatasource; @@ -402,15 +401,14 @@ export default class LokiLanguageProvider extends LanguageProvider { */ async fetchLogLabels(): Promise { const url = '/loki/api/v1/label'; - try { - this.logLabelFetchTs = Date.now().valueOf(); - const rangeParams = this.datasource.getTimeRangeParams(); - const res = await this.request(url, rangeParams); + const timeRange = this.datasource.getTimeRangeParams(); + this.logLabelFetchTs = Date.now().valueOf(); + + const res = await this.request(url, timeRange); + if (Array.isArray(res)) { this.labelKeys = res.slice().sort(); - this.logLabelOptions = this.labelKeys.map((key: string) => ({ label: key, value: key, isLeaf: false })); - } catch (e) { - console.error(e); } + return []; } @@ -473,41 +471,23 @@ export default class LokiLanguageProvider extends LanguageProvider { async fetchLabelValues(key: string): Promise { const url = `/loki/api/v1/label/${key}/values`; - let values: string[] = []; const rangeParams = this.datasource.getTimeRangeParams(); const { from: start, to: end } = rangeParams; const cacheKey = this.generateCacheKey(url, start, end, key); const params = { start, end }; - let value = this.labelsCache.get(cacheKey); - if (!value) { - try { - // Clear value when requesting new one. Empty object being truthy also makes sure we don't request twice. - this.labelsCache.set(cacheKey, []); - const res = await this.request(url, params); - values = res.slice().sort(); - value = values; - this.labelsCache.set(cacheKey, value); - - this.logLabelOptions = this.addLabelValuesToOptions(key, values); - } catch (e) { - console.error(e); + let labelValue = this.labelsCache.get(cacheKey); + if (!labelValue) { + // Clear value when requesting new one. Empty object being truthy also makes sure we don't request twice. + this.labelsCache.set(cacheKey, []); + const res = await this.request(url, params); + if (Array.isArray(res)) { + labelValue = res.slice().sort(); + this.labelsCache.set(cacheKey, labelValue); } - } else { - this.logLabelOptions = this.addLabelValuesToOptions(key, value); } - return value ?? []; - } - private addLabelValuesToOptions = (labelKey: string, values: string[]) => { - return this.logLabelOptions.map((keyOption) => - keyOption.value === labelKey - ? { - ...keyOption, - children: values.map((value) => ({ label: value, value })), - } - : keyOption - ); - }; + return labelValue ?? []; + } }