From 7c540cd7a54e4493f878fdbc73e1348cd3c35073 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Mon, 6 Jun 2022 17:55:37 -0600 Subject: [PATCH 1/2] Fix prom-client new promise usage and convert metrics file to ts --- .../{collectMetrics.js => collectMetrics.ts} | 51 ++++++++++--------- .../externals/meteor/facts-base.d.ts | 5 ++ .../definition/externals/meteor/mongo.d.ts | 1 + 3 files changed, 34 insertions(+), 23 deletions(-) rename apps/meteor/app/metrics/server/lib/{collectMetrics.js => collectMetrics.ts} (77%) create mode 100644 apps/meteor/definition/externals/meteor/facts-base.d.ts diff --git a/apps/meteor/app/metrics/server/lib/collectMetrics.js b/apps/meteor/app/metrics/server/lib/collectMetrics.ts similarity index 77% rename from apps/meteor/app/metrics/server/lib/collectMetrics.js rename to apps/meteor/app/metrics/server/lib/collectMetrics.ts index 46e32dd1d1d8..61eacebba057 100644 --- a/apps/meteor/app/metrics/server/lib/collectMetrics.js +++ b/apps/meteor/app/metrics/server/lib/collectMetrics.ts @@ -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 => { 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('uniqueID'), + // eslint-disable-next-line @typescript-eslint/camelcase + site_url: settings.get('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); @@ -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); @@ -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 = ` Rocket.Chat Prometheus Exporter @@ -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 = { @@ -122,19 +124,19 @@ const was = { resetInterval: 0, collectGC: false, }; -const updatePrometheusConfig = async () => { +const updatePrometheusConfig = async (): Promise => { 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('Prometheus_Enabled'), + resetInterval: settings.get('Prometheus_Reset_Interval'), + collectGC: settings.get('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; } @@ -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); } @@ -177,7 +182,7 @@ const updatePrometheusConfig = async () => { } if (is.collectGC && gcStatsInitiated === false) { gcStatsInitiated = true; - gcStats()(); + gcStats(client.register)(); } } catch (error) { SystemLogger.error(error); diff --git a/apps/meteor/definition/externals/meteor/facts-base.d.ts b/apps/meteor/definition/externals/meteor/facts-base.d.ts new file mode 100644 index 000000000000..9b56d44d5ce2 --- /dev/null +++ b/apps/meteor/definition/externals/meteor/facts-base.d.ts @@ -0,0 +1,5 @@ +declare module 'meteor/facts-base' { + namespace Facts { + function incrementServerFact(pkg: 'pkg' | 'fact', fact: string | number, increment: number): void; + } +} diff --git a/apps/meteor/definition/externals/meteor/mongo.d.ts b/apps/meteor/definition/externals/meteor/mongo.d.ts index 7efb5b5b0860..7c973f888777 100644 --- a/apps/meteor/definition/externals/meteor/mongo.d.ts +++ b/apps/meteor/definition/externals/meteor/mongo.d.ts @@ -12,6 +12,7 @@ declare module 'meteor/mongo' { onSkippedEntries(callback: Function): void; waitUntilCaughtUp(): void; _defineTooFarBehind(value: number): void; + _entryQueue?: unknown[]; } interface MongoConnection { From 1a62154c4d405daca6f7f0fd2651dbdafa7a4d50 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 7 Jun 2022 07:22:32 -0600 Subject: [PATCH 2/2] for some reason the types package dissapeared from yarn.lock --- apps/meteor/package.json | 1 + yarn.lock | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/meteor/package.json b/apps/meteor/package.json index aabd707580e3..04bd5bc7a2e2 100644 --- a/apps/meteor/package.json +++ b/apps/meteor/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index bd6380a7cf07..0f32d0a54e46 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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" @@ -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