Skip to content

Commit

Permalink
fix(logstash-http): rework logs queuing
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Sep 15, 2023
1 parent 7df2ba5 commit a764419
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions packages/logstash-http/src/LogStashHttpAppender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export class LogStashHttpAppender extends BaseAppender<LogStashHttpOptions> {
...this.config.options.retryOptions
});
}

if (this.config.options.delayToFlush) {
this.#timer = setInterval(() => this.flush(), this.config.options.delayToFlush);
}
}

write(loggingEvent: LogEvent) {
Expand All @@ -90,14 +94,9 @@ export class LogStashHttpAppender extends BaseAppender<LogStashHttpOptions> {
const {bufferMax = 0, delayToFlush = 0} = this.config.options;
this.#buffer.push(bulk);

if (!bufferMax || bufferMax <= this.#buffer.length) {
if ((!bufferMax && !delayToFlush) || (bufferMax && bufferMax <= this.#buffer.length)) {
return this.flush();
}

if (delayToFlush) {
this.#timer && clearTimeout(this.#timer);
this.#timer = setTimeout(() => this.flush(), delayToFlush);
}
}

async flush() {
Expand All @@ -111,14 +110,18 @@ export class LogStashHttpAppender extends BaseAppender<LogStashHttpOptions> {
const {url, application, logType, requireAlias, debug} = this.config.options;
const _index = typeof application === "function" ? application() : application;

const bulkData = buffer.flatMap((item) => [JSON.stringify({
index: {
_index,
_type: logType,
require_alias: requireAlias,
_id: v4()
}
}), item]);
const bulkData = buffer.flatMap((item) => [
JSON.stringify({
index: {
_index,
_type: logType,
require_alias: requireAlias,
_id: v4()
}
}),
item
]);

try {
const result = await this.client({
url: "",
Expand Down Expand Up @@ -152,6 +155,7 @@ export class LogStashHttpAppender extends BaseAppender<LogStashHttpOptions> {
}

shutdown() {
this.#timer && clearInterval(this.#timer);
return this.flush();
}
}

0 comments on commit a764419

Please sign in to comment.