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(instrumentation): add back support for absolute paths via require-in-the-middle #3457

Merged
merged 7 commits into from
Dec 5, 2022
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All notable changes to experimental packages in this project will be documented

* fix(instrumentation-xhr): http.url attribute should be absolute [#3200](https://github.com/open-telemetry/opentelemetry-js/pull/3200) @t2t2
* fix(instrumentation-grpc): always set grpc semcov status code attribute with numeric value [#3076](https://github.com/open-telemetry/opentelemetry-js/pull/3076) @blumamir
* fix(instrumentation): add back support for absolute paths via `require-in-the-middle` [#3457](https://github.com/open-telemetry/opentelemetry-js/pull/3457) @mhassan1

### :books: (Refine Doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from './RequireInTheMiddleSingleton';
import { InstrumentationModuleDefinition } from './types';
import { diag } from '@opentelemetry/api';
import * as RequireInTheMiddle from 'require-in-the-middle';

/**
* Base abstract class for instrumenting node plugins
Expand All @@ -33,7 +34,7 @@ export abstract class InstrumentationBase<T = any>
implements types.Instrumentation
{
private _modules: InstrumentationModuleDefinition<T>[];
private _hooks: Hooked[] = [];
private _hooks: (Hooked | RequireInTheMiddle.Hooked)[] = [];
private _requireInTheMiddleSingleton: RequireInTheMiddleSingleton =
RequireInTheMiddleSingleton.getInstance();
private _enabled = false;
Expand Down Expand Up @@ -166,21 +167,26 @@ export abstract class InstrumentationBase<T = any>

this._warnOnPreloadedModules();
for (const module of this._modules) {
this._hooks.push(
this._requireInTheMiddleSingleton.register(
module.name,
(exports, name, baseDir) => {
return this._onRequire<typeof exports>(
module as unknown as InstrumentationModuleDefinition<
typeof exports
>,
exports,
name,
baseDir
);
}
)
);
const onRequire: RequireInTheMiddle.OnRequireFn = (
exports,
name,
baseDir
) => {
return this._onRequire<typeof exports>(
module as unknown as InstrumentationModuleDefinition<typeof exports>,
exports,
name,
baseDir
);
};

// `RequireInTheMiddleSingleton` does not support absolute paths.
// For an absolute paths, we must create a separate instance of `RequireInTheMiddle`.
const hook = path.isAbsolute(module.name)
? RequireInTheMiddle([module.name], { internals: true }, onRequire)
: this._requireInTheMiddleSingleton.register(module.name, onRequire);

this._hooks.push(hook);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

import * as assert from 'assert';
import * as sinon from 'sinon';
import * as path from 'path';
import {
InstrumentationBase,
InstrumentationModuleDefinition,
InstrumentationNodeModuleDefinition,
InstrumentationNodeModuleFile,
} from '../../src';

const MODULE_NAME = 'test-module';
Expand Down Expand Up @@ -284,4 +287,97 @@ describe('InstrumentationBase', () => {
});
});
});

describe('enable/disable', () => {
describe('AND a normal module name', () => {
type Exports = Record<string, unknown>;
type ExportsPatched = Exports & { __patched?: boolean };
const moduleName = 'net';
class TestInstrumentation extends InstrumentationBase<Exports> {
constructor() {
super('@opentelemetry/instrumentation-net-test', '0.0.0', {
enabled: false,
});
}
init(): InstrumentationNodeModuleDefinition<Exports>[] {
return [
new InstrumentationNodeModuleDefinition<Exports>(
moduleName,
['*'],
(exports: ExportsPatched) => {
exports.__patched = true;
return exports;
},
(exports: ExportsPatched) => {
exports.__patched = false;
return exports;
}
),
];
}
}

const instrumentation = new TestInstrumentation();

it('should patch the module', () => {
instrumentation.enable();
const exportsPatched = require(moduleName);
assert.equal(exportsPatched.__patched, true, 'after enable');
instrumentation.disable();
assert.equal(exportsPatched.__patched, false, 'after disable');
instrumentation.enable();
assert.equal(exportsPatched.__patched, true, 'after re-enable');
});
});

describe('AND an absolute path module name', () => {
type Exports = Record<string, unknown>;
type ExportsPatched = Exports & { __patched?: boolean };
const moduleName = 'absolutePathTestFixture';
const fileName = path.join(__dirname, 'fixtures', `${moduleName}.js`);
class TestInstrumentation extends InstrumentationBase<Exports> {
constructor() {
super('@opentelemetry/instrumentation-absolute-path-test', '0.0.0', {
enabled: false,
});
}
init(): InstrumentationNodeModuleDefinition<Exports>[] {
return [
new InstrumentationNodeModuleDefinition<Exports>(
fileName,
['*'],
undefined,
undefined,
[
new InstrumentationNodeModuleFile(
moduleName,
['*'],
(exports: ExportsPatched) => {
exports.__patched = true;
return exports;
},
(exports?: ExportsPatched) => {
if (exports) exports.__patched = false;
return exports;
}
),
]
),
];
}
}

const instrumentation = new TestInstrumentation();

it('should patch the module', () => {
instrumentation.enable();
const exportsPatched = require(fileName);
assert.equal(exportsPatched.__patched, true, 'after enable');
instrumentation.disable();
assert.equal(exportsPatched.__patched, false, 'after disable');
instrumentation.enable();
assert.equal(exportsPatched.__patched, true, 'after re-enable');
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module.exports = {};