logs_model.ts 8.9 KB

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