Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Fix prom-client new promise usage #25781

Merged
merged 2 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ import { SystemLogger } from '../../../../server/lib/logger/system';
import { metrics } from './metrics';
import { getAppsStatistics } from '../../../statistics/server/lib/getAppsStatistics';

Facts.incrementServerFact = function (pkg, fact, increment) {
Facts.incrementServerFact = function (pkg: 'pkg' | 'fact', fact: string | number, increment: number): void {
metrics.meteorFacts.inc({ pkg, fact }, increment);
};

const setPrometheusData = async () => {
const setPrometheusData = async (): Promise<void> => {
metrics.info.set(
{
version: Info.version,
unique_id: settings.get('uniqueID'),
site_url: settings.get('Site_Url'),
// eslint-disable-next-line @typescript-eslint/camelcase
unique_id: settings.get<string>('uniqueID'),
// eslint-disable-next-line @typescript-eslint/camelcase
site_url: settings.get<string>('Site_Url'),
},
1,
);

const sessions = Array.from(Meteor.server.sessions.values());
const sessions = Array.from<{ userId: string }>(Meteor.server.sessions.values());
const authenticatedSessions = sessions.filter((s) => s.userId);
metrics.ddpSessions.set(Meteor.server.sessions.size);
metrics.ddpAuthenticatedSessions.set(authenticatedSessions.length);
Expand All @@ -53,7 +55,7 @@ const setPrometheusData = async () => {
metrics.version.set({ version: statistics.version }, 1);
metrics.migration.set(getControl().version);
metrics.instanceCount.set(statistics.instanceCount);
metrics.oplogEnabled.set({ enabled: statistics.oplogEnabled }, 1);
metrics.oplogEnabled.set({ enabled: `${statistics.oplogEnabled}` }, 1);

// User statistics
metrics.totalUsers.set(statistics.totalUsers);
Expand Down Expand Up @@ -85,17 +87,17 @@ const app = connect();
// const compression = require('compression');
// app.use(compression());

app.use('/metrics', (req, res) => {
app.use('/metrics', (_req, res) => {
res.setHeader('Content-Type', 'text/plain');
const data = client.register.metrics();
client.register.metrics().then((data) => {
metrics.metricsRequests.inc();
metrics.metricsSize.set(data.length);

metrics.metricsRequests.inc();
metrics.metricsSize.set(data.length);

res.end(data);
res.end(data);
});
});

app.use('/', (req, res) => {
app.use('/', (_req, res) => {
const html = `<html>
<head>
<title>Rocket.Chat Prometheus Exporter</title>
Expand All @@ -112,8 +114,8 @@ app.use('/', (req, res) => {

const server = http.createServer(app);

let timer;
let resetTimer;
let timer: number;
let resetTimer: number;
let defaultMetricsInitiated = false;
let gcStatsInitiated = false;
const was = {
Expand All @@ -122,19 +124,19 @@ const was = {
resetInterval: 0,
collectGC: false,
};
const updatePrometheusConfig = async () => {
const updatePrometheusConfig = async (): Promise<void> => {
const is = {
port: process.env.PROMETHEUS_PORT || settings.get('Prometheus_Port'),
enabled: settings.get('Prometheus_Enabled'),
resetInterval: settings.get('Prometheus_Reset_Interval'),
collectGC: settings.get('Prometheus_Garbage_Collector'),
enabled: settings.get<boolean>('Prometheus_Enabled'),
resetInterval: settings.get<number>('Prometheus_Reset_Interval'),
collectGC: settings.get<boolean>('Prometheus_Garbage_Collector'),
};

if (Object.values(is).some((s) => s == null)) {
return;
}

if (Object.entries(is).every(([k, v]) => v === was[k])) {
if (Object.entries(is).every(([k, v]) => v === was[k as keyof typeof was])) {
return;
}

Expand Down Expand Up @@ -162,8 +164,11 @@ const updatePrometheusConfig = async () => {
Meteor.clearInterval(resetTimer);
if (is.resetInterval) {
resetTimer = Meteor.setInterval(() => {
client.register.getMetricsAsArray().forEach((metric) => {
metric.hashMap = {};
client.register.getMetricsAsArray().then((metrics) => {
metrics.forEach((metric) => {
// @ts-expect-error
metric.hashMap = {};
});
});
}, is.resetInterval);
}
Expand All @@ -177,7 +182,7 @@ const updatePrometheusConfig = async () => {
}
if (is.collectGC && gcStatsInitiated === false) {
gcStatsInitiated = true;
gcStats()();
gcStats(client.register)();
}
} catch (error) {
SystemLogger.error(error);
Expand Down
5 changes: 5 additions & 0 deletions apps/meteor/definition/externals/meteor/facts-base.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'meteor/facts-base' {
namespace Facts {
function incrementServerFact(pkg: 'pkg' | 'fact', fact: string | number, increment: number): void;
}
}
1 change: 1 addition & 0 deletions apps/meteor/definition/externals/meteor/mongo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare module 'meteor/mongo' {
onSkippedEntries(callback: Function): void;
waitUntilCaughtUp(): void;
_defineTooFarBehind(value: number): void;
_entryQueue?: unknown[];
}

interface MongoConnection {
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"@types/nodemailer": "^6.4.4",
"@types/parseurl": "^1.3.1",
"@types/photoswipe": "^4.1.2",
"@types/prometheus-gc-stats": "^0.6.2",
"@types/psl": "^1.1.0",
"@types/react": "~17.0.42",
"@types/react-dom": "~17.0.14",
Expand Down
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4819,6 +4819,7 @@ __metadata:
"@types/object-path": ^0.11.1
"@types/parseurl": ^1.3.1
"@types/photoswipe": ^4.1.2
"@types/prometheus-gc-stats": ^0.6.2
"@types/proxy-from-env": ^1.0.1
"@types/psl": ^1.1.0
"@types/react": ~17.0.42
Expand Down Expand Up @@ -7671,6 +7672,13 @@ __metadata:
languageName: node
linkType: hard

"@types/prometheus-gc-stats@npm:^0.6.2":
version: 0.6.2
resolution: "@types/prometheus-gc-stats@npm:0.6.2"
checksum: 403b3dbd792b83e592376e2002260cf57fb18f98c8b8528a24dc65e545cb8d0e9bf9941dc28edfa397b670b9a7336913da991005ef0278611209dde9b51406db
languageName: node
linkType: hard

"@types/prop-types@npm:*":
version: 15.7.4
resolution: "@types/prop-types@npm:15.7.4"
Expand Down Expand Up @@ -8695,7 +8703,7 @@ __metadata:
human-interval: ~1.0.0
moment-timezone: ~0.5.27
mongodb: ~3.5.0
checksum: acb4ebb7e7356f6e53e810d821eb6aa3d88bbfb9e85183e707517bee6d1eea1f189f38bdf0dd2b91360492ab7643134d510c320d2523d86596498ab98e59735b
checksum: f5f68008298f9482631f1f494e392cd6b8ba7971a3b0ece81ae2abe60f53d67973ff4476156fa5c9c41b8b58c4ccd284e95c545e0523996dfd05f9a80b843e07
languageName: node
linkType: hard

Expand Down