Skip to content

Commit

Permalink
move from request to axios (#97)
Browse files Browse the repository at this point in the history
* move from request to axios

* update moment to 2.29.4

* create instance of axios

* update from axiosIn to axiosInstance fix linting
  • Loading branch information
resdenia authored Jul 26, 2022
1 parent ab2a44a commit 7e81339
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 131 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ A few notes are worth mentioning regarding the use of the UDP protocol:


## Update log
**2.1.4**
- Replace from request to axios

**2.0.4**
- Add parameter to manage User-agent

Expand Down
1 change: 0 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const logger = logzioLogger.createLogger({
timeout: 1000,
});


logger.log('some testing');

for (let i = 0; i < count; i++) {
Expand Down
5 changes: 5 additions & 0 deletions lib/axiosInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const axios = require('axios');

const axiosInstance = axios.create();

module.exports = axiosInstance;
38 changes: 19 additions & 19 deletions lib/logzio-nodejs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const request = require('request-promise');
const stringifySafe = require('json-stringify-safe');
const assign = require('lodash.assign');
const dgram = require('dgram');
const zlib = require('zlib');
const axiosInstance = require('./axiosInstance');


const nanoSecDigits = 9;

Expand Down Expand Up @@ -78,6 +79,17 @@ class LogzioLogger {

this.protocol = protocol;
this._setProtocol(port);
this.url = `${this.protocol}://${this.host}:${this.port}?token=${this.token}`;

this.axiosInstance = axiosInstance;
this.axiosInstance.defaults.headers.post = {
Host: this.host,
Accept: '*/*',
'Content-Type': 'text/plain',
...(this.setUserAgent ? { 'user-agent': USER_AGENT } : {}),
...(this.compress ? { 'content-encoding': 'gzip' } : {}),

};

/*
Callback method executed on each bulk of messages sent to logzio.
Expand All @@ -92,7 +104,6 @@ class LogzioLogger {
this.timeout = timeout;

// build the url for logging
this.url = `${this.protocol}://${this.host}:${this.port}?token=${this.token}`;

this.messages = [];
this.bulkId = 1;
Expand Down Expand Up @@ -254,44 +265,33 @@ class LogzioLogger {

_send(bulk) {
const body = messagesToBody(bulk.msgs);
const options = {
uri: this.url,
headers: {
host: this.host,
accept: '*/*',
'content-type': 'text/plain',
...(this.setUserAgent ? { 'user-agent': USER_AGENT } : {}),
},
};

if (typeof this.timeout !== 'undefined') {
options.timeout = this.timeout;
this.axiosInstance.defaults.timeout = this.timeout;
}

return Promise.resolve()
.then(() => {
if (this.compress) {
options.headers['content-encoding'] = 'gzip';
return zlibPromised(body);
}
return body;
})
.then((finalBody) => {
options.body = finalBody;
this._tryToSend(options, bulk);
this._tryToSend(finalBody, bulk);
});
}

_tryToSend(options, bulk) {
this._debug(`Sending bulk of ${bulk.msgs.length} logs`)
return request.post(options)
_tryToSend(body, bulk) {
this._debug(`Sending bulk of ${bulk.msgs.length} logs`);
return this.axiosInstance.post(this.url, body)
.then(() => {
this._debug(`Bulk #${bulk.id} - sent successfully`);
this.callback();
})
.catch((err) => {
// In rare cases server is busy
const errorCode = err.cause && err.cause.code;
const errorCode = err.code;
if (UNAVAILABLE_CODES.includes(errorCode)) {
if (bulk.attemptNumber >= this.numberOfRetries) {
return this.callback(new Error(`Failed after ${bulk.attemptNumber} retries on error = ${err}`), bulk);
Expand Down
Loading

0 comments on commit 7e81339

Please sign in to comment.