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

Feat/meteor integration #3811

Closed
Closed
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
2 changes: 2 additions & 0 deletions lib/config/normalizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
TRACE_CONTINUATION_STRATEGY_RESTART_EXTERNAL,
CONTEXT_MANAGER_ASYNCHOOKS,
CONTEXT_MANAGER_ASYNCLOCALSTORAGE,
CONTEXT_MANAGER_FIBERS,
} = require('../constants');

/**
Expand Down Expand Up @@ -588,6 +589,7 @@ function normalizeTraceContinuationStrategy(opts, fields, defaults, logger) {
const ALLOWED_CONTEXT_MANAGER = {
[CONTEXT_MANAGER_ASYNCHOOKS]: true,
[CONTEXT_MANAGER_ASYNCLOCALSTORAGE]: true,
[CONTEXT_MANAGER_FIBERS]: true,
};

/**
Expand Down
1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
CAPTURE_ERROR_LOG_STACK_TRACES_ALWAYS: 'always',
CONTEXT_MANAGER_ASYNCHOOKS: 'asynchooks',
CONTEXT_MANAGER_ASYNCLOCALSTORAGE: 'asynclocalstorage',
CONTEXT_MANAGER_FIBERS: 'fibers',
TRACE_CONTINUATION_STRATEGY_CONTINUE: 'continue',
TRACE_CONTINUATION_STRATEGY_RESTART: 'restart',
TRACE_CONTINUATION_STRATEGY_RESTART_EXTERNAL: 'restart_external',
Expand Down
6 changes: 6 additions & 0 deletions lib/instrumentation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const semver = require('semver');
const {
CONTEXT_MANAGER_ASYNCHOOKS,
CONTEXT_MANAGER_ASYNCLOCALSTORAGE,
CONTEXT_MANAGER_FIBERS,
} = require('../constants');
var { Ids } = require('./ids');
var Transaction = require('./transaction');
Expand All @@ -27,6 +28,9 @@ const {
const { getLambdaHandlerInfo } = require('../lambda');
const undiciInstr = require('./modules/undici');
const azureFunctionsInstr = require('./azure-functions');
const {
FibersContextManager,
} = require('./run-context/FibersRunContextManager');

const nodeSupportsAsyncLocalStorage = semver.satisfies(
process.versions.node,
Expand Down Expand Up @@ -417,6 +421,8 @@ Instrumentation.prototype.start = function (runContextClass) {
this._log,
runContextClass,
);
} else if (confContextManager === CONTEXT_MANAGER_FIBERS) {
this._runCtxMgr = new FibersContextManager(this._log, runContextClass);
} else {
if (confContextManager === CONTEXT_MANAGER_ASYNCLOCALSTORAGE) {
this._log.warn(
Expand Down
143 changes: 143 additions & 0 deletions lib/instrumentation/run-context/FibersRunContextManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/

'use strict';

const Fiber = require('fibers');

const { BasicRunContextManager } = require('./BasicRunContextManager');

class FibersContextStore {
getStore() {
return Fiber.current;
}

run(store, callback, ...args) {
if (args.length >= 1) {
return Fiber((...rest) => {
const fiber = Fiber.current;
for (const key in store) {
if (store.hasOwnProperty(key)) {
fiber[key] = store[key];
}
}

Fiber.yield(callback(...rest));
}).run(args);
}
// eslint-disable-next-line new-cap
return Fiber((...rest) => {
const fiber = Fiber.current;
for (const key in store) {
if (store.hasOwnProperty(key)) {
fiber[key] = store[key];
}
}

Fiber.yield(callback(...rest));
}).run();
}
}

/**
* A RunContextManager that uses core node `AsyncLocalStorage` as the mechanism
* for run-context tracking.
*
* (Adapted from https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-context-async-hooks/src/AsyncLocalStorageContextManager.ts)
*/
class FibersContextManager extends BasicRunContextManager {
constructor(log, runContextClass) {
super(log, runContextClass);
this._runContextFromAsyncId = new Map();
this._asyncLocalStorage = new FibersContextStore();
}

enable() {
super.enable();
return this;
}

disable() {
super.disable();
this._runContextFromAsyncId.clear();
const store = this._asyncLocalStorage.getStore();
if (store) store.reset();
return this;
}

// Reset state for re-use of this context manager by tests in the same process.
testReset() {
this.disable();
this._stack = [];
}

active() {
// const store = this._asyncLocalStorage.getStore();
const store = this._stack[this._stack.length - 1];
if (store == null) {
return this.root();
} else {
return store;
}
}

with(runContext, fn, thisArg, ...args) {
const cb = thisArg == null ? fn : fn.bind(thisArg);
return this._asyncLocalStorage.run(runContext, cb, ...args);
}

/**
* Init hook will be called when userland create a async context, setting the
* context as the current one if it exist.
* @param asyncId id of the async context
* @param type the resource type
*/
_init(asyncId, type, triggerAsyncId) {
// ignore TIMERWRAP as they combine timers with same timeout which can lead to
// false context propagation. TIMERWRAP has been removed in node 11
// every timer has it's own `Timeout` resource anyway which is used to propagete
// context.
if (type === 'TIMERWRAP') {
return;
}

const context = this._stack[this._stack.length - 1];
if (context !== undefined) {
this._runContextFromAsyncId.set(asyncId, context);
}
}

/**
* Destroy hook will be called when a given context is no longer used so we can
* remove its attached context.
* @param asyncId id of the async context
*/
_destroy(asyncId) {
this._runContextFromAsyncId.delete(asyncId);
}

/**
* Before hook is called just before executing a async context.
* @param asyncId id of the async context
*/
_before(asyncId) {
const context = this._runContextFromAsyncId.get(asyncId);
if (context !== undefined) {
this._enterRunContext(context);
}
}

/**
* After hook is called just after completing the execution of a async context.
*/
_after() {
this._exitRunContext();
}
}

module.exports = {
FibersContextManager,
};
2 changes: 2 additions & 0 deletions lib/instrumentation/run-context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ const {
const {
AsyncLocalStorageRunContextManager,
} = require('./AsyncLocalStorageRunContextManager');
const { FibersRunContextManager } = require('./FibersRunContextManager');
const { BasicRunContextManager } = require('./BasicRunContextManager');
const { RunContext } = require('./RunContext');

module.exports = {
AsyncHooksRunContextManager,
AsyncLocalStorageRunContextManager,
BasicRunContextManager,
FibersRunContextManager,
RunContext,
};
Loading
Loading