Skip to content

Commit 3a7b8e9

Browse files
committed
fix(logstash-http): clean unnecessary dependencies and fix data serialization/mapping
1 parent 1a4e4a6 commit 3a7b8e9

19 files changed

+100
-113
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.

docs/appenders/logstash-http.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ npm install --save @tsed/logger-logstash-http
1717
- `options.logChannel` - `string` (optional) - also used to identify your application's logs [but in a more specific way]
1818
- `options.logType` - `string` (optional) - used for the `type` field in the logstash data
1919
- `options.timeout` - `integer` (optional, defaults to 5000ms) - the timeout for the HTTP request.
20+
- `options.delayToFlush` - `integer` (optional, defaults to 0) - the delay before flushing buffer if the max buffer isn't reached.
2021
- `options.maxBuffer` - `integer` (optional, defaults to 0) - Group bulk request by the maxBuffer number. By Default the buffer is disabled.
2122
- `options.retriesOptions` - `object` (optional) - Configure retries strategy. See [axios-retry](https://www.google.com/search?client=firefox-b-d&q=axios-retry) options for more details.
2223

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,4 @@
113113
]
114114
},
115115
"packageManager": "yarn@3.2.1"
116-
}
116+
}

packages/connect/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
"peerDependencies": {
4848
"@tsed/logger": "6.3.0"
4949
}
50-
}
50+
}

packages/file/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
"peerDependencies": {
4949
"@tsed/logger": "6.3.0"
5050
}
51-
}
51+
}

packages/insight/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
"peerDependencies": {
4949
"@tsed/logger": "6.3.0"
5050
}
51-
}
51+
}

packages/logentries/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
"peerDependencies": {
4949
"@tsed/logger": "6.3.0"
5050
}
51-
}
51+
}

packages/logger/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,5 @@
4545
},
4646
"devDependencies": {
4747
"typescript": "4.7.4"
48-
},
49-
"peerDependencies": {}
50-
}
48+
}
49+
}

packages/loggly/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@
4949
"peerDependencies": {
5050
"@tsed/logger": "6.3.0"
5151
}
52-
}
52+
}

packages/logstash-http/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
},
4141
"homepage": "https://github.com/tsedio/logger",
4242
"dependencies": {
43-
"@types/axios": "^0.14.0",
44-
"axios": "^0.26.1",
43+
"axios": "0.27.2",
4544
"axios-retry": "^3.3.1",
4645
"tslib": "2.3.1"
4746
},
@@ -51,4 +50,4 @@
5150
"peerDependencies": {
5251
"@tsed/logger": "6.3.0"
5352
}
54-
}
53+
}

