-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjsonLogScript
More file actions
137 lines (123 loc) · 5.05 KB
/
jsonLogScript
File metadata and controls
137 lines (123 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Function called to extract date, level, app name and message
//
// @param preJSONString: string - optional non-JSON string proceeding JSON object
// @param jsonObject: {} - JSON log data
// @returns {date: Date, level: string, category: string, appName: string, message: string, rawLine: string, additionalJSON: {} }
//
// category is the availability zone
// appName is the pod name
//
const extractDateLevelCategoryAppNameMessage = function (preJSONString, jsonObject) {
let level = 'info';
let date = new Date(0);
let category = '';
let kind = '';
let message = "Message field not defined - click '?'";
let additionalJSON = {};
const ignoreFields = [];
const typeahead = [];
// Kube object?
if (jsonObject.kind && jsonObject.metadata) {
kind = jsonObject.kind;
message = jsonObject.metadata.name;
if (jsonObject.metadata.creationTimestamp) {
date = new Date(jsonObject.metadata.creationTimestamp);
}
level = '';
additionalJSON['level'] = undefined;
// Errors detected by the parseJson plugin?
if (jsonObject['errors']) {
level = 'error';
additionalJSON['level'] = level;
}
}
else { // Try to dynamically find fields
let dateSet, levelSet, kindSet, messageSet = false;
const fieldValues = []; // array of {field, value} pairs
// Recursively traverse JSON and build fieldValues array
function traverseJson(obj) {
for (let field in obj) {
const value = obj[field];
if (typeof field === 'string' && (typeof value === 'string' || typeof value === 'number')) {
field = field.toLowerCase();
fieldValues.push({ field, value });
} else if (typeof value === 'object' && !Array.isArray(value)) {
traverseJson(value);
}
}
}
traverseJson(jsonObject);
// Check each JSON field,value pair looking for date, info, kind and message
for (const fieldValue of fieldValues) {
const field = fieldValue.field;
const value = fieldValue.value;
checkForDateLevelKindMessage(field, value);
if (dateSet && levelSet && kindSet && messageSet) break;
}
// Check for data, level, kind and message fields
function checkForDateLevelKindMessage(field, value) {
if (!dateSet) {
if ((field.includes('ts') || field.includes('time') || field.includes('date')) && isValidDate(value)) {
dateSet = true;
date = new Date(value);
return;
}
}
if (typeof value !== 'string') return;
if (!levelSet) {
if (field === 'level') {
levelSet = true;
level = value;
if (value.startsWith('err')) typeahead.push(field + ':' + value);
return;
} else if (field === 'severity') {
level = value;
return;
} else if (field === 'error') {
level = 'error';
return;
}
}
if (field === 'error' && value.length > 0) typeahead.push(field + ':*');
if (!kindSet) {
if (field === 'kind' || field === 'app' || field === 'appname' || field === 'applicationname' || field === 'actionname') {
kindSet = true;
kind = value;
return;
} else if (field.length <= 64 && (field.startsWith("thread") ||
field.startsWith('app') || field.endsWith('app'))) {
kind = value;
return;
}
}
if (!messageSet) {
if (field === 'message' || field === 'msg' || field === 'error') {
messageSet = true;
message = value;
return;
} else if (field.startsWith('message')) {
message = value;
return;
}
}
}
// Returns true, if this is a valid date
function isValidDate(value) {
try {
let date = new Date(value);
if (date.toString() === 'Invalid Date' && typeof value === 'string') {
const tokens = value.split(':', 2);
if (tokens.length === 2) {
let d = new Date(tokens[0]);
date = new Date(d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ':' + tokens[1]);
}
}
if (date.toString() === 'Invalid Date') return false;
} catch (e) {
return false;
}
return true;
}
}
return { date, level, category, kind, message, rawLine: undefined, additionalJSON, ignoreFields, typeahead };
};