Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt to unmangle logs from log aggregators #827

Merged
merged 4 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/logreader-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/logreader-main.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"webpack-cli": "^4.10.0"
},
"engines": {
"node": "^14.0.0",
"npm": "^7.0.0"
"node": "^16.0.0",
"npm": "^8.0.0"
},
"dependencies": {
"@babel/runtime": "^7.19.0",
Expand Down
14 changes: 13 additions & 1 deletion src/ExceptionParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ window.unserialize = unserialize;

export class ExceptionParser {
isException (logMessage) {
return this.isNewStyleException(logMessage) || this.isOldStyleException(logMessage) || this.isBackgroundJobException(logMessage);
return this.isNewStyleException(logMessage) || this.isOldStyleException(logMessage) || this.isBackgroundJobException(logMessage) || this.isNestedJsonException(logMessage);
}

isNewStyleException (logMessage) {
return logMessage.Exception;
}

isNestedJsonException (logMessage) {
return logMessage.substr && logMessage[0] === '{';
}

isOldStyleException (logMessage) {
return logMessage.substr && logMessage.substr(0, 12) === 'Exception: {';
}
Expand All @@ -34,6 +38,14 @@ export class ExceptionParser {
console.log(logMessage.substr(10));
console.error(e);
}
} else if (this.isNestedJsonException(logMessage)) {
try {
return this.tryParseJSON(logMessage);
} catch (e) {
console.log('Error while parsing exception:');
console.log(logMessage.substr(10));
console.error(e);
}
} else {
data = this.tryParseJSON(logMessage.substr(logMessage.indexOf('{"Exception":')));
const messageHead = logMessage.substr(0, logMessage.indexOf('{"Exception":'));
Expand Down
19 changes: 17 additions & 2 deletions src/LogParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import splitter from "json-string-splitter";
export function parseLog(raw: string): any[] {
try {
const lines = raw.split('\n');
return lines.map(tryParseJSON);
return lines.map(tryParseJSON).map(fixUpLogJson);
} catch (e) {
console.log("falling back to json splitter")
// the input might have had its data reformatted, breaking the original newline separated json
const lines = splitter(raw).jsons;
return lines.map(tryParseJSON);
return lines.map(tryParseJSON).map(fixUpLogJson);
}
}

Expand Down Expand Up @@ -36,3 +36,18 @@ function tryParseJSON(json: string): any {
}
}
}

/**
* attempt to fixup some mangling done by log aggregators to our json
* to increase the number of times we can just copy-paste logs directly from users.
*/
function fixUpLogJson(message: any): any {
if (message.content?.message && message.content?.attributes && message.id && !message.reqId) {
return {
message: message.content.message,
...message.content.attributes
}
}

return message;
}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

if (!defined('PHPUNIT_RUN')) {
define('PHPUNIT_RUN', 1);
define('PHPUNIT_RUN', 1);
}

require_once __DIR__ . '/../../../lib/base.php';
Expand Down