From f2814046d1ef68ebb0cc894deb7c37ea9ccb7076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Farkas?= Date: Fri, 22 Apr 2022 08:43:33 +0200 Subject: [PATCH] loki: logrowcontext: explicitly name fields (#48043) --- .../app/plugins/datasource/loki/datasource.ts | 49 +++++++++++++++---- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index 476a1b95a17..96a2357254b 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -557,17 +557,46 @@ export class LokiDatasource const limit = (options && options.limit) || 10; const { query, range } = this.prepareLogRowContextQueryTarget(row, limit, direction); - const sortResults = (result: DataQueryResponse): DataQueryResponse => { + const processDataFrame = (frame: DataFrame): DataFrame => { + // log-row-context requires specific field-names to work, so we set them here: "ts", "line", "id" + const cache = new FieldCache(frame); + const timestampField = cache.getFirstFieldOfType(FieldType.time); + const lineField = cache.getFirstFieldOfType(FieldType.string); + const idField = cache.getFieldByName('id'); + + if (timestampField === undefined || lineField === undefined || idField === undefined) { + // this should never really happen, but i want to keep typescript happy + return { ...frame, fields: [] }; + } + + return { + ...frame, + fields: [ + { + ...timestampField, + name: 'ts', + }, + { + ...lineField, + name: 'line', + }, + { + ...idField, + name: 'id', + }, + ], + }; + }; + + const processResults = (result: DataQueryResponse): DataQueryResponse => { + const frames: DataFrame[] = result.data; + const processedFrames = frames + .map((frame) => sortDataFrameByTime(frame, 'DESCENDING')) + .map((frame) => processDataFrame(frame)); // rename fields if needed + return { ...result, - data: result.data.map((frame: DataFrame) => { - const timestampFieldIndex = frame.fields.findIndex((field) => field.type === FieldType.time); - if (timestampFieldIndex === -1) { - return frame; - } - - return sortDataFrameByTime(frame, 'DESCENDING'); - }), + data: processedFrames, }; }; @@ -584,7 +613,7 @@ export class LokiDatasource }; throw error; }), - switchMap((res) => of(sortResults(res))) + switchMap((res) => of(processResults(res))) ) ); };