Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
os: process.platform,
osRelease: os.release(),
}, 1);
// Collect default prometheus metrics every 10 seconds
const collectDefaultMetrics = prom.collectDefaultMetrics;
collectDefaultMetrics();
// Create metric histogram and summary for api response times
const responseTimeHistogram = new prom.Histogram({
name: 'http_request_duration_seconds',
help: `Time in seconds consumed from ${version.name} receiving a request until a response is sent per path`,
labelNames: ['path'],
});
const responseTimeSummary = new prom.Summary({
name: 'http_request_duration_seconds_total',
help: `Time in seconds consumed from ${version.name} receiving a request until a response is sent in total`,
});
// function for recording time consumed for a request and adding as metric
function recordResponseTimes() {
return async function responseTime(ctx, next) {
const requestTime = Date.now();
await next();
const diff = Math.ceil((Date.now() - requestTime) / 1000);
responseTimeSummary.observe(diff);
responseTimeHistogram.observe({ path: ctx.request.url }, diff);
if (diff > Config.allowedResponseTime) {
logger.warn(`Request for endpoint ${ctx.request.url} took ${diff} s, which is longer than allowed ${Config.allowedResponseTime} s`);
}
};
// one sample metrics only - a counter
RocketChat.metrics.meteorMethods = new client.Summary({
name: 'rocketchat_meteor_methods',
help: 'summary of meteor methods count and time',
labelNames: ['method', 'has_connection', 'has_user'],
});
RocketChat.metrics.rocketchatCallbacks = new client.Summary({
name: 'rocketchat_callbacks',
help: 'summary of rocketchat callbacks count and time',
labelNames: ['hook', 'callback'],
});
RocketChat.metrics.rocketchatHooks = new client.Summary({
name: 'rocketchat_hooks',
help: 'summary of rocketchat hooks count and time',
labelNames: ['hook', 'callbacks_length'],
});
RocketChat.metrics.rocketchatRestApi = new client.Summary({
name: 'rocketchat_rest_api',
help: 'summary of rocketchat rest api count and time',
labelNames: ['method', 'entrypoint', 'user_agent', 'status', 'version'],
});
RocketChat.metrics.meteorSubscriptions = new client.Summary({
name: 'rocketchat_meteor_subscriptions',
help: 'summary of meteor subscriptions count and time',
labelNames: ['subscription'],
});
chan.refCounter.unref("LibraryModule::handleUncache");
return;
}
chan.logger.log("[library] " + user.getName() + " deleted " + data.id +
"from the library");
chan.refCounter.unref("LibraryModule::handleUncache");
});
};
const librarySearchQueryCount = new Counter({
name: 'cytube_library_search_query_count',
help: 'Counter for number of channel library searches',
labelNames: ['source']
});
const librarySearchResultSize = new Summary({
name: 'cytube_library_search_results_size',
help: 'Summary for number of channel library results returned',
labelNames: ['source']
});
LibraryModule.prototype.handleSearchMedia = function (user, data) {
var query = data.query.substring(0, 100);
var searchYT = function () {
librarySearchQueryCount.labels('yt').inc(1, new Date());
InfoGetter.Getters.ytSearch(query, function (e, vids) {
if (!e) {
librarySearchResultSize.labels('yt')
.observe(vids.length, new Date());
user.socket.emit("searchResults", {
source: "yt",
results: vids
});
labelNames: ['hook', 'callback'],
});
RocketChat.metrics.rocketchatHooks = new client.Summary({
name: 'rocketchat_hooks',
help: 'summary of rocketchat hooks count and time',
labelNames: ['hook', 'callbacks_length'],
});
RocketChat.metrics.rocketchatRestApi = new client.Summary({
name: 'rocketchat_rest_api',
help: 'summary of rocketchat rest api count and time',
labelNames: ['method', 'entrypoint', 'user_agent', 'status', 'version'],
});
RocketChat.metrics.meteorSubscriptions = new client.Summary({
name: 'rocketchat_meteor_subscriptions',
help: 'summary of meteor subscriptions count and time',
labelNames: ['subscription'],
});
RocketChat.metrics.messagesSent = new client.Counter({ name: 'rocketchat_message_sent', help: 'cumulated number of messages sent' });
RocketChat.metrics.notificationsSent = new client.Counter({ name: 'rocketchat_notification_sent', labelNames: ['notification_type'], help: 'cumulated number of notifications sent' });
RocketChat.metrics.ddpSessions = new client.Gauge({ name: 'rocketchat_ddp_sessions_count', help: 'number of open ddp sessions' });
RocketChat.metrics.ddpAthenticatedSessions = new client.Gauge({ name: 'rocketchat_ddp_sessions_auth', help: 'number of authenticated open ddp sessions' });
RocketChat.metrics.ddpConnectedUsers = new client.Gauge({ name: 'rocketchat_ddp_connected_users', help: 'number of unique connected users' });
RocketChat.metrics.version = new client.Gauge({ name: 'rocketchat_version', labelNames: ['version'], help: 'Rocket.Chat version' });
RocketChat.metrics.migration = new client.Gauge({ name: 'rocketchat_migration', help: 'migration versoin' });
RocketChat.metrics.instanceCount = new client.Gauge({ name: 'rocketchat_instance_count', help: 'instances running' });
RocketChat.metrics.oplogEnabled = new client.Gauge({ name: 'rocketchat_oplog_enabled', labelNames: ['enabled'], help: 'oplog enabled' });
opts.summary.registers = [register];
}
if (prefix) {
defaultOpts.prefix = prefix;
opts.histogram.name = `${prefix}${opts.histogram.name}`;
opts.summary.name = `${prefix}${opts.summary.name}`;
}
Object.keys(metrics)
.filter(opts.hasOwnProperty.bind(opts))
.forEach((key) => {
Object.assign(opts[key], metrics[key]);
});
client.collectDefaultMetrics(defaultOpts);
const routeHist = new client.Histogram(opts.histogram);
const routeSum = new client.Summary(opts.summary);
if (endpoint) {
fastify.route({
url: endpoint,
method: 'GET',
schema: {hide: true},
handler: (_, reply) => {
const data = register
? register.metrics()
: client.register.metrics();
reply.type('text/plain').send(data);
},
});
}
fastify.addHook('onRequest', (request, _, next) => {
import client from 'prom-client';
import connect from 'connect';
import http from 'http';
import _ from 'underscore';
import { Meteor } from 'meteor/meteor';
import { Info } from '../../../utils';
import { Migrations } from '../../../migrations';
import { settings } from '../../../settings';
import { Statistics } from '../../../models';
client.collectDefaultMetrics();
export const metrics = {};
metrics.meteorMethods = new client.Summary({
name: 'rocketchat_meteor_methods',
help: 'summary of meteor methods count and time',
labelNames: ['method', 'has_connection', 'has_user'],
});
metrics.rocketchatCallbacks = new client.Summary({
name: 'rocketchat_callbacks',
help: 'summary of rocketchat callbacks count and time',
labelNames: ['hook', 'callback'],
});
metrics.rocketchatHooks = new client.Summary({
name: 'rocketchat_hooks',
help: 'summary of rocketchat hooks count and time',
labelNames: ['hook', 'callbacks_length'],
});
protected createSummaryInternal(options: PercentiledMetricOptions): Summary {
return new SummaryProxy(
this.initializeMetric(
new prom.Summary(this.getMetricConstruct(options)),
options,
(metric: prom.Summary, labels: Labels) => metric.observe(labels as prom.labelValues, 0),
),
options.labelNames,
);
}
export const metrics = {};
metrics.meteorMethods = new client.Summary({
name: 'rocketchat_meteor_methods',
help: 'summary of meteor methods count and time',
labelNames: ['method', 'has_connection', 'has_user'],
});
metrics.rocketchatCallbacks = new client.Summary({
name: 'rocketchat_callbacks',
help: 'summary of rocketchat callbacks count and time',
labelNames: ['hook', 'callback'],
});
metrics.rocketchatHooks = new client.Summary({
name: 'rocketchat_hooks',
help: 'summary of rocketchat hooks count and time',
labelNames: ['hook', 'callbacks_length'],
});
metrics.rocketchatRestApi = new client.Summary({
name: 'rocketchat_rest_api',
help: 'summary of rocketchat rest api count and time',
labelNames: ['method', 'entrypoint', 'user_agent', 'status', 'version'],
});
metrics.meteorSubscriptions = new client.Summary({
name: 'rocketchat_meteor_subscriptions',
help: 'summary of meteor subscriptions count and time',
labelNames: ['subscription'],
});
});
const testGauge = new client.Gauge({
name: 'gaugeName',
help: 'gaugeHelp',
registers: [register]
});
const testHistogram = new client.Histogram({
name: 'histogramName',
help: 'histogramHelp',
labelNames: ['label1'],
buckets: [0.1, 5, 15, 50, 100, 500]
});
const testSummary = new client.Summary({
name: 'summaryName',
help: 'summaryHelp'
});
/**
* INITIALIZE METRICS
*/
const initCounters = function() {
testCounter.inc(
{
label1: 'Test Label'
},
0
);
testGauge.set(0);
function initPrometheus(app) {
const latency = new Summary({
name: 'cytube_http_req_duration_seconds',
help: 'HTTP Request latency from execution of the first middleware '
+ 'until the "finish" event on the response object.',
labelNames: ['method', 'statusCode']
});
const requests = new Counter({
name: 'cytube_http_req_total',
help: 'HTTP Request count',
labelNames: ['method', 'statusCode']
});
app.use((req, res, next) => {
const startTime = process.hrtime();
res.on('finish', () => {
try {
const diff = process.hrtime(startTime);