Skip to content

Commit

Permalink
[FIX] Fix prom-client new promise usage (#25781)
Browse files Browse the repository at this point in the history
<!-- This is a pull request template, you do not need to uncomment or remove the comments, they won't show up in the PR text. -->

<!-- Your Pull Request name should start with one of the following tags
  [NEW] For new features
  [IMPROVE] For an improvement (performance or little improvements) in existing features
  [FIX] For bug fixes that affect the end-user
  [BREAK] For pull requests including breaking changes
  Chore: For small tasks
  Doc: For documentation
-->

<!-- Checklist!!! If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code. 
  - I have read the Contributing Guide - https://github.com/RocketChat/Rocket.Chat/blob/develop/.github/CONTRIBUTING.md#contributing-to-rocketchat doc
  - I have signed the CLA - https://cla-assistant.io/RocketChat/Rocket.Chat
  - Lint and unit tests pass locally with my changes
  - I have added tests that prove my fix is effective or that my feature works (if applicable)
  - I have added necessary documentation (if applicable)
  - Any dependent changes have been merged and published in downstream modules
-->

## Proposed changes (including videos or screenshots)
<!-- CHANGELOG -->
<!--
  Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request.
  If it fixes a bug or resolves a feature request, be sure to link to that issue below.
  This description will appear in the release notes if we accept the contribution.
-->

<!-- END CHANGELOG -->

## Issue(s)
<!-- Link the issues being closed by or related to this PR. For example, you can use #594 if this PR closes issue number 594 -->

## Steps to test or reproduce
<!-- Mention how you would reproduce the bug if not mentioned on the issue page already. Also mention which screens are going to have the changes if applicable -->

## Further comments
<!-- If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc... -->
Looks like the update of de dep had breaking changes and now uses promises at some calls. This PR fixes those promise usages and converts the file to TS (hopefully it helps us to spot the error in the future)
  • Loading branch information
KevLehman authored Jun 8, 2022
1 parent 4391ca6 commit b021a2f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
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
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4848,6 +4848,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 @@ -7661,6 +7662,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

0 comments on commit b021a2f

Please sign in to comment.