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: missing subscription metrics on monolith #30295

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
41 changes: 19 additions & 22 deletions apps/meteor/app/lib/server/lib/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { WebApp } from 'meteor/webapp';
import _ from 'underscore';

import { setSubscriptionOverwrite } from '../../../../server/lib/instrumentation/registers';
import { getMethodArgs } from '../../../../server/lib/logger/logPayloads';
import { metrics } from '../../../metrics/server';
import { settings } from '../../../settings/server';
Expand Down Expand Up @@ -80,38 +81,34 @@
const originalMeteorMethods = Meteor.methods;

Meteor.methods = function (methodMap) {
_.each(methodMap, (handler, name) => {

Check warning on line 84 in apps/meteor/app/lib/server/lib/debug.js

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Consider using the native Array.prototype.forEach() or Object.entries().forEach()
wrapMethods(name, handler, methodMap);
});
originalMeteorMethods(methodMap);
};

const originalMeteorPublish = Meteor.publish;
setSubscriptionOverwrite(function ({ name, func, args }) {
traceConnection(Log_Trace_Subscriptions, Log_Trace_Subscriptions_Filter, 'subscription', name, this.connection, this.userId);

Meteor.publish = function (name, func) {
return originalMeteorPublish(name, function (...args) {
traceConnection(Log_Trace_Subscriptions, Log_Trace_Subscriptions_Filter, 'subscription', name, this.connection, this.userId);

logger.subscription({
publication: name,
userId: this.userId,
userAgent: this.connection?.httpHeaders['user-agent'],
referer: this.connection?.httpHeaders.referer,
remoteIP: this.connection?.clientAddress,
instanceId: InstanceStatus.id(),
});
logger.subscription({
publication: name,
userId: this.userId,
userAgent: this.connection?.httpHeaders['user-agent'],
referer: this.connection?.httpHeaders.referer,
remoteIP: this.connection?.clientAddress,
instanceId: InstanceStatus.id(),
});

const end = metrics.meteorSubscriptions.startTimer({ subscription: name });
const end = metrics.meteorSubscriptions.startTimer({ subscription: name });

const originalReady = this.ready;
this.ready = function () {
end();
return originalReady.apply(this, args);
};
const originalReady = this.ready;
this.ready = function () {
end();
return originalReady.apply(this, args);
};

return func.apply(this, args);
});
};
return func.apply(this, args);
});

WebApp.rawConnectHandlers.use((req, res, next) => {
res.setHeader('X-Instance-ID', InstanceStatus.id());
Expand Down
11 changes: 11 additions & 0 deletions apps/meteor/server/lib/instrumentation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Meteor } from 'meteor/meteor';

import { runOverwrittenPublish } from './registers';

const originalMeteorPublish = Meteor.publish;

Meteor.publish = function (name, func) {
return originalMeteorPublish(name, function (...args: any) {
return runOverwrittenPublish.call(this, { name, func, args });
});
};
19 changes: 19 additions & 0 deletions apps/meteor/server/lib/instrumentation/registers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type publishReturn = void | Mongo.Cursor<any> | Mongo.Cursor<any>[] | Promise<void | Mongo.Cursor<any> | Mongo.Cursor<any>[]>;

type overwritedPublishParam = {
name: string | null;
func: (...args: any[]) => publishReturn;
args: unknown[];
};

let runPublish = ({ func, args }: overwritedPublishParam): publishReturn => {
return func.apply(this, args);
};

export function setSubscriptionOverwrite(func: (param: overwritedPublishParam) => publishReturn): void {
runPublish = func;
}

export function runOverwrittenPublish(this: Subscription, param: overwritedPublishParam): publishReturn {
return runPublish.call(this, param);
}
1 change: 1 addition & 0 deletions apps/meteor/server/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './lib/instrumentation';
import './models/startup';

/**
Expand Down
Loading