Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Use axios
Browse files Browse the repository at this point in the history
  • Loading branch information
İbrahim Gürses committed Jul 20, 2019
1 parent 2917c68 commit ba8eb24
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 74 deletions.
43 changes: 37 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -85,6 +85,7 @@
},
"dependencies": {
"@thundra/warmup": "1.0.0",
"axios": "^0.19.0",
"falafel": "^2.1.0",
"koalas": "^1.0.2",
"lodash.flatten": "^4.4.0",
Expand Down
89 changes: 21 additions & 68 deletions src/Reporter.ts
Expand Up @@ -8,56 +8,21 @@ import ThundraLogger from './ThundraLogger';
import ThundraConfig from './plugins/config/ThundraConfig';
import BaseMonitoringData from './plugins/data/base/BaseMonitoringData';
import MonitoringDataType from './plugins/data/base/MonitoringDataType';
const axios = require('axios');

const httpAgent = new http.Agent({
keepAlive: true,
});
const httpsAgent = new https.Agent({
maxCachedSessions: 1,
keepAlive: true,
const session = axios.create({
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
});

class Reporter {
private readonly MAX_MONITOR_DATA_BATCH_SIZE: number = 100;
private reports: any[];
private config: ThundraConfig;
private useHttps: boolean;
private requestOptions: http.RequestOptions;

constructor(config: ThundraConfig, u?: url.URL) {
this.reports = [];
this.config = config ? config : new ThundraConfig({});
this.useHttps = (u ? u.protocol : URL.protocol) === 'https:';
this.requestOptions = this.createRequestOptions();
}

createRequestOptions(u?: url.URL): http.RequestOptions {
const path = this.config.enableCompositeData ?
COMPOSITE_MONITORING_DATA_PATH : MONITORING_DATA_PATH;

return {
method: 'POST',
hostname: u ? u.hostname : URL.hostname,
path: (u ? u.pathname : URL.pathname) + path,
port: parseInt(u ? u.port : URL.port, 0),
headers: {
'Content-Type': 'application/json',
'Authorization': 'ApiKey ' + this.config.apiKey,
},
agent: this.useHttps ? httpsAgent : httpAgent,
createConnection: (options: http.ClientRequestArgs, oncreate: (err: Error, socket: net.Socket) => void) => {
try {
const socket: net.Socket = net.createConnection(options.port as number, options.hostname);
socket.setNoDelay(true);
socket.setKeepAlive(true);
oncreate(null, socket);
return socket;
} catch (e) {
oncreate(e, null);
throw e;
}
},
};
}

addReport(report: any): void {
Expand Down Expand Up @@ -129,7 +94,7 @@ class Reporter {
if (isAsync) {
reportPromises.push(this.writeBatchToCW(batch, isComposite));
} else {
reportPromises.push(this.request(batch));
reportPromises.push(this.post(batch));
}
});

Expand All @@ -138,36 +103,24 @@ class Reporter {
});
}

request(batch: any[]): Promise<any> {
post(batch: any[]): Promise<any> {
return new Promise((resolve, reject) => {
let request: http.ClientRequest;
const responseHandler = (response: http.IncomingMessage) => {
let responseData = '';
response.on('data', (chunk: Buffer | string) => {
responseData += chunk;
});
response.on('end', () => {
if (response.statusCode !== 200) {
ThundraLogger.getInstance().debug(JSON.stringify(this.reports));
return reject({ status: response.statusCode, data: responseData });
}
return resolve({ status: response.statusCode, data: responseData });
});
};

this.useHttps
? request = https.request(this.requestOptions, responseHandler)
: request = http.request(this.requestOptions, responseHandler);

request.on('error', (error: any) => {
return reject(error);
const path = this.config.enableCompositeData ?
COMPOSITE_MONITORING_DATA_PATH : MONITORING_DATA_PATH;
session.post(
URL.protocol + '//' + URL.hostname + URL.path + path,
batch,
{
headers: {
'Content-Type': 'application/json',
'Authorization': 'ApiKey ' + this.config.apiKey,
},
},
).then((res: any) => {
resolve(res);
}).catch((err: any) => {
reject(err);
});
try {
request.write(JSON.stringify(batch));
request.end();
} catch (error) {
ThundraLogger.getInstance().error('Cannot serialize report data. ' + error);
}
});
}

Expand Down

0 comments on commit ba8eb24

Please sign in to comment.