diff --git a/.betterer.results b/.betterer.results index 7e4f52998ee..ec429576157 100644 --- a/.betterer.results +++ b/.betterer.results @@ -7862,21 +7862,7 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "10"], [0, 0, 0, "Unexpected any. Specify a different type.", "11"], [0, 0, 0, "Unexpected any. Specify a different type.", "12"], - [0, 0, 0, "Unexpected any. Specify a different type.", "13"], - [0, 0, 0, "Unexpected any. Specify a different type.", "14"], - [0, 0, 0, "Unexpected any. Specify a different type.", "15"], - [0, 0, 0, "Unexpected any. Specify a different type.", "16"], - [0, 0, 0, "Unexpected any. Specify a different type.", "17"], - [0, 0, 0, "Unexpected any. Specify a different type.", "18"], - [0, 0, 0, "Unexpected any. Specify a different type.", "19"], - [0, 0, 0, "Unexpected any. Specify a different type.", "20"], - [0, 0, 0, "Unexpected any. Specify a different type.", "21"], - [0, 0, 0, "Unexpected any. Specify a different type.", "22"], - [0, 0, 0, "Unexpected any. Specify a different type.", "23"], - [0, 0, 0, "Unexpected any. Specify a different type.", "24"], - [0, 0, 0, "Unexpected any. Specify a different type.", "25"], - [0, 0, 0, "Unexpected any. Specify a different type.", "26"], - [0, 0, 0, "Unexpected any. Specify a different type.", "27"] + [0, 0, 0, "Unexpected any. Specify a different type.", "13"] ], "public/app/plugins/datasource/loki/datasource.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], diff --git a/public/app/plugins/datasource/loki/datasource.test.ts b/public/app/plugins/datasource/loki/datasource.test.ts index 785f3b7b9c2..b01703da4b7 100644 --- a/public/app/plugins/datasource/loki/datasource.test.ts +++ b/public/app/plugins/datasource/loki/datasource.test.ts @@ -942,7 +942,7 @@ describe('LokiDatasource', () => { describe('prepareLogRowContextQueryTarget', () => { const ds = createLokiDSForTests(); - it('creates query with only labels from /labels API', () => { + it('creates query with only labels from /labels API', async () => { const row: LogRowModel = { rowIndex: 0, dataFrame: new MutableDataFrame({ @@ -956,15 +956,39 @@ describe('LokiDatasource', () => { }), labels: { bar: 'baz', foo: 'uniqueParsedLabel' }, uid: '1', - } as any; + } as unknown as LogRowModel; //Mock stored labels to only include "bar" label + jest.spyOn(ds.languageProvider, 'start').mockImplementation(() => Promise.resolve([])); jest.spyOn(ds.languageProvider, 'getLabelKeys').mockImplementation(() => ['bar']); - const contextQuery = ds.prepareLogRowContextQueryTarget(row, 10, 'BACKWARD'); + const contextQuery = await ds.prepareLogRowContextQueryTarget(row, 10, 'BACKWARD'); expect(contextQuery.query.expr).toContain('baz'); expect(contextQuery.query.expr).not.toContain('uniqueParsedLabel'); }); + + it('should call languageProvider.start to fetch labels', async () => { + const row: LogRowModel = { + rowIndex: 0, + dataFrame: new MutableDataFrame({ + fields: [ + { + name: 'ts', + type: FieldType.time, + values: [0], + }, + ], + }), + labels: { bar: 'baz', foo: 'uniqueParsedLabel' }, + uid: '1', + } as unknown as LogRowModel; + + //Mock stored labels to only include "bar" label + jest.spyOn(ds.languageProvider, 'start').mockImplementation(() => Promise.resolve([])); + await ds.prepareLogRowContextQueryTarget(row, 10, 'BACKWARD'); + + expect(ds.languageProvider.start).toBeCalled(); + }); }); describe('logs volume data provider', () => { diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index f72a4b6ed73..e9ee1be9771 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -594,10 +594,10 @@ export class LokiDatasource return Math.ceil(date.valueOf() * 1e6); } - getLogRowContext = (row: LogRowModel, options?: RowContextOptions): Promise<{ data: DataFrame[] }> => { + getLogRowContext = async (row: LogRowModel, options?: RowContextOptions): Promise<{ data: DataFrame[] }> => { const direction = (options && options.direction) || 'BACKWARD'; const limit = (options && options.limit) || 10; - const { query, range } = this.prepareLogRowContextQueryTarget(row, limit, direction); + const { query, range } = await this.prepareLogRowContextQueryTarget(row, limit, direction); const processDataFrame = (frame: DataFrame): DataFrame => { // log-row-context requires specific field-names to work, so we set them here: "ts", "line", "id" @@ -660,11 +660,13 @@ export class LokiDatasource ); }; - prepareLogRowContextQueryTarget = ( + prepareLogRowContextQueryTarget = async ( row: LogRowModel, limit: number, direction: 'BACKWARD' | 'FORWARD' - ): { query: LokiQuery; range: TimeRange } => { + ): Promise<{ query: LokiQuery; range: TimeRange }> => { + // need to await the languageProvider to be started to have all labels. This call is not blocking after it has been called once. + await this.languageProvider.start(); const labels = this.languageProvider.getLabelKeys(); const expr = Object.keys(row.labels) .map((label: string) => {