packages/logstash-http/src/LogStashHttpAppender.ts

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class LogStashHttpOptions {
2929
logChannel: string;
3030
auth?: AxiosBasicCredentials;
3131
timeout?: number;
32+
delayToFlush?: number;
3233
params?: Record<string, any>;
3334
headers?: Record<string, any>;
3435
retryOptions?: IAxiosRetryConfig;
@@ -39,6 +40,7 @@ export class LogStashHttpAppender extends BaseAppender<LogStashHttpOptions> {
3940
private client: ReturnType<typeof axios.create>;
4041

4142
#buffer: Record<string, any>[] = [];
43+
#timer: NodeJS.Timeout;
4244

4345
build() {
4446
if ($log.level !== "OFF" && this.config.options) {
@@ -48,10 +50,8 @@ export class LogStashHttpAppender extends BaseAppender<LogStashHttpOptions> {
4850
timeout: this.config.options.timeout || 5000,
4951
params: this.config.options.params,
5052
headers: {
51-
...(this.config.options.headers || {}),
52-
"Content-Type": "application/x-ndjson"
53-
},
54-
withCredentials: true
53+
...(this.config.options.headers || {})
54+
}
5555
});
5656

5757
axiosRetry(this.client, {
@@ -67,67 +67,83 @@ export class LogStashHttpAppender extends BaseAppender<LogStashHttpOptions> {
6767
const level = loggingEvent.level.toString().toLowerCase();
6868

6969
if (level !== "off") {
70-
const logstashEvent = [
71-
{
72-
...loggingEvent.getData(),
73-
message: format(loggingEvent.getMessage()),
74-
context: loggingEvent.context.toJSON(),
75-
level: loggingEvent.level.level / 100,
76-
level_name: level,
77-
channel: logChannel,
78-
datetime: new Date(loggingEvent.startTime).toISOString()
79-
}
80-
];
81-
82-
this.send(logstashEvent);
70+
const {time, ...props} = loggingEvent.getData();
71+
72+
this.send({
73+
...props,
74+
message: format(loggingEvent.getMessage()),
75+
context: loggingEvent.context.toJSON(),
76+
level: loggingEvent.level.level / 100,
77+
level_name: level,
78+
channel: logChannel,
79+
datetime: new Date(time || loggingEvent.startTime).toISOString()
80+
});
8381
}
8482
}
8583

8684
send(bulk: Record<string, any>) {
87-
const {bufferMax = 0} = this.config.options;
85+
const {bufferMax = 0, delayToFlush = 0} = this.config.options;
8886
this.#buffer.push(bulk);
8987

88+
if (delayToFlush) {
89+
this.#timer && clearTimeout(this.#timer);
90+
this.#timer = setTimeout(() => this.flush(), delayToFlush);
91+
}
92+
9093
if (bufferMax <= this.#buffer.length) {
9194
this.#buffer.push(bulk);
9295
return this.flush();
9396
}
9497
}
9598

96-
flush() {
99+
async flush() {
97100
// send to server
98101
const buffer = this.#buffer;
99102
this.#buffer = [];
100-
101103
if (buffer.length) {
102-
const {url} = this.config.options;
103-
const {application, logType} = this.config.options;
104-
105-
const header = JSON.stringify({
106-
index: {
107-
_index: typeof application === "function" ? application() : application,
108-
_type: logType
109-
}
110-
});
104+
return this;
105+
}
111106

112-
const bulkData =
113-
buffer
114-
.flatMap((obj) => {
115-
return [header, JSON.stringify(obj)];
116-
}, [])
117-
.join("\n") + "\n";
118-
119-
return this.client.post("", bulkData).catch((error) => {
120-
if (error.response) {
121-
console.error(
122-
`Ts.ED Logger.logstash-http Appender error posting to ${url}: ${error.response.status} - ${JSON.stringify(error.response.data)}`
123-
);
124-
return;
107+
const {url} = this.config.options;
108+
const {application, logType} = this.config.options;
109+
const _index = typeof application === "function" ? application() : application;
110+
111+
const action = JSON.stringify({
112+
index: {
113+
_index,
114+
_type: logType
115+
}
116+
});
117+
118+
const bulkData = buffer.flatMap((item) => [action, item]);
119+
try {
120+
await this.client({
121+
url: "",
122+
method: "POST",
123+
data: this.serializeBulk(bulkData),
124+
headers: {
125+
"Content-Type": "application/x-ndjson"
125126
}
126-
console.error(`Ts.ED Logger.logstash-http Appender error: ${error.message}`);
127127
});
128+
} catch (error) {
129+
if (error.response) {
130+
console.error(
131+
`Ts.ED Logger.logstash-http Appender error posting to ${url}: ${error.response.status} - ${JSON.stringify(error.response.data)}`
132+
);
133+
return;
134+
}
135+
console.error(`Ts.ED Logger.logstash-http Appender error: ${error.message}`);
128136
}
129137
}
130138

139+
serializeBulk(array: Array<Record<string, any> | string>): string {
140+
return array.reduce<string>((ndjson, obj) => {
141+
const str = typeof obj === "string" ? obj : JSON.stringify(obj);
142+
143+
return ndjson + str + "\n";
144+
}, "");
145+
}
146+
131147
shutdown() {
132148
return this.flush();
133149
}

packages/logstash-udp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
"peerDependencies": {
4949
"@tsed/logger": "6.3.0"
5050
}
51-
}
51+
}

packages/rabbitmq/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@
4949
"peerDependencies": {
5050
"@tsed/logger": "6.3.0"
5151
}
52-
}
52+
}

packages/seq/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
"peerDependencies": {
4949
"@tsed/logger": "6.3.0"
5050
}
51-
}
51+
}

packages/slack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
"peerDependencies": {
4949
"@tsed/logger": "6.3.0"
5050
}
51-
}
51+
}

packages/smtp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
"peerDependencies": {
4949
"@tsed/logger": "6.3.0"
5050
}
51-
}
51+
}

0 commit comments

Comments
 (0)