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 { ...@@ -23,6 +23,15 @@ export function getLogLevel(line: string): LogLevel {
return LogLevel.unknown; 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 { export function addLogLevelToSeries(series: SeriesData, lineIndex: number): SeriesData {
return { return {
...series, // Keeps Tags, RefID etc ...series, // Keeps Tags, RefID etc
......
...@@ -13,6 +13,7 @@ import { ...@@ -13,6 +13,7 @@ import {
toLegacyResponseData, toLegacyResponseData,
FieldCache, FieldCache,
FieldType, FieldType,
getLogLevelFromKey,
LogRowModel, LogRowModel,
LogsModel, LogsModel,
LogsMetaItem, LogsMetaItem,
...@@ -368,7 +369,17 @@ export function processLogSeriesRow( ...@@ -368,7 +369,17 @@ export function processLogSeriesRow(
const timeEpochMs = time.valueOf(); const timeEpochMs = time.valueOf();
const timeFromNow = time.fromNow(); const timeFromNow = time.fromNow();
const timeLocal = time.format('YYYY-MM-DD HH:mm:ss'); 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 hasAnsi = hasAnsiCodes(message);
const searchWords = series.meta && series.meta.searchWords ? series.meta.searchWords : []; 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 { import {
dedupLogRows, dedupLogRows,
calculateFieldStats, calculateFieldStats,
...@@ -460,8 +460,12 @@ describe('seriesDataToLogsModel', () => { ...@@ -460,8 +460,12 @@ describe('seriesDataToLogsModel', () => {
name: 'message', name: 'message',
type: FieldType.string, 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); const logsModel = seriesDataToLogsModel(series, 0);
...@@ -470,7 +474,7 @@ describe('seriesDataToLogsModel', () => { ...@@ -470,7 +474,7 @@ describe('seriesDataToLogsModel', () => {
{ {
entry: 'WARN boooo', entry: 'WARN boooo',
labels: undefined, labels: undefined,
logLevel: 'warning', logLevel: LogLevel.debug,
uniqueLabels: {}, uniqueLabels: {},
}, },
]); ]);
...@@ -482,6 +486,7 @@ describe('seriesDataToLogsModel', () => { ...@@ -482,6 +486,7 @@ describe('seriesDataToLogsModel', () => {
labels: { labels: {
foo: 'bar', foo: 'bar',
baz: '1', baz: '1',
level: 'dbug',
}, },
fields: [ fields: [
{ {
...@@ -500,6 +505,7 @@ describe('seriesDataToLogsModel', () => { ...@@ -500,6 +505,7 @@ describe('seriesDataToLogsModel', () => {
labels: { labels: {
foo: 'bar', foo: 'bar',
baz: '2', baz: '2',
level: 'err',
}, },
fields: [ fields: [
{ {
...@@ -521,19 +527,19 @@ describe('seriesDataToLogsModel', () => { ...@@ -521,19 +527,19 @@ describe('seriesDataToLogsModel', () => {
{ {
entry: 'INFO 2', entry: 'INFO 2',
labels: { foo: 'bar', baz: '2' }, labels: { foo: 'bar', baz: '2' },
logLevel: 'info', logLevel: LogLevel.error,
uniqueLabels: { baz: '2' }, uniqueLabels: { baz: '2' },
}, },
{ {
entry: 'WARN boooo', entry: 'WARN boooo',
labels: { foo: 'bar', baz: '1' }, labels: { foo: 'bar', baz: '1' },
logLevel: 'warning', logLevel: LogLevel.debug,
uniqueLabels: { baz: '1' }, uniqueLabels: { baz: '1' },
}, },
{ {
entry: 'INFO 1', entry: 'INFO 1',
labels: { foo: 'bar', baz: '2' }, labels: { foo: 'bar', baz: '2' },
logLevel: 'info', logLevel: LogLevel.error,
uniqueLabels: { baz: '2' }, 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