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

test(ioredis): test ioredis ESM usage #1752

Closed
wants to merge 1 commit into from
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
Expand Up @@ -6,7 +6,7 @@
"types": "build/src/index.d.ts",
"repository": "open-telemetry/opentelemetry-js-contrib",
"scripts": {
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test": "cross-env NODE_NO_WARNINGS=1 NODE_OPTIONS='--loader @opentelemetry/instrumentation/hook.mjs' nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts' 'test/**/*.test.mjs'",
"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-all-versions": "tav",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
: module; // CommonJS
console.log(
'XXX IORedisInstrumentation: esm?',
module[Symbol.toStringTag] === 'Module'
);
diag.debug('Applying patch for ioredis');
if (isWrapped(moduleExports.prototype.sendCommand)) {
this._unwrap(moduleExports.prototype, 'sendCommand');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.
*/

import assert from 'assert';

import { context } from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import testUtils from '@opentelemetry/contrib-test-utils';
import Redis from 'ioredis';

import { IORedisInstrumentation } from '../build/src/index.js';

const CONFIG = {
host: process.env.OPENTELEMETRY_REDIS_HOST || 'localhost',
port: parseInt(process.env.OPENTELEMETRY_REDIS_PORT || '63790', 10),
};
const REDIS_URL = `redis://${CONFIG.host}:${CONFIG.port}`;

describe('ioredis ESM', () => {
const shouldTestLocal = process.env.RUN_REDIS_TESTS_LOCAL;
const shouldTest = process.env.RUN_REDIS_TESTS || shouldTestLocal;
const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
let instrumentation;
let contextManager;
let client;

before(() => {
if (!shouldTest) {
this.skip();
}
if (shouldTestLocal) {
testUtils.startDocker('redis');
}

provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
instrumentation = new IORedisInstrumentation();
instrumentation.setTracerProvider(provider);

client = new Redis(REDIS_URL);
});

after(async () => {
await client.quit();
if (shouldTestLocal) {
testUtils.cleanUpDocker('redis');
}
});

beforeEach(() => {
contextManager = new AsyncHooksContextManager().enable();
context.setGlobalContextManager(contextManager);
});
afterEach(() => {
context.disable();
});

it('should create spans', async () => {
// Use a random part in key names because redis instance is used for parallel running tests.
const randomId = ((Math.random() * 2 ** 32) >>> 0).toString(16);
const testKeyName = `test-${randomId}`;

const tracer = provider.getTracer('ioredis-test');
await tracer.startActiveSpan('manual', async (span) => {
client.set(testKeyName, 'bar');
let val = await client.get(testKeyName);
assert(val === 'bar');
span.end();
});

const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 3);
assert.strictEqual(spans[0].name, 'set');
assert.strictEqual(spans[0].attributes['db.system'], 'redis');
assert.strictEqual(spans[1].name, 'get');
assert.strictEqual(spans[1].attributes['db.system'], 'redis');
assert.strictEqual(spans[2].name, 'manual');
});
});

Loading