Loki: Bring back processed bytes as meta info (#34092)

* Loki: Bring back processed bytes as meta info

* style: Lint
This commit is contained in:
mmenbawy
2021-05-24 12:28:10 +02:00
committed by GitHub
parent e21b90681f
commit 6796a89e9d
2 changed files with 39 additions and 1 deletions

View File

@@ -698,6 +698,8 @@ describe('logSeriesToLogsModel', () => {
meta: { meta: {
searchWords: ['test'], searchWords: ['test'],
limit: 1000, limit: 1000,
stats: [{ displayName: 'Summary: total bytes processed', value: 97048, unit: 'decbytes' }],
custom: { lokiQueryStatKey: 'Summary: total bytes processed' },
preferredVisualisationType: 'logs', preferredVisualisationType: 'logs',
}, },
}, },
@@ -705,7 +707,10 @@ describe('logSeriesToLogsModel', () => {
const metaData = { const metaData = {
hasUniqueLabels: false, hasUniqueLabels: false,
meta: [{ label: LIMIT_LABEL, value: 1000, kind: 0 }], meta: [
{ label: LIMIT_LABEL, value: 1000, kind: 0 },
{ label: 'Total bytes processed', value: '97.0 kB', kind: 1 },
],
rows: [], rows: [],
}; };
@@ -739,6 +744,8 @@ describe('logSeriesToLogsModel', () => {
meta: { meta: {
searchWords: ['test'], searchWords: ['test'],
limit: 1000, limit: 1000,
stats: [{ displayName: 'Summary: total bytes processed', value: 97048, unit: 'decbytes' }],
custom: { lokiQueryStatKey: 'Summary: total bytes processed' },
preferredVisualisationType: 'logs', preferredVisualisationType: 'logs',
}, },
}), }),
@@ -749,6 +756,8 @@ describe('logSeriesToLogsModel', () => {
meta: { meta: {
searchWords: ['test'], searchWords: ['test'],
limit: 1000, limit: 1000,
stats: [{ displayName: 'Summary: total bytes processed', value: 97048, unit: 'decbytes' }],
custom: { lokiQueryStatKey: 'Summary: total bytes processed' },
preferredVisualisationType: 'logs', preferredVisualisationType: 'logs',
}, },
}), }),
@@ -758,6 +767,7 @@ describe('logSeriesToLogsModel', () => {
expect(logsModel.meta).toMatchObject([ expect(logsModel.meta).toMatchObject([
{ kind: 2, label: 'Common labels', value: { foo: 'bar', level: 'dbug' } }, { kind: 2, label: 'Common labels', value: { foo: 'bar', level: 'dbug' } },
{ kind: 0, label: LIMIT_LABEL, value: 2000 }, { kind: 0, label: LIMIT_LABEL, value: 2000 },
{ kind: 1, label: 'Total bytes processed', value: '194 kB' },
]); ]);
expect(logsModel.rows).toHaveLength(3); expect(logsModel.rows).toHaveLength(3);
expect(logsModel.rows).toMatchObject([ expect(logsModel.rows).toMatchObject([

View File

@@ -33,6 +33,7 @@ import {
DataQuery, DataQuery,
} from '@grafana/data'; } from '@grafana/data';
import { getThemeColor } from 'app/core/utils/colors'; import { getThemeColor } from 'app/core/utils/colors';
import { SIPrefix } from '@grafana/data/src/valueFormats/symbolFormatters';
import { config } from '@grafana/runtime'; import { config } from '@grafana/runtime';
export const LIMIT_LABEL = 'Line limit'; export const LIMIT_LABEL = 'Line limit';
@@ -443,10 +444,16 @@ export function logSeriesToLogsModel(logSeries: DataFrame[]): LogsModel | undefi
kind: LogsMetaKind.Number, kind: LogsMetaKind.Number,
}); });
} }
let totalBytes = 0;
const queriesVisited: { [refId: string]: boolean } = {};
// To add just 1 error message // To add just 1 error message
let errorMetaAdded = false; let errorMetaAdded = false;
for (const series of logSeries) { for (const series of logSeries) {
const totalBytesKey = series.meta?.custom?.lokiQueryStatKey;
const { refId } = series; // Stats are per query, keeping track by refId
if (!errorMetaAdded && series.meta?.custom?.error) { if (!errorMetaAdded && series.meta?.custom?.error) {
meta.push({ meta.push({
label: '', label: '',
@@ -455,7 +462,28 @@ export function logSeriesToLogsModel(logSeries: DataFrame[]): LogsModel | undefi
}); });
errorMetaAdded = true; errorMetaAdded = true;
} }
if (refId && !queriesVisited[refId]) {
if (totalBytesKey && series.meta?.stats) {
const byteStat = series.meta.stats.find((stat) => stat.displayName === totalBytesKey);
if (byteStat) {
totalBytes += byteStat.value;
}
}
queriesVisited[refId] = true;
}
} }
if (totalBytes > 0) {
const { text, suffix } = SIPrefix('B')(totalBytes);
meta.push({
label: 'Total bytes processed',
value: `${text} ${suffix}`,
kind: LogsMetaKind.String,
});
}
return { return {
hasUniqueLabels, hasUniqueLabels,
meta, meta,