diff --git a/public/app/plugins/datasource/loki/datasource.test.ts b/public/app/plugins/datasource/loki/datasource.test.ts index 4f9f6ca3372..1e5158630aa 100644 --- a/public/app/plugins/datasource/loki/datasource.test.ts +++ b/public/app/plugins/datasource/loki/datasource.test.ts @@ -442,9 +442,74 @@ describe('LokiDatasource', () => { expect(res.length).toBe(2); expect(res[0].text).toBe('hello'); expect(res[0].tags).toEqual(['value']); + expect(res[0].time).toEqual(1); expect(res[1].text).toBe('hello 2'); expect(res[1].tags).toEqual(['value2']); + expect(res[1].time).toEqual(2); + }); + + it('should transform the loki dataplane data to annotation response', async () => { + const originalDataplaneState = config.featureToggles.lokiLogsDataplane; + config.featureToggles.lokiLogsDataplane = true; + const testFrame: DataFrame = { + refId: 'A', + fields: [ + { + name: 'timestamp', + type: FieldType.time, + config: {}, + values: [1, 2], + }, + { + name: 'body', + type: FieldType.string, + config: {}, + values: ['hello', 'hello 2'], + }, + { + name: 'labels', + type: FieldType.other, + config: {}, + values: [ + { + label: 'value', + label2: 'value ', + }, + { + label: '', + label2: 'value2', + label3: ' ', + }, + ], + }, + { + name: 'tsNs', + type: FieldType.string, + config: {}, + values: ['1000000', '2000000'], + }, + { + name: 'id', + type: FieldType.string, + config: {}, + values: ['id1', 'id2'], + }, + ], + length: 2, + }; + const res = await getTestContext(testFrame, { stepInterval: '15s' }); + + expect(res.length).toBe(2); + expect(res[0].text).toBe('hello'); + expect(res[0].tags).toEqual(['value']); + expect(res[0].time).toEqual(1); + + expect(res[1].text).toBe('hello 2'); + expect(res[1].tags).toEqual(['value2']); + expect(res[1].time).toEqual(2); + + config.featureToggles.lokiLogsDataplane = originalDataplaneState; }); describe('Formatting', () => { diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index f661d8d5f39..e146973ccf7 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -1042,8 +1042,12 @@ export class LokiDatasource const annotations: AnnotationEvent[] = []; const splitKeys: string[] = tagKeys.split(',').filter((v: string) => v !== ''); + const isDataplaneLog = config.featureToggles.lokiLogsDataplane; + for (const frame of data) { - const view = new DataFrameView<{ Time: string; Line: string; labels: Labels }>(frame); + const view = new DataFrameView<{ timestamp: string; Time: string; body: string; Line: string; labels: Labels }>( + frame + ); view.forEach((row) => { const { labels } = row; @@ -1063,15 +1067,17 @@ export class LokiDatasource return true; }) - .map(([key, val]) => val); // keep only the label-value + .map(([_, val]) => val); // keep only the label-value // remove duplicates const tags = Array.from(new Set(maybeDuplicatedTags)); + const logLine = isDataplaneLog ? row.body : row.Line; + annotations.push({ - time: new Date(row.Time).valueOf(), + time: isDataplaneLog ? new Date(row.timestamp).valueOf() : new Date(row.Time).valueOf(), title: renderLegendFormat(titleFormat, labels), - text: renderLegendFormat(textFormat, labels) || row.Line, + text: renderLegendFormat(textFormat, labels) || logLine, tags, }); });