2018-10-30 10:14:01 -05:00
|
|
|
import _ from 'lodash';
|
2018-11-02 02:25:36 -05:00
|
|
|
import { TimeSeries } from 'app/core/core';
|
2018-11-06 05:00:05 -06:00
|
|
|
import colors from 'app/core/utils/colors';
|
2018-10-30 10:14:01 -05:00
|
|
|
|
2018-07-20 10:07:17 -05:00
|
|
|
export enum LogLevel {
|
2018-11-23 09:29:55 -06:00
|
|
|
crit = 'critical',
|
|
|
|
critical = 'critical',
|
|
|
|
warn = 'warning',
|
|
|
|
warning = 'warning',
|
2018-07-20 10:07:17 -05:00
|
|
|
err = 'error',
|
|
|
|
error = 'error',
|
|
|
|
info = 'info',
|
|
|
|
debug = 'debug',
|
|
|
|
trace = 'trace',
|
2018-11-23 09:29:55 -06:00
|
|
|
unkown = 'unkown',
|
2018-07-20 10:07:17 -05:00
|
|
|
}
|
|
|
|
|
2018-11-06 05:00:05 -06:00
|
|
|
export const LogLevelColor = {
|
2018-11-23 09:29:55 -06:00
|
|
|
[LogLevel.critical]: colors[7],
|
|
|
|
[LogLevel.warning]: colors[1],
|
2018-11-06 05:00:05 -06:00
|
|
|
[LogLevel.error]: colors[4],
|
|
|
|
[LogLevel.info]: colors[0],
|
2018-11-23 09:29:55 -06:00
|
|
|
[LogLevel.debug]: colors[5],
|
|
|
|
[LogLevel.trace]: colors[2],
|
|
|
|
[LogLevel.unkown]: '#ddd',
|
2018-11-06 05:00:05 -06:00
|
|
|
};
|
|
|
|
|
2018-07-20 10:07:17 -05:00
|
|
|
export interface LogSearchMatch {
|
|
|
|
start: number;
|
|
|
|
length: number;
|
2018-10-05 06:00:45 -05:00
|
|
|
text: string;
|
2018-07-20 10:07:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface LogRow {
|
2018-11-18 03:38:06 -06:00
|
|
|
duplicates?: number;
|
2018-07-20 10:07:17 -05:00
|
|
|
entry: string;
|
2018-11-08 07:24:54 -06:00
|
|
|
key: string; // timestamp + labels
|
2018-11-28 03:46:35 -06:00
|
|
|
labels: LogsStreamLabels;
|
2018-07-20 10:07:17 -05:00
|
|
|
logLevel: LogLevel;
|
2018-11-08 07:24:54 -06:00
|
|
|
searchWords?: string[];
|
|
|
|
timestamp: string; // ISO with nanosec precision
|
2018-07-20 10:07:17 -05:00
|
|
|
timeFromNow: string;
|
2018-11-08 07:24:54 -06:00
|
|
|
timeEpochMs: number;
|
2018-07-20 10:07:17 -05:00
|
|
|
timeLocal: string;
|
2018-11-28 03:46:35 -06:00
|
|
|
uniqueLabels?: LogsStreamLabels;
|
|
|
|
}
|
|
|
|
|
2018-12-03 10:00:50 -06:00
|
|
|
export interface LogsLabelStat {
|
|
|
|
active?: boolean;
|
|
|
|
count: number;
|
|
|
|
proportion: number;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
2018-11-28 03:46:35 -06:00
|
|
|
export enum LogsMetaKind {
|
|
|
|
Number,
|
|
|
|
String,
|
|
|
|
LabelsMap,
|
2018-07-20 10:07:17 -05:00
|
|
|
}
|
|
|
|
|
2018-11-02 02:25:36 -05:00
|
|
|
export interface LogsMetaItem {
|
|
|
|
label: string;
|
2018-11-28 03:46:35 -06:00
|
|
|
value: string | number | LogsStreamLabels;
|
|
|
|
kind: LogsMetaKind;
|
2018-11-02 02:25:36 -05:00
|
|
|
}
|
|
|
|
|
2018-07-20 10:07:17 -05:00
|
|
|
export interface LogsModel {
|
2018-11-30 05:18:01 -06:00
|
|
|
id: string; // Identify one logs result from another
|
2018-11-02 02:25:36 -05:00
|
|
|
meta?: LogsMetaItem[];
|
2018-07-20 10:07:17 -05:00
|
|
|
rows: LogRow[];
|
2018-11-02 02:25:36 -05:00
|
|
|
series?: TimeSeries[];
|
2018-07-20 10:07:17 -05:00
|
|
|
}
|
2018-10-30 10:14:01 -05:00
|
|
|
|
2018-11-02 02:25:36 -05:00
|
|
|
export interface LogsStream {
|
|
|
|
labels: string;
|
|
|
|
entries: LogsStreamEntry[];
|
2018-11-08 07:24:54 -06:00
|
|
|
search?: string;
|
|
|
|
parsedLabels?: LogsStreamLabels;
|
2018-11-28 03:46:35 -06:00
|
|
|
uniqueLabels?: LogsStreamLabels;
|
2018-11-02 02:25:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface LogsStreamEntry {
|
|
|
|
line: string;
|
|
|
|
timestamp: string;
|
2018-10-30 10:14:01 -05:00
|
|
|
}
|
2018-11-08 07:24:54 -06:00
|
|
|
|
|
|
|
export interface LogsStreamLabels {
|
|
|
|
[key: string]: string;
|
|
|
|
}
|
|
|
|
|
2018-11-18 03:38:06 -06:00
|
|
|
export enum LogsDedupStrategy {
|
|
|
|
none = 'none',
|
|
|
|
exact = 'exact',
|
|
|
|
numbers = 'numbers',
|
|
|
|
signature = 'signature',
|
|
|
|
}
|
|
|
|
|
2018-12-03 10:00:50 -06:00
|
|
|
export function calculateLogsLabelStats(rows: LogRow[], label: string): LogsLabelStat[] {
|
|
|
|
// Consider only rows that have the given label
|
|
|
|
const rowsWithLabel = rows.filter(row => row.labels[label] !== undefined);
|
|
|
|
const rowCount = rowsWithLabel.length;
|
|
|
|
|
|
|
|
// Get label value counts for eligible rows
|
|
|
|
const countsByValue = _.countBy(rowsWithLabel, row => (row as LogRow).labels[label]);
|
|
|
|
const sortedCounts = _.chain(countsByValue)
|
|
|
|
.map((count, value) => ({ count, value, proportion: count / rowCount }))
|
|
|
|
.sortBy('count')
|
|
|
|
.reverse()
|
|
|
|
.value();
|
|
|
|
|
|
|
|
return sortedCounts;
|
|
|
|
}
|
|
|
|
|
2018-11-18 03:38:06 -06:00
|
|
|
const isoDateRegexp = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-6]\d[,\.]\d+([+-][0-2]\d:[0-5]\d|Z)/g;
|
|
|
|
function isDuplicateRow(row: LogRow, other: LogRow, strategy: LogsDedupStrategy): boolean {
|
|
|
|
switch (strategy) {
|
|
|
|
case LogsDedupStrategy.exact:
|
|
|
|
// Exact still strips dates
|
|
|
|
return row.entry.replace(isoDateRegexp, '') === other.entry.replace(isoDateRegexp, '');
|
|
|
|
|
|
|
|
case LogsDedupStrategy.numbers:
|
|
|
|
return row.entry.replace(/\d/g, '') === other.entry.replace(/\d/g, '');
|
|
|
|
|
|
|
|
case LogsDedupStrategy.signature:
|
|
|
|
return row.entry.replace(/\w/g, '') === other.entry.replace(/\w/g, '');
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function dedupLogRows(logs: LogsModel, strategy: LogsDedupStrategy): LogsModel {
|
|
|
|
if (strategy === LogsDedupStrategy.none) {
|
|
|
|
return logs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dedupedRows = logs.rows.reduce((result: LogRow[], row: LogRow, index, list) => {
|
|
|
|
const previous = result[result.length - 1];
|
|
|
|
if (index > 0 && isDuplicateRow(row, previous, strategy)) {
|
|
|
|
previous.duplicates++;
|
|
|
|
} else {
|
|
|
|
row.duplicates = 0;
|
|
|
|
result.push(row);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...logs,
|
|
|
|
rows: dedupedRows,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-23 09:29:55 -06:00
|
|
|
export function filterLogLevels(logs: LogsModel, hiddenLogLevels: Set<LogLevel>): LogsModel {
|
|
|
|
if (hiddenLogLevels.size === 0) {
|
|
|
|
return logs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const filteredRows = logs.rows.reduce((result: LogRow[], row: LogRow, index, list) => {
|
|
|
|
if (!hiddenLogLevels.has(row.logLevel)) {
|
|
|
|
result.push(row);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...logs,
|
|
|
|
rows: filteredRows,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-08 07:24:54 -06:00
|
|
|
export function makeSeriesForLogs(rows: LogRow[], intervalMs: number): TimeSeries[] {
|
|
|
|
// Graph time series by log level
|
|
|
|
const seriesByLevel = {};
|
|
|
|
rows.forEach(row => {
|
|
|
|
if (!seriesByLevel[row.logLevel]) {
|
|
|
|
seriesByLevel[row.logLevel] = { lastTs: null, datapoints: [], alias: row.logLevel };
|
|
|
|
}
|
|
|
|
const levelSeries = seriesByLevel[row.logLevel];
|
|
|
|
|
|
|
|
// Bucket to nearest minute
|
|
|
|
const time = Math.round(row.timeEpochMs / intervalMs / 10) * intervalMs * 10;
|
|
|
|
// Entry for time
|
|
|
|
if (time === levelSeries.lastTs) {
|
|
|
|
levelSeries.datapoints[levelSeries.datapoints.length - 1][0]++;
|
|
|
|
} else {
|
|
|
|
levelSeries.datapoints.push([1, time]);
|
|
|
|
levelSeries.lastTs = time;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Object.keys(seriesByLevel).reduce((acc, level) => {
|
|
|
|
if (seriesByLevel[level]) {
|
|
|
|
const gs = new TimeSeries(seriesByLevel[level]);
|
|
|
|
gs.setColor(LogLevelColor[level]);
|
|
|
|
acc.push(gs);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
}
|