Skip to content

Commit

Permalink
Merge branch 'feature/fiberless' into feature/async-events
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/test.yml
  • Loading branch information
leonardoventurini committed Nov 8, 2023
2 parents ab37fdb + c5e4966 commit c3980f5
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 187 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
matrix:
meteorRelease:
# Meteor 3 Alpha
- '--release 3.0-alpha.16'
- '--release 3.0-alpha.17'
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand All @@ -30,7 +30,4 @@ jobs:
- name: Run Tests
run: |
# Fix using old versions of Meteor
export NODE_TLS_REJECT_UNAUTHORIZED=0
mtest --package ./ --once ${{ matrix.meteorRelease }}
80 changes: 0 additions & 80 deletions experimental.js

This file was deleted.

5 changes: 1 addition & 4 deletions lib/auto_connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,9 @@ Kadira._connectWithSettings = function () {
* Previously there was two log messages (one for instrumentation,
* and another for connection), this way we merged both of them.
*/

console.log('Monti APM: Instrumentation started...');

Kadira._startInstrumenting(function () {
Kadira._connectWithEnv();
Kadira._connectWithSettings();

console.log('Monti APM: Instrumentation completed');
}).catch(console.error);
});
2 changes: 1 addition & 1 deletion lib/common/unify.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Monti = Kadira;
Kadira.wrapAsync = Meteor.wrapAsync;

