Legacy annotations: add dataplane support (#96226)

This commit is contained in:
Matias Chomicki 2024-11-12 18:44:06 +00:00 committed by GitHub
parent 580d073b96
commit 68db1c6e68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 4 deletions

View File

@ -442,9 +442,74 @@ describe('LokiDatasource', () => {
expect(res.length).toBe(2); expect(res.length).toBe(2);
expect(res[0].text).toBe('hello'); expect(res[0].text).toBe('hello');
expect(res[0].tags).toEqual(['value']); expect(res[0].tags).toEqual(['value']);
expect(res[0].time).toEqual(1);
expect(res[1].text).toBe('hello 2'); expect(res[1].text).toBe('hello 2');
expect(res[1].tags).toEqual(['value2']); 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', () => { describe('Formatting', () => {

View File

@ -1042,8 +1042,12 @@ export class LokiDatasource
const annotations: AnnotationEvent[] = []; const annotations: AnnotationEvent[] = [];
const splitKeys: string[] = tagKeys.split(',').filter((v: string) => v !== ''); const splitKeys: string[] = tagKeys.split(',').filter((v: string) => v !== '');
const isDataplaneLog = config.featureToggles.lokiLogsDataplane;
for (const frame of data) { 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) => { view.forEach((row) => {
const { labels } = row; const { labels } = row;
@ -1063,15 +1067,17 @@ export class LokiDatasource
return true; return true;
}) })
.map(([key, val]) => val); // keep only the label-value .map(([_, val]) => val); // keep only the label-value
// remove duplicates // remove duplicates
const tags = Array.from(new Set(maybeDuplicatedTags)); const tags = Array.from(new Set(maybeDuplicatedTags));
const logLine = isDataplaneLog ? row.body : row.Line;
annotations.push({ annotations.push({
time: new Date(row.Time).valueOf(), time: isDataplaneLog ? new Date(row.timestamp).valueOf() : new Date(row.Time).valueOf(),
title: renderLegendFormat(titleFormat, labels), title: renderLegendFormat(titleFormat, labels),
text: renderLegendFormat(textFormat, labels) || row.Line, text: renderLegendFormat(textFormat, labels) || logLine,
tags, tags,
}); });
}); });