[IMPROVE] Inject metrics on callbacks (#13266)

pull/13248/head^2
Diego Sampaio 6 years ago committed by GitHub
parent 7c6ff2a740
commit dabe271c13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      packages/rocketchat-api/server/v1/im.js
  2. 28
      packages/rocketchat-callbacks/lib/callbacks.js
  3. 1
      packages/rocketchat-callbacks/package.js
  4. 5
      packages/rocketchat-metrics/package.js
  5. 31
      packages/rocketchat-metrics/server/callbacksMetrics.js
  6. 2
      packages/rocketchat-metrics/server/index.js
  7. 29
      packages/rocketchat-metrics/server/lib/metrics.js

@ -12,7 +12,8 @@ function findDirectMessageRoom(params, user) {
type: 'd',
});
if (!room || room.t !== 'd') {
const canAccess = Meteor.call('canAccessRoom', room._id, user._id);
if (!canAccess || !room || room.t !== 'd') {
throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "username" param provided does not match any dirct message');
}

@ -1,6 +1,5 @@
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { metrics, StatsTracker } from 'meteor/rocketchat:metrics';
import _ from 'underscore';
/*
@ -70,6 +69,9 @@ callbacks.remove = function(hook, id) {
callbacks[hook] = getHooks(hook).filter((callback) => callback.id !== id);
};
callbacks.runItem = function({ callback, result, constant /* , hook */ }) {
return callback(result, constant);
};
/*
* Successively run all of a hook's callbacks on an item
@ -85,29 +87,17 @@ callbacks.run = function(hook, item, constant) {
return item;
}
let rocketchatHooksEnd;
if (Meteor.isServer) {
rocketchatHooksEnd = metrics.rocketchatHooks.startTimer({ hook, callbacks_length: callbacks.length });
}
let totalTime = 0;
const result = callbackItems.reduce(function(result, callback) {
let rocketchatCallbacksEnd;
if (Meteor.isServer) {
rocketchatCallbacksEnd = metrics.rocketchatCallbacks.startTimer({ hook, callback: callback.id });
}
const time = callbacks.showTime === true || callbacks.showTotalTime === true ? Date.now() : 0;
const callbackResult = callback(result, constant);
const callbackResult = callbacks.runItem({ hook, callback, result, constant, time });
if (callbacks.showTime === true || callbacks.showTotalTime === true) {
const currentTime = Date.now() - time;
totalTime += currentTime;
if (callbacks.showTime === true) {
if (Meteor.isServer) {
rocketchatCallbacksEnd();
StatsTracker.timing('callbacks.time', currentTime, [`hook:${ hook }`, `callback:${ callback.id }`]);
} else {
if (!Meteor.isServer) {
let stack = callback.stack && typeof callback.stack.split === 'function' && callback.stack.split('\n');
stack = stack && stack[2] && (stack[2].match(/\(.+\)/) || [])[0];
console.log(String(currentTime), hook, callback.id, stack);
@ -117,14 +107,8 @@ callbacks.run = function(hook, item, constant) {
return (typeof callbackResult === 'undefined') ? result : callbackResult;
}, item);
if (Meteor.isServer) {
rocketchatHooksEnd();
}
if (callbacks.showTotalTime === true) {
if (Meteor.isServer) {
StatsTracker.timing('callbacks.totalTime', totalTime, [`hook:${ hook }`]);
} else {
if (!Meteor.isServer) {
console.log(`${ hook }:`, totalTime);
}
}

@ -8,7 +8,6 @@ Package.describe({
Package.onUse(function(api) {
api.use([
'ecmascript',
'rocketchat:metrics',
]);
api.mainModule('client/index.js', 'client');
api.mainModule('server/index.js', 'server');

@ -8,6 +8,11 @@ Package.describe({
Package.onUse(function(api) {
api.use([
'ecmascript',
'rocketchat:callbacks',
'rocketchat:migrations',
'rocketchat:models',
'rocketchat:settings',
'rocketchat:utils',
'rocketchat:version',
]);
api.mainModule('server/index.js', 'server');

@ -0,0 +1,31 @@
import { callbacks } from 'meteor/rocketchat:callbacks';
import { metrics } from './lib/metrics';
import StatsTracker from './lib/statsTracker';
const {
run: originalRun,
runItem: originalRunItem,
} = callbacks;
callbacks.run = function(hook, item, constant) {
const rocketchatHooksEnd = metrics.rocketchatHooks.startTimer({ hook, callbacks_length: callbacks.length });
const result = originalRun(hook, item, constant);
rocketchatHooksEnd();
return result;
};
callbacks.runItem = function({ callback, result, constant, hook, time }) {
const rocketchatCallbacksEnd = metrics.rocketchatCallbacks.startTimer({ hook, callback: callback.id });
const newResult = originalRunItem({ callback, result, constant });
StatsTracker.timing('callbacks.time', (Date.now() - time), [`hook:${ hook }`, `callback:${ callback.id }`]);
rocketchatCallbacksEnd();
return newResult;
};

@ -1,6 +1,8 @@
import { metrics } from './lib/metrics';
import StatsTracker from './lib/statsTracker';
import './callbacksMetrics';
export {
metrics,
StatsTracker,

@ -1,16 +1,16 @@
import { Meteor } from 'meteor/meteor';
import client from 'prom-client';
import connect from 'connect';
import http from 'http';
import _ from 'underscore';
import { Meteor } from 'meteor/meteor';
import { Info } from 'meteor/rocketchat:utils';
import { Migrations } from 'meteor/rocketchat:migrations';
import { settings } from 'meteor/rocketchat:settings';
import { Statistics } from 'meteor/rocketchat:models';
client.collectDefaultMetrics();
export const metrics = {};
let Info;
let _Migrations;
// one sample metrics only - a counter
metrics.meteorMethods = new client.Summary({
name: 'rocketchat_meteor_methods',
@ -77,21 +77,11 @@ metrics.totalDirectMessages = new client.Gauge({ name: 'rocketchat_direct_messag
metrics.totalLivechatMessages = new client.Gauge({ name: 'rocketchat_livechat_messages_total', help: 'total of messages in livechat rooms' });
const setPrometheusData = async() => {
const { settings } = await import('meteor/rocketchat:settings');
client.register.setDefaultLabels({
uniqueId: settings.get('uniqueID'),
siteUrl: settings.get('Site_Url'),
});
const date = new Date();
if (!Info) {
const Utils = await import('meteor/rocketchat:utils');
Info = Utils.Info;
}
if (!_Migrations) {
const { Migrations } = await import('meteor/rocketchat:migrations');
_Migrations = Migrations;
}
client.register.setDefaultLabels({
unique_id: settings.get('uniqueID'),
site_url: settings.get('Site_Url'),
@ -103,11 +93,6 @@ const setPrometheusData = async() => {
metrics.ddpSessions.set(sessions.length, date);
metrics.ddpAthenticatedSessions.set(authenticatedSessions.length, date);
metrics.ddpConnectedUsers.set(_.unique(authenticatedSessions.map((s) => s.userId)).length, date);
const { Statistics } = await import('meteor/rocketchat:models');
if (!Statistics) {
return;
}
const statistics = Statistics.findLast();
if (!statistics) {
@ -115,7 +100,7 @@ const setPrometheusData = async() => {
}
metrics.version.set({ version: statistics.version }, 1, date);
metrics.migration.set(_Migrations._getControl().version, date);
metrics.migration.set(Migrations._getControl().version, date);
metrics.instanceCount.set(statistics.instanceCount, date);
metrics.oplogEnabled.set({ enabled: statistics.oplogEnabled }, 1, date);
@ -171,7 +156,6 @@ const server = http.createServer(app);
let timer;
const updatePrometheusConfig = async() => {
const { settings } = await import('meteor/rocketchat:settings');
const port = settings.get('Prometheus_Port');
const enabled = settings.get('Prometheus_Enabled');
if (port == null || enabled == null) {
@ -191,7 +175,6 @@ const updatePrometheusConfig = async() => {
};
Meteor.startup(async() => {
const { settings } = await import('meteor/rocketchat:settings');
settings.get('Prometheus_Enabled', updatePrometheusConfig);
settings.get('Prometheus_Port', updatePrometheusConfig);
});

Loading…
Cancel
Save