logs_model.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import _ from 'lodash';
  2. import { TimeSeries } from 'app/core/core';
  3. import colors, { getThemeColor } from 'app/core/utils/colors';
  4. export enum LogLevel {
  5. crit = 'critical',
  6. critical = 'critical',
  7. warn = 'warning',
  8. warning = 'warning',
  9. err = 'error',
  10. error = 'error',
  11. info = 'info',
  12. debug = 'debug',
  13. trace = 'trace',
  14. unkown = 'unkown',
  15. }
  16. export const LogLevelColor = {
  17. [LogLevel.critical]: colors[7],
  18. [LogLevel.warning]: colors[1],
  19. [LogLevel.error]: colors[4],
  20. [LogLevel.info]: colors[0],
  21. [LogLevel.debug]: colors[5],
  22. [LogLevel.trace]: colors[2],
  23. [LogLevel.unkown]: getThemeColor('#8e8e8e', '#dde4ed'),
  24. };
  25. export interface LogSearchMatch {
  26. start: number;
  27. length: number;
  28. text: string;
  29. }
  30. export interface LogRow {
  31. duplicates?: number;
  32. entry: string;
  33. key: string; // timestamp + labels
  34. labels: LogsStreamLabels;
  35. logLevel: LogLevel;
  36. searchWords?: string[];
  37. timestamp: string; // ISO with nanosec precision
  38. timeFromNow: string;
  39. timeEpochMs: number;
  40. timeLocal: string;
  41. uniqueLabels?: LogsStreamLabels;
  42. }
  43. export interface LogsLabelStat {
  44. active?: boolean;
  45. count: number;
  46. proportion: number;
  47. value: string;
  48. }
  49. export enum LogsMetaKind {
  50. Number,
  51. String,
  52. LabelsMap,
  53. }
  54. export interface LogsMetaItem {
  55. label: string;
  56. value: string | number | LogsStreamLabels;
  57. kind: LogsMetaKind;
  58. }
  59. export interface LogsModel {
  60. id: string; // Identify one logs result from another
  61. meta?: LogsMetaItem[];
  62. rows: LogRow[];
  63. series?: TimeSeries[];
  64. }
  65. export interface LogsStream {
  66. labels: string;
  67. entries: LogsStreamEntry[];
  68. search?: string;
  69. parsedLabels?: LogsStreamLabels;
  70. uniqueLabels?: LogsStreamLabels;
  71. }
  72. export interface LogsStreamEntry {
  73. line: string;
  74. timestamp: string;
  75. }
  76. export interface LogsStreamLabels {
  77. [key: string]: string;
  78. }
  79. export enum LogsDedupDescription {
  80. none = 'No de-duplication',
  81. exact = 'De-duplication of successive lines that are identical, ignoring ISO datetimes.',
  82. numbers = 'De-duplication of successive lines that are identical when ignoring numbers, e.g., IP addresses, latencies.',
  83. signature = 'De-duplication of successive lines that have identical punctuation and whitespace.',
  84. }
  85. export enum LogsDedupStrategy {
  86. none = 'none',
  87. exact = 'exact',
  88. numbers = 'numbers',
  89. signature = 'signature',
  90. }
  91. export interface LogsParser {
  92. /**
  93. * Value-agnostic matcher for a field label.
  94. * Used to filter rows, and first capture group contains the value.
  95. */
  96. buildMatcher: (label: string) => RegExp;
  97. /**
  98. * Returns all parsable substrings from a line, used for highlighting
  99. */
  100. getFields: (line: string) => string[];
  101. /**
  102. * Gets the label name from a parsable substring of a line
  103. */
  104. getLabelFromField: (field: string) => string;
  105. /**
  106. * Gets the label value from a parsable substring of a line
  107. */
  108. getValueFromField: (field: string) => string;
  109. /**
  110. * Function to verify if this is a valid parser for the given line.
  111. * The parser accepts the line unless it returns undefined.
  112. */
  113. test: (line: string) => any;
  114. }
  115. const LOGFMT_REGEXP = /(?:^|\s)(\w+)=("[^"]*"|\S+)/;
  116. export const LogsParsers: { [name: string]: LogsParser } = {
  117. JSON: {
  118. buildMatcher: label => new RegExp(`(?:{|,)\\s*"${label}"\\s*:\\s*"?([\\d\\.]+|[^"]*)"?`),
  119. getFields: line => {
  120. const fields = [];
  121. try {
  122. const parsed = JSON.parse(line);
  123. _.map(parsed, (value, key) => {
  124. const fieldMatcher = new RegExp(`"${key}"\\s*:\\s*"?${_.escapeRegExp(JSON.stringify(value))}"?`);
  125. const match = line.match(fieldMatcher);
  126. if (match) {
  127. fields.push(match[0]);
  128. }
  129. });
  130. } catch {}
  131. return fields;
  132. },
  133. getLabelFromField: field => (field.match(/^"(\w+)"\s*:/) || [])[1],
  134. getValueFromField: field => (field.match(/:\s*(.*)$/) || [])[1],
  135. test: line => {
  136. try {
  137. return JSON.parse(line);
  138. } catch (error) {}
  139. },
  140. },
  141. logfmt: {
  142. buildMatcher: label => new RegExp(`(?:^|\\s)${label}=("[^"]*"|\\S+)`),
  143. getFields: line => {
  144. const fields = [];
  145. line.replace(new RegExp(LOGFMT_REGEXP, 'g'), substring => {
  146. fields.push(substring.trim());
  147. return '';
  148. });
  149. return fields;
  150. },
  151. getLabelFromField: field => (field.match(LOGFMT_REGEXP) || [])[1],
  152. getValueFromField: field => (field.match(LOGFMT_REGEXP) || [])[2],
  153. test: line => LOGFMT_REGEXP.test(line),
  154. },
  155. };
  156. export function calculateFieldStats(rows: LogRow[], extractor: RegExp): LogsLabelStat[] {
  157. // Consider only rows that satisfy the matcher
  158. const rowsWithField = rows.filter(row => extractor.test(row.entry));
  159. const rowCount = rowsWithField.length;
  160. // Get field value counts for eligible rows
  161. const countsByValue = _.countBy(rowsWithField, row => (row as LogRow).entry.match(extractor)[1]);
  162. const sortedCounts = _.chain(countsByValue)
  163. .map((count, value) => ({ count, value, proportion: count / rowCount }))
  164. .sortBy('count')
  165. .reverse()
  166. .value();
  167. return sortedCounts;
  168. }
  169. export function calculateLogsLabelStats(rows: LogRow[], label: string): LogsLabelStat[] {
  170. // Consider only rows that have the given label
  171. const rowsWithLabel = rows.filter(row => row.labels[label] !== undefined);
  172. const rowCount = rowsWithLabel.length;
  173. // Get label value counts for eligible rows
  174. const countsByValue = _.countBy(rowsWithLabel, row => (row as LogRow).labels[label]);
  175. const sortedCounts = _.chain(countsByValue)
  176. .map((count, value) => ({ count, value, proportion: count / rowCount }))
  177. .sortBy('count')
  178. .reverse()
  179. .value();
  180. return sortedCounts;
  181. }
  182. 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;
  183. function isDuplicateRow(row: LogRow, other: LogRow, strategy: LogsDedupStrategy): boolean {
  184. switch (strategy) {
  185. case LogsDedupStrategy.exact:
  186. // Exact still strips dates
  187. return row.entry.replace(isoDateRegexp, '') === other.entry.replace(isoDateRegexp, '');
  188. case LogsDedupStrategy.numbers:
  189. return row.entry.replace(/\d/g, '') === other.entry.replace(/\d/g, '');
  190. case LogsDedupStrategy.signature:
  191. return row.entry.replace(/\w/g, '') === other.entry.replace(/\w/g, '');
  192. default:
  193. return false;
  194. }
  195. }
  196. export function dedupLogRows(logs: LogsModel, strategy: LogsDedupStrategy): LogsModel {
  197. if (strategy === LogsDedupStrategy.none) {
  198. return logs;
  199. }
  200. const dedupedRows = logs.rows.reduce((result: LogRow[], row: LogRow, index, list) => {
  201. const previous = result[result.length - 1];
  202. if (index > 0 && isDuplicateRow(row, previous, strategy)) {
  203. previous.duplicates++;
  204. } else {
  205. row.duplicates = 0;
  206. result.push(row);
  207. }
  208. return result;
  209. }, []);
  210. return {
  211. ...logs,
  212. rows: dedupedRows,
  213. };
  214. }
  215. export function getParser(line: string): LogsParser {
  216. let parser;
  217. try {
  218. if (LogsParsers.JSON.test(line)) {
  219. parser = LogsParsers.JSON;
  220. }
  221. } catch (error) {}
  222. if (!parser && LogsParsers.logfmt.test(line)) {
  223. parser = LogsParsers.logfmt;
  224. }
  225. return parser;
  226. }
  227. export function filterLogLevels(logs: LogsModel, hiddenLogLevels: Set<LogLevel>): LogsModel {
  228. if (hiddenLogLevels.size === 0) {
  229. return logs;
  230. }
  231. const filteredRows = logs.rows.reduce((result: LogRow[], row: LogRow, index, list) => {
  232. if (!hiddenLogLevels.has(row.logLevel)) {
  233. result.push(row);
  234. }
  235. return result;
  236. }, []);
  237. return {
  238. ...logs,
  239. rows: filteredRows,
  240. };
  241. }
  242. export function makeSeriesForLogs(rows: LogRow[], intervalMs: number): TimeSeries[] {
  243. // currently interval is rangeMs / resolution, which is too low for showing series as bars.
  244. // need at least 10px per bucket, so we multiply interval by 10. Should be solved higher up the chain
  245. // when executing queries & interval calculated and not here but this is a temporary fix.
  246. // intervalMs = intervalMs * 10;
  247. // Graph time series by log level
  248. const seriesByLevel = {};
  249. const bucketSize = intervalMs * 10;
  250. const seriesList = [];
  251. for (const row of rows) {
  252. let series = seriesByLevel[row.logLevel];
  253. if (!series) {
  254. seriesByLevel[row.logLevel] = series = {
  255. lastTs: null,
  256. datapoints: [],
  257. alias: row.logLevel,
  258. color: LogLevelColor[row.logLevel],
  259. };
  260. seriesList.push(series);
  261. }
  262. // align time to bucket size
  263. const time = Math.round(row.timeEpochMs / bucketSize) * bucketSize;
  264. // Entry for time
  265. if (time === series.lastTs) {
  266. series.datapoints[series.datapoints.length - 1][0]++;
  267. } else {
  268. series.datapoints.push([1, time]);
  269. series.lastTs = time;
  270. }
  271. // add zero to other levels to aid stacking so each level series has same number of points
  272. for (const other of seriesList) {
  273. if (other !== series && other.lastTs !== time) {
  274. other.datapoints.push([0, time]);
  275. other.lastTs = time;
  276. }
  277. }
  278. }
  279. return seriesList.map(series => {
  280. series.datapoints.sort((a, b) => {
  281. return a[1] - b[1];
  282. });
  283. return new TimeSeries(series);
  284. });
  285. }