Skip to content

Commit

Permalink
feat: US124288 - Add Ability to Throttle Logs (#7)
Browse files Browse the repository at this point in the history
* Add ability to throttle logs in the client, only sending the same log/error once per minute
* Remove semantic-release from package.json
* Add tests
  • Loading branch information
svanherk authored Mar 11, 2021
1 parent e9c6def commit f5ff8aa
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 145 deletions.
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @dlockhart
* @dlockhart @svanherk
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const batchSize = 500;
const batchTime = 5000;

const serverLogger = new ServerLogger(batchSize, batchTime);
export function createClient(appId) {
return new LoggingClient(appId, serverLogger);
export function createClient(appId, opts) {
return new LoggingClient(appId, serverLogger, opts);
}
43 changes: 36 additions & 7 deletions logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const _isFiniteNumber = (val) => val !== null && isFinite(val) && !isNaN(val);

const dataLoggingEndpointAttribute = 'data-logging-endpoint';
const defaultThrottleRateMs = 60000;

export class ServerLogger {

Expand Down Expand Up @@ -157,9 +158,12 @@ export class LogBuilder {
}

export class LoggingClient {
constructor(appId, logger) {
constructor(appId, logger, opts) {
this._appId = appId;
this._logger = logger;
this._shouldThrottle = opts ? !!opts.shouldThrottle : false;

this._uniqueLogs = new Map();
}

log(developerMessage) {
Expand All @@ -170,8 +174,11 @@ export class LoggingClient {
const logs = developerMessages.map(developerMessage => new LogBuilder(this._appId)
.withMessage(developerMessage)
.withLocation()
.build());
this._logger.logBatch(logs);
.build()
).filter(this._throttle.bind(this));
if (logs.length > 0) {
this._logger.logBatch(logs);
}
}

error(error, developerMessage) {
Expand All @@ -183,8 +190,11 @@ export class LoggingClient {
.withError(error)
.withMessage(developerMessage)
.withLocation()
.build());
this._logger.logBatch(logs);
.build()
).filter(this._throttle.bind(this));
if (logs.length > 0) {
this._logger.logBatch(logs);
}
}

legacyError(message, source, lineno, colno, error, developerMessage) {
Expand All @@ -197,7 +207,26 @@ export class LoggingClient {
.withError(error)
.withMessage(developerMessage)
.withLocation()
.build());
this._logger.logBatch(logs);
.build()
).filter(this._throttle.bind(this));
if (logs.length > 0) {
this._logger.logBatch(logs);
}
}

_throttle(log) {
if (!this._shouldThrottle) {
return true;
}

const now = new Date().getTime();
const key = JSON.stringify(log);
const lastLogged = this._uniqueLogs.get(key);
if (lastLogged === undefined || now - lastLogged >= defaultThrottleRateMs) {
this._uniqueLogs.set(key, now);
return true;
} else {
return false;
}
}
}
20 changes: 1 addition & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,7 @@
"eslint-plugin-sort-class-members": "^1",
"eslint": "^6",
"karma-sauce-launcher": "^2",
"semantic-release": "^17.1.1",
"sinon": "^9.0.3"
},
"dependencies": {},
"release": {
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/github",
"@semantic-release/npm",
"@semantic-release/release-notes-generator",
[
"@semantic-release/git",
{
"assets": [
"package.json"
],
"message": "chore(release): ${nextRelease.version} [skip ci]"
}
]
]
}
"dependencies": {}
}
Loading

0 comments on commit f5ff8aa

Please sign in to comment.