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(ioredis): prototype running tests in ESM mode #1794

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extension": ["ts"],
"node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm", "experimental-loader=@opentelemetry/instrumentation/hook.mjs"],
"spec": ["test/**/*.test.ts"]
}
16 changes: 14 additions & 2 deletions plugins/node/opentelemetry-instrumentation-ioredis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
"repository": "open-telemetry/opentelemetry-js-contrib",
"type": "commonjs",
"scripts": {
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test:esm": "nyc ts-mocha --config esm.mocharc.json -p tsconfig.esm.json 'test/**/*.test.ts'",
"package:to_esm": "node -e 'var fs=require(\"fs\");var p=require(\"./package.json\");p.type=\"module\";fs.writeFileSync(\"package.json\",JSON.stringify(p,null,2))'",
"package:to_cjs": "node -e 'var fs=require(\"fs\");var p=require(\"./package.json\");p.type=\"commonjs\";fs.writeFileSync(\"package.json\",JSON.stringify(p,null,2))'",
"mocha:patch": "npm run mocha:unpatch; cp ./node_modules/mocha/lib/cli/cli.js ./node_modules/mocha/lib/cli/cli.backup.js && head -n -4 ./node_modules/mocha/lib/cli/cli.backup.js > ./node_modules/mocha/lib/cli/cli.js && echo 'exports.main();' >> ./node_modules/mocha/lib/cli/cli.js",
"mocha:unpatch": "mv ./node_modules/mocha/lib/cli/cli.backup.js ./node_modules/mocha/lib/cli/cli.js",
"prepare:test:esm": "npm run package:to_esm && npm run mocha:patch",
"restore:test:cjs": "npm run package:to_cjs && npm run mocha:unpatch",
"debugging:test:esm:mocha-cli": "node --loader=@opentelemetry/instrumentation/hook.mjs /home/marc/opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-ioredis/node_modules/mocha/lib/cli/cli.js",
"debugging:test:esm:via-node-directly": "RUN_REDIS_TESTS_LOCAL=true node --trace-warnings --experimental-specifier-resolution=node --loader=ts-node/esm --experimental-loader=@opentelemetry/instrumentation/hook.mjs /home/marc/opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-ioredis/node_modules/mocha/lib/cli/cli.js test/**/*.test.ts --no-config --no-package --extension ts --diff --reporter spec --slow 75 --timeout 2000 --ui bdd --watch-ignore node_modules --watch-ignore .git",
"test:debug": "cross-env RUN_REDIS_TESTS_LOCAL=true ts-mocha --inspect-brk --no-timeouts -p tsconfig.json 'test/**/*.test.ts'",
"test:local": "cross-env RUN_REDIS_TESTS_LOCAL=true npm run test",
"test:local:esm": "cross-env RUN_REDIS_TESTS_LOCAL=true npm run test:esm",
"test-all-versions": "tav",
"test-all-versions:local": "cross-env RUN_REDIS_TESTS_LOCAL=true npm run test-all-versions",
"tdd": "npm run test -- --watch-extensions ts --watch",
Expand Down Expand Up @@ -60,12 +71,13 @@
"@types/node": "18.6.5",
"cross-env": "7.0.3",
"ioredis": "5.2.2",
"mocha": "7.2.0",
"mocha": "9.2.2",
"nyc": "15.1.0",
"rimraf": "5.0.5",
"sinon": "15.2.0",
"test-all-versions": "5.0.1",
"ts-mocha": "10.0.0",
"ts-node": "^10.9.1",
"typescript": "4.4.4"
},
"dependencies": {
Expand All @@ -75,4 +87,4 @@
"@types/ioredis4": "npm:@types/ioredis@^4.28.10"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-ioredis#readme"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
? module.default // ESM
: module; // CommonJS
diag.debug('Applying patch for ioredis');
console.log(
'XXX IORedisInstrumentation: esm?',
module[Symbol.toStringTag] === 'Module'
);
if (isWrapped(moduleExports.prototype.sendCommand)) {
this._unwrap(moduleExports.prototype, 'sendCommand');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import * as assert from 'assert';
import * as sinon from 'sinon';
import * as ioredisTypes from 'ioredis';
import { IORedisInstrumentation } from '../src';
import { IORedisInstrumentation } from '../src/index';
import {
IORedisInstrumentationConfig,
DbStatementSerializer,
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('ioredis', () => {
context.disable();
});

before(function () {
before(async function () {
// needs to be "function" to have MochaContext "this" context
if (!shouldTest) {
// this.skip() workaround
Expand All @@ -112,7 +112,7 @@ describe('ioredis', () => {
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
instrumentation = new IORedisInstrumentation();
instrumentation.setTracerProvider(provider);
ioredis = require('ioredis');
ioredis = (await import('ioredis')).default;
});

after(() => {
Expand Down Expand Up @@ -233,11 +233,11 @@ describe('ioredis', () => {
});

describe('Instrumenting query operations', () => {
before(() => {
before(async () => {
instrumentation.disable();
instrumentation = new IORedisInstrumentation();
instrumentation.setTracerProvider(provider);
require('ioredis');
await import('ioredis');
});

IOREDIS_CALLBACK_OPERATIONS.forEach(command => {
Expand Down Expand Up @@ -322,7 +322,7 @@ describe('ioredis', () => {
assert.strictEqual(
exceptionEvent.attributes?.[
SemanticAttributes.EXCEPTION_STACKTRACE
],
],
ex.stack
);
assert.strictEqual(
Expand Down Expand Up @@ -600,7 +600,7 @@ describe('ioredis', () => {
[SemanticAttributes.EXCEPTION_MESSAGE]:
'NOSCRIPT No matching script. Please use EVAL.',
[SemanticAttributes.EXCEPTION_STACKTRACE]:
predictableStackTrace,
predictableStackTrace,
[SemanticAttributes.EXCEPTION_TYPE]: 'ReplyError',
},
name: 'exception',
Expand Down Expand Up @@ -819,11 +819,11 @@ describe('ioredis', () => {
});

describe('Instrumenting with a custom hooks', () => {
before(() => {
before(async () => {
instrumentation.disable();
instrumentation = new IORedisInstrumentation();
instrumentation.setTracerProvider(provider);
require('ioredis');
await import('ioredis');
});

it('should call requestHook when set in config', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../tsconfig.base",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
},
"include": [
"src/**/*.ts",
"test/**/*.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"outDir": "lib",
"noEmit": false,
"declaration": true,
"sourceMap": true,
"noEmitOnError": true,
"stripInternal": true,
"target": "es6",
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"isolatedModules": true,
"typeRoots": ["node_modules/@types", "types"]
},
"include": ["src", "types", "test"],
"exclude": ["node_modules"]
}
Loading