Commit a07296bf by Marcus Efraimsson Committed by GitHub

explore: don't parse log levels if provided by field or label (#17180)

If a field or a label named level is returned from datasource that is 
used as log level for the logs result instead of parsing the log level 
from the message.

Closes #17122
parent 04d473b3
......@@ -23,6 +23,15 @@ export function getLogLevel(line: string): LogLevel {
return LogLevel.unknown;
}
export function getLogLevelFromKey(key: string): LogLevel {
const level = (LogLevel as any)[key];
if (level) {
return level;
}
return LogLevel.unknown;
}
export function addLogLevelToSeries(series: SeriesData, lineIndex: number): SeriesData {
return {
...series, // Keeps Tags, RefID etc
......
......@@ -13,6 +13,7 @@ import {
toLegacyResponseData,
FieldCache,
FieldType,
getLogLevelFromKey,
LogRowModel,
LogsModel,
LogsMetaItem,
......@@ -368,7 +369,17 @@ export function processLogSeriesRow(
const timeEpochMs = time.valueOf();
const timeFromNow = time.fromNow();
const timeLocal = time.format('YYYY-MM-DD HH:mm:ss');
const logLevel = getLogLevel(message);
let logLevel = LogLevel.unknown;
const logLevelField = fieldCache.getFieldByName('level');
if (logLevelField) {
logLevel = getLogLevelFromKey(row[logLevelField.index]);
} else if (series.labels && Object.keys(series.labels).indexOf('level') !== -1) {
logLevel = getLogLevelFromKey(series.labels['level']);
} else {
logLevel = getLogLevel(message);
}
const hasAnsi = hasAnsiCodes(message);
const searchWords = series.meta && series.meta.searchWords ? series.meta.searchWords : [];
......
import { SeriesData, FieldType, LogsModel, LogsMetaKind, LogsDedupStrategy } from '@grafana/ui';
import { SeriesData, FieldType, LogsModel, LogsMetaKind, LogsDedupStrategy, LogLevel } from '@grafana/ui';
import {
dedupLogRows,
calculateFieldStats,
......@@ -460,8 +460,12 @@ describe('seriesDataToLogsModel', () => {
name: 'message',
type: FieldType.string,
},
{
name: 'level',
type: FieldType.string,
},
],
rows: [['1970-01-01T00:00:01Z', 'WARN boooo']],
rows: [['1970-01-01T00:00:01Z', 'WARN boooo', 'dbug']],
},
];
const logsModel = seriesDataToLogsModel(series, 0);
......@@ -470,7 +474,7 @@ describe('seriesDataToLogsModel', () => {
{
entry: 'WARN boooo',
labels: undefined,
logLevel: 'warning',
logLevel: LogLevel.debug,
uniqueLabels: {},
},
]);
......@@ -482,6 +486,7 @@ describe('seriesDataToLogsModel', () => {
labels: {
foo: 'bar',
baz: '1',
level: 'dbug',
},
fields: [
{
......@@ -500,6 +505,7 @@ describe('seriesDataToLogsModel', () => {
labels: {
foo: 'bar',
baz: '2',
level: 'err',
},
fields: [
{
......@@ -521,19 +527,19 @@ describe('seriesDataToLogsModel', () => {
{
entry: 'INFO 2',
labels: { foo: 'bar', baz: '2' },
logLevel: 'info',
logLevel: LogLevel.error,
uniqueLabels: { baz: '2' },
},
{
entry: 'WARN boooo',
labels: { foo: 'bar', baz: '1' },
logLevel: 'warning',
logLevel: LogLevel.debug,
uniqueLabels: { baz: '1' },
},
{
entry: 'INFO 1',
labels: { foo: 'bar', baz: '2' },
logLevel: 'info',
logLevel: LogLevel.error,
uniqueLabels: { baz: '2' },
},
]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment