openhim-core
Version:
The OpenHIM core application that provides logging and routing of http requests
230 lines (214 loc) • 12.4 kB
JavaScript
var SDC, application, config, domain, logger, os, sdc, statsdServer, timer, util;
config = require('./config/config');
SDC = require("statsd-client");
statsdServer = config.get('statsd');
application = config.get('application');
logger = require("winston");
os = require('os');
timer = new Date();
domain = os.hostname() + '.' + application.name;
util = require('util');
sdc = new SDC(statsdServer);
exports.incrementTransactionCount = function(ctx, done) {
var error, error1, fn, i, j, len, len1, metric, orchestration, ref, ref1, transactionStatus;
logger.info('sending counts to statsd for ' + domain + '.' + ctx.authorisedChannel._id);
transactionStatus = ctx.transactionStatus;
try {
sdc.increment(domain + '.channels');
sdc.increment(domain + '.channels.' + transactionStatus);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.statuses.' + transactionStatus);
if (ctx.mediatorResponse != null) {
if (ctx.mediatorResponse.metrics != null) {
ref = ctx.mediatorResponse.metrics;
for (i = 0, len = ref.length; i < len; i++) {
metric = ref[i];
if (metric.type === 'counter') {
logger.info('incrementing mediator counter ' + metric.name);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.mediator_metrics.' + metric.name);
}
if (metric.type === 'timer') {
logger.info('incrementing mediator timer ' + metric.name);
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.mediator_metrics.' + metric.name, metric.value);
}
if (metric.type === 'gauge') {
logger.info('incrementing mediator gauge ' + metric.name);
sdc.gauge(domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.mediator_metrics.' + metric.name, metric.value);
}
}
}
if (ctx.mediatorResponse.orchestrations != null) {
ref1 = ctx.mediatorResponse.orchestrations;
fn = function(orchestration) {
var k, len2, orchestrationName, orchestrationStatus, ref2, results;
orchestrationStatus = orchestration.response.status;
orchestrationName = orchestration.name;
if (orchestration.group) {
orchestrationName = orchestration.group + "." + orchestration.name;
}
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.orchestrations.' + orchestrationName);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.statuses.' + transactionStatus + '.orchestrations.' + orchestrationName);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.statuses.' + transactionStatus + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus);
if (orchestration.metrics != null) {
ref2 = orchestration.metrics;
results = [];
for (k = 0, len2 = ref2.length; k < len2; k++) {
metric = ref2[k];
if (metric.type === 'counter') {
logger.info('incrementing orchestration counter ' + metric.name);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.orchestrations.' + orchestrationName + '.' + metric.name, metric.value);
}
if (metric.type === 'timer') {
logger.info('incrementing orchestration timer ' + metric.name);
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.orchestrations.' + orchestrationName + '.' + metric.name, metric.value);
}
if (metric.type === 'gauge') {
logger.info('incrementing orchestration gauge ' + metric.name);
results.push(sdc.gauge(domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.orchestrations.' + orchestrationName + '.' + metric.name, metric.value));
} else {
results.push(void 0);
}
}
return results;
}
};
for (j = 0, len1 = ref1.length; j < len1; j++) {
orchestration = ref1[j];
fn(orchestration);
}
}
}
} catch (error1) {
error = error1;
logger.error(error, done);
}
return done();
};
exports.measureTransactionDuration = function(ctx, done) {
var error, error1, fn, i, len, orchestration, ref, transactionStatus;
logger.info('sending durations to statsd for ' + domain + '.' + ctx.authorisedChannel._id);
transactionStatus = ctx.transactionStatus;
try {
sdc.timing(domain + '.channels', ctx.timer);
sdc.timing(domain + '.channels.' + transactionStatus, ctx.timer);
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id, ctx.timer);
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.statuses.' + transactionStatus, ctx.timer);
if (ctx.mediatorResponse != null) {
if (ctx.mediatorResponse.orchestrations != null) {
ref = ctx.mediatorResponse.orchestrations;
fn = function(orchestration) {
var orchestrationName, orchestrationStatus, orchestratrionDuration;
orchestratrionDuration = orchestration.response.timestamp - orchestration.request.timestamp;
orchestrationStatus = orchestration.response.status;
orchestrationName = orchestration.name;
if (orchestration.group) {
orchestrationName = orchestration.group + "." + orchestration.name;
}
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.orchestrations.' + orchestrationName, orchestratrionDuration);
return sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus, orchestratrionDuration);
};
for (i = 0, len = ref.length; i < len; i++) {
orchestration = ref[i];
fn(orchestration);
}
}
}
} catch (error1) {
error = error1;
logger.error(error, done);
}
return done();
};
exports.nonPrimaryRouteRequestCount = function(ctx, route, done) {
var fn, i, j, len, len1, metric, orchestration, ref, ref1;
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status);
if (route.metrics != null) {
ref = route.metrics;
for (i = 0, len = ref.length; i < len; i++) {
metric = ref[i];
if (metric.type === 'counter') {
logger.info('incrementing mediator counter ' + metric.name);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.mediator_metrics.' + metric.name);
}
if (metric.type === 'timer') {
logger.info('incrementing mediator timer ' + metric.name);
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.mediator_metrics.' + metric.name, metric.value);
}
if (metric.type === 'gauge') {
logger.info('incrementing mediator gauge ' + metric.name);
sdc.gauge(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.mediator_metrics.' + metric.name, metric.value);
}
}
}
if (route.orchestrations != null) {
ref1 = route.orchestrations;
fn = function(orchestration) {
var k, len2, orchestrationName, orchestrationStatus, ref2, results;
orchestrationStatus = orchestration.response.status;
orchestrationName = orchestration.name;
if (orchestration.group) {
orchestrationName = orchestration.group + "." + orchestration.name;
}
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status + '.orchestrations.' + orchestrationName);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus);
if (orchestration.metrics != null) {
ref2 = orchestration.metrics;
results = [];
for (k = 0, len2 = ref2.length; k < len2; k++) {
metric = ref2[k];
if (metric.type === 'counter') {
logger.info('incrementing ' + route.name + ' orchestration counter ' + metric.name);
sdc.increment(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName + '.' + metric.name, metric.value);
}
if (metric.type === 'timer') {
logger.info('incrementing ' + route.name + 'orchestration timer ' + metric.name);
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName + '.' + metric.name, metric.value);
}
if (metric.type === 'gauge') {
logger.info('incrementing ' + route.name + 'orchestration gauge ' + metric.name);
results.push(sdc.gauge(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName + '.' + metric.name, metric.value));
} else {
results.push(void 0);
}
}
return results;
}
};
for (j = 0, len1 = ref1.length; j < len1; j++) {
orchestration = ref1[j];
fn(orchestration);
}
}
return done();
};
exports.nonPrimaryRouteDurations = function(ctx, route, done) {
var fn, i, len, orchestration, ref;
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name, ctx.timer);
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status, ctx.timer);
if (route.orchestrations != null) {
ref = route.orchestrations;
fn = function(orchestration) {
var orchestrationName, orchestrationStatus, orchestratrionDuration;
orchestratrionDuration = orchestration.response.timestamp - orchestration.request.timestamp;
orchestrationStatus = orchestration.response.status;
orchestrationName = orchestration.name;
if (orchestration.group) {
orchestrationName = orchestration.group + "." + orchestration.name;
}
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName, orchestratrionDuration);
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus, orchestratrionDuration);
sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status + '.orchestrations.' + orchestrationName, orchestratrionDuration);
return sdc.timing(domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus, orchestratrionDuration);
};
for (i = 0, len = ref.length; i < len; i++) {
orchestration = ref[i];
fn(orchestration);
}
}
return done();
};
//# sourceMappingURL=stats.js.map