if (Meteor.isServer) {
const EventEmitter = require('events').EventEmitter;
const EventEmitter = Npm.require('events').EventEmitter;
const eventBus = new EventEmitter();
eventBus.setMaxListeners(0);

Expand Down
1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export const EventType = {
HTTP: 'http',
Start: 'start',
Wait: 'wait',
Email: 'email',
};
11 changes: 6 additions & 5 deletions lib/hijack/db/get-cursor-data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { pick } from '../../utils';

export function getCursorData ({ type, cursor }) {
const cursorDescription = cursor._cursorDescription;

Expand All @@ -11,12 +9,15 @@ export function getCursorData ({ type, cursor }) {
});

if (cursorDescription.options) {
let cursorOptions = pick(cursorDescription.options, ['fields', 'projection', 'sort', 'limit']);
for (let field in cursorOptions) {
let value = cursorOptions[field];
const fields = ['fields', 'projection', 'sort', 'limit'];

for (let field of fields) {
let value = cursorDescription.options[field];

if (typeof value === 'object') {
value = JSON.stringify(value);
}

payload[field] = value;
}
}
Expand Down
3 changes: 0 additions & 3 deletions lib/hijack/db/hijack-async-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ export function hijackAsyncMethods () {
}

return Kadira.tracer.asyncEvent(EventType.DB, payload, null, async (event) => {
// this cause V8 to avoid any performance optimizations, but this is must use
// otherwise, if the error adds try catch block our logs get messy and didn't work
// see: issue #6
const result = await originalFunc.apply(this, arguments);
// handling functions which can be triggered with an asyncCallback
let endData = {};
Expand Down
7 changes: 4 additions & 3 deletions lib/hijack/email.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { pick } from '../utils';
import { EventType } from '../constants';

const CAPTURED_OPTIONS = ['from', 'to', 'cc', 'bcc', 'replyTo', 'messageId'];

const getWrapper = (originalSend, eventName = 'email') => function wrapper (options) {
const data = pick(options, CAPTURED_OPTIONS);
const getWrapper = (originalSend, func) => function wrapper (options) {
const data = { ...pick(options, CAPTURED_OPTIONS), func };

return Kadira.tracer.asyncEvent(eventName, data, null, () => originalSend.call(this, options));
return Kadira.tracer.asyncEvent(EventType.Email, data, null, () => originalSend.call(this, options));
};

if (Package['email']) {
Expand Down
16 changes: 3 additions & 13 deletions lib/hijack/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,9 @@ export function handleErrorEvent (eventEmitter, trace, event) {
}

export function wrapFs () {
// Some npm packages will do fs calls in the
// callback of another fs call.
// This variable is set with the kadiraInfo while
// a callback is run so we can track other fs calls
let fsKadiraInfo = null;

let originalStat = fs.stat;
fs.stat = function () {
const kadiraInfo = Kadira._getInfo() || fsKadiraInfo;
const kadiraInfo = Kadira._getInfo();

if (kadiraInfo) {
let event = Kadira.tracer.event(kadiraInfo.trace, EventType.FS, {
Expand All @@ -47,11 +41,7 @@ export function wrapFs () {
wrapCallback(arguments, (cb) => function () {
Kadira.tracer.eventEnd(kadiraInfo.trace, event);

try {
cb(...arguments);
} finally {
fsKadiraInfo = null;
}
cb(...arguments);
});
}

Expand All @@ -60,7 +50,7 @@ export function wrapFs () {

let originalCreateReadStream = fs.createReadStream;
fs.createReadStream = function () {
const kadiraInfo = Kadira._getInfo() || fsKadiraInfo;
const kadiraInfo = Kadira._getInfo();
let stream = originalCreateReadStream.apply(this, arguments);

if (kadiraInfo) {
Expand Down
21 changes: 2 additions & 19 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ Package.onTest(function (api) {
'tests/event_loop_monitor.js',
], 'server');

if (canRunTestsWithFetch()) {
api.addFiles(['tests/hijack/http_fetch.js'], 'server');
}
api.addFiles(['tests/hijack/http_fetch.js'], 'server');

// common client
api.addFiles([
Expand All @@ -110,21 +108,6 @@ Package.onTest(function (api) {
], ['client', 'server']);
});

// use meteor/fetch in tests only for NodeJS 8.11+ (Meteor 1.7+)
function canRunTestsWithFetch () {
const nums = process.versions.node.split('.').map(Number);

const major = nums[0];
const minor = nums[1];

if (major < 8) return false;

if (major > 8) return true;

// major === 8 and ...
return minor >= 11;
}

function configurePackage (api, isTesting) {
api.versionsFrom('[email protected]');
api.use('montiapm:[email protected]', ['server']);
Expand All @@ -143,7 +126,7 @@ function configurePackage (api, isTesting) {
api.use(['http', 'email'], 'server', { weak: !isTesting });

api.use('fetch', 'server', {
weak: !(isTesting && canRunTestsWithFetch()),
weak: !isTesting,
});

api.use(['random', 'ecmascript', 'tracker'], ['client']);
Expand Down
7 changes: 6 additions & 1 deletion tests/hijack/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ addAsyncTest(

let events = getLastMethodEvents([0, 2, 3]);

let expected = [['start',{userId: null,params: '[10,"abc"]'}],['wait',{waitOn: []},{at: 1,endAt: 1}],['db',{coll: 'tinytest-data',func: 'insertAsync'},{at: 1,endAt: 1}],['complete']];
let expected = [
['start',{userId: null,params: '[10,"abc"]'}],
['wait',{waitOn: []},{at: 1,endAt: 1}],
['db',{coll: 'tinytest-data',func: 'insertAsync'},{at: 1,endAt: 1}],
['complete']
];

test.stableEqual(events, expected);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/hijack/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ addAsyncTest(
const expected = [
['start'],
['wait'],
['emailAsync'],
['email'],
['complete']
];

Expand Down
7 changes: 6 additions & 1 deletion tests/hijack/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ addAsyncTest(

let events = getLastMethodEvents([0, 2, 3]);

let expected = [['start',{userId: null,params: '[]'}],['wait',{waitOn: []},{at: 1,endAt: 1}],['db',{coll: 'tinytest-data',func: 'insertAsync'},{at: 1,endAt: 1}],['complete']];
let expected = [
['start',{userId: null,params: '[]'}],
['wait',{waitOn: []},{at: 1,endAt: 1}],
['db',{coll: 'tinytest-data',func: 'insertAsync'},{at: 1,endAt: 1}],
['complete']
];

test.stableEqual(events, expected);
}
Expand Down
92 changes: 41 additions & 51 deletions tests/hijack/webapp.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,47 @@
import { WebApp } from 'meteor/webapp';
import { wrapWebApp } from '../../lib/hijack/wrap_webapp';
import { addAsyncTest, releaseParts } from '../_helpers/helpers';
import { addAsyncTest } from '../_helpers/helpers';

// Check if Meteor 1.7 or newer, which are the
// versions that wrap connect handlers in a fiber and are easy
// to wrap the static middleware
const httpMonitoringEnabled = releaseParts[0] > 1 ||
(releaseParts[0] > 0 && releaseParts[1] > 6);

if (httpMonitoringEnabled) {
wrapWebApp();

addAsyncTest(
'Webapp - return connect app from .use',
async function (test) {
const result = WebApp.expressHandlers.use((req, res, next) => {
next();
});

test.equal(result, WebApp.expressHandlers);
}
);

addAsyncTest(
'Webapp - filter headers',
async function (test) {
Kadira.tracer.redactField('x--test--authorization');

let req = {
url: '/test',
method: 'GET',
headers: {
'content-type': 'application/json',
'content-length': '1000',
'x--test--authorization': 'secret'
}
};

const firstMiddleware = WebApp.rawExpressHandlers.parent._router.stack[0].handle;
addAsyncTest(
'Webapp - return connect app from .use',
async function (test) {
const result = WebApp.expressHandlers.use((req, res, next) => {
next();
});

await new Promise((resolve) => {
firstMiddleware(
req,
{ on () {} },
function () {
const expected = JSON.stringify({
'content-type': 'application/json',
'content-length': '1000',
'x--test--authorization': 'Monti: redacted'
});
test.equal(req.__kadiraInfo.trace.events[0].data.headers, expected);
resolve();
test.equal(result, WebApp.expressHandlers);
}
);

addAsyncTest(
'Webapp - filter headers',
async function (test) {
Kadira.tracer.redactField('x--test--authorization');

let req = {
url: '/test',
method: 'GET',
headers: {
'content-type': 'application/json',
'content-length': '1000',
'x--test--authorization': 'secret'
}
};

const firstMiddleware = WebApp.rawExpressHandlers.parent._router.stack[0].handle;

await new Promise((resolve) => {
firstMiddleware(
req,
{ on () {} },
function () {
const expected = JSON.stringify({
'content-type': 'application/json',
'content-length': '1000',
'x--test--authorization': 'Monti: redacted'
});
});
test.equal(req.__kadiraInfo.trace.events[0].data.headers, expected);
resolve();
});
});
}
});
Loading

0 comments on commit c3980f5

Please sign in to comment.