nxt-aws-client
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -9,2 +9,5 @@ export interface IClientConfig { | ||
}; | ||
cloudwatch: { | ||
namespace: string; | ||
}; | ||
s3: { | ||
@@ -11,0 +14,0 @@ bucket: string; |
@@ -19,2 +19,5 @@ "use strict"; | ||
}, | ||
cloudwatch: { | ||
namespace: `nxt-${env}-client`, | ||
}, | ||
s3: { | ||
@@ -21,0 +24,0 @@ bucket: `nxt-${env}-import`, |
@@ -12,3 +12,6 @@ import { IClientConfig } from '../config'; | ||
private readonly requestsBus; | ||
private readonly stats; | ||
private readonly cloudwatch; | ||
private id; | ||
private statsTimer; | ||
constructor(config: IClientConfig, options: ISocketClientOptions); | ||
@@ -18,2 +21,4 @@ predictOffer(request: IOfferPredictRequest): Promise<IOfferPredictResponse>; | ||
private createClient; | ||
private collectStats; | ||
private flushStats; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const api_1 = require("../domain/api"); | ||
const AWS = require("aws-sdk"); | ||
const WebSocket = require("ws"); | ||
const events_1 = require("events"); | ||
const PING_INTERVAL = 20000; | ||
var ESTatsType; | ||
(function (ESTatsType) { | ||
ESTatsType["RequestElapsed"] = "request_elapsed"; | ||
ESTatsType["RequestErrors"] = "request_errors"; | ||
ESTatsType["ConnectionErrors"] = "connection_errors"; | ||
})(ESTatsType || (ESTatsType = {})); | ||
const PING_INTERVAL = 20 * 1000; | ||
const STATS_INTERVAL = 30 * 1000; | ||
class SocketClient { | ||
@@ -11,7 +19,13 @@ constructor(config, options) { | ||
this.options = options; | ||
this.stats = Object.values(ESTatsType).reduce((res, x) => { | ||
res[x] = []; | ||
return res; | ||
}, {}); | ||
this.id = 0; | ||
this.options.logger.debug({ url: config.socket.url }, 'Connecting to WS server.'); | ||
this.cloudwatch = new AWS.CloudWatch({ region: config.region }); | ||
this.requestsBus = new events_1.EventEmitter(); | ||
this.requestsBus.setMaxListeners(0); | ||
this.createClient(); | ||
this.statsTimer = setTimeout(() => this.flushStats(), STATS_INTERVAL); | ||
} | ||
@@ -29,2 +43,3 @@ async predictOffer(request) { | ||
let timeoutId; | ||
const sentAt = Date.now(); | ||
const handler = (response) => { | ||
@@ -40,2 +55,3 @@ if (response.id !== request.id) { | ||
} | ||
this.collectStats(ESTatsType.RequestElapsed, Date.now() - sentAt); | ||
resolve(response.payload); | ||
@@ -45,2 +61,3 @@ }; | ||
this.requestsBus.removeListener('event', handler); | ||
this.collectStats(ESTatsType.RequestErrors, 1); | ||
reject(new Error('Request timeout.')); | ||
@@ -66,3 +83,6 @@ }, 5 * 1000); | ||
}); | ||
this.client.on('error', error => this.options.logger.error({ error }, 'Socket error.')); | ||
this.client.on('error', error => { | ||
this.options.logger.error({ error }, 'Socket error.'); | ||
this.collectStats(ESTatsType.ConnectionErrors, 1); | ||
}); | ||
this.client.on('close', (code, reason) => { | ||
@@ -78,2 +98,3 @@ this.options.logger.debug({ code, reason }, 'Socket closed.'); | ||
}, 'Unexpected socket response.'); | ||
this.collectStats(ESTatsType.ConnectionErrors, 1); | ||
}); | ||
@@ -126,3 +147,46 @@ let pingTimeout, pongTimeout; | ||
} | ||
collectStats(type, value) { | ||
const length = this.stats[type].push(value); | ||
if (length >= 150) { | ||
this.flushStats(); | ||
} | ||
} | ||
flushStats() { | ||
clearTimeout(this.statsTimer); | ||
this.statsTimer = setTimeout(() => this.flushStats(), STATS_INTERVAL); | ||
const metrics = Object.keys(this.stats) | ||
.filter(type => this.stats[type].length > 0); | ||
if (metrics.length === 0) { | ||
return; | ||
} | ||
this.cloudwatch.putMetricData({ | ||
Namespace: this.config.cloudwatch.namespace, | ||
MetricData: metrics | ||
.map((type) => { | ||
const data = this.stats[type]; | ||
this.stats[type] = []; | ||
const counts = []; | ||
const values = data.reduce((res, value) => { | ||
const index = res.indexOf(value); | ||
if (index === -1) { | ||
res.push(value); | ||
counts.push(1); | ||
} | ||
else { | ||
counts[index] += 1; | ||
} | ||
return res; | ||
}, []); | ||
return { | ||
MetricName: type, | ||
Counts: counts, | ||
Values: values, | ||
}; | ||
}), | ||
}).promise() | ||
.catch((error) => { | ||
this.options.logger.error({ error }, 'Metric send error.'); | ||
}); | ||
} | ||
} | ||
exports.SocketClient = SocketClient; |
{ | ||
"name": "nxt-aws-client", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"main": "./dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./dist/index.d.ts", |
25539
850