Skip to content

Commit

Permalink
feat(ioredis): Update instrumentation-ioredis to version 5.x.x (open-…
Browse files Browse the repository at this point in the history
  • Loading branch information
savardc authored Mar 2, 2023
1 parent 7180353 commit f5f7ac6
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/opentelemetry-redis-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const serializationSubsets = [

export type DbStatementSerializer = (
cmdName: string,
cmdArgs: Array<string | Buffer | number>
cmdArgs: Array<string | Buffer | number | any[]>
) => string;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ioredis:
# Ignoring v4.19.0. Tests never ends. Caused by https://github.com/luin/ioredis/pull/1219
versions: "^2.5.0 || ^3.2.2 || 4.14.1 || 4.16.3 || 4.17.3 || 4.18.0 || 4.19.2 || 4.19.4 || 4.22.0 || 4.24.5 || 4.26.0 || 4.27.2 || ^4.27.6"
versions: "^2.5.0 || ^3.2.2 || 4.14.1 || 4.16.3 || 4.17.3 || 4.18.0 || 4.19.2 || 4.19.4 || 4.22.0 || 4.24.5 || 4.26.0 || 4.27.2 || ^4.27.6 || 5.0.4 || ^5.2.4"
commands: npm run test

# Fix missing `contrib-test-utils` package
Expand Down
4 changes: 2 additions & 2 deletions plugins/node/opentelemetry-instrumentation-ioredis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ npm install --save @opentelemetry/instrumentation-ioredis

### Supported Versions

- `>=2.0.0 <5`
- `>=2.0.0 <6`

## Usage

Expand Down Expand Up @@ -47,7 +47,7 @@ registerInstrumentations({
IORedis instrumentation has few options available to choose from. You can set the following:

| Options | Type | Description |
| ----------------------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
|-------------------------|---------------------------------------------------|-------------------------------------------------------------------------------------------------------------------|
| `dbStatementSerializer` | `DbStatementSerializer` | IORedis instrumentation will serialize db.statement using the specified function. |
| `requestHook` | `RedisRequestCustomAttributeFunction` (function) | Function for adding custom attributes on db request. Receives params: `span, { moduleVersion, cmdName, cmdArgs }` |
| `responseHook` | `RedisResponseCustomAttributeFunction` (function) | Function for adding custom attributes on db response |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"@types/sinon": "10.0.9",
"@types/node": "18.11.7",
"cross-env": "7.0.3",
"ioredis": "5.2.2",
"gts": "3.1.0",
"ioredis": "4.27.7",
"mocha": "7.2.0",
"nyc": "15.1.0",
"rimraf": "3.0.2",
Expand All @@ -73,7 +73,7 @@
"@opentelemetry/instrumentation": "^0.35.1",
"@opentelemetry/redis-common": "^0.34.0",
"@opentelemetry/semantic-conventions": "^1.0.0",
"@types/ioredis": "4.26.6"
"@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 @@ -15,13 +15,13 @@
*/

import { diag, trace, context, SpanKind } from '@opentelemetry/api';
import type * as ioredisTypes from 'ioredis';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
} from '@opentelemetry/instrumentation';
import { IORedisInstrumentationConfig, IORedisCommand } from './types';
import { IORedisInstrumentationConfig } from './types';
import { IORedisCommand, RedisInterface } from './internal-types';
import {
DbSystemValues,
SemanticAttributes,
Expand All @@ -35,9 +35,7 @@ const DEFAULT_CONFIG: IORedisInstrumentationConfig = {
requireParentSpan: true,
};

export class IORedisInstrumentation extends InstrumentationBase<
typeof ioredisTypes
> {
export class IORedisInstrumentation extends InstrumentationBase<any> {
constructor(_config: IORedisInstrumentationConfig = {}) {
super(
'@opentelemetry/instrumentation-ioredis',
Expand All @@ -46,11 +44,11 @@ export class IORedisInstrumentation extends InstrumentationBase<
);
}

init(): InstrumentationNodeModuleDefinition<typeof ioredisTypes>[] {
init(): InstrumentationNodeModuleDefinition<any>[] {
return [
new InstrumentationNodeModuleDefinition<typeof ioredisTypes>(
new InstrumentationNodeModuleDefinition<any>(
'ioredis',
['>1 <5'],
['>1', '<6'],
(moduleExports, moduleVersion?: string) => {
diag.debug('Applying patch for ioredis');
if (isWrapped(moduleExports.prototype.sendCommand)) {
Expand Down Expand Up @@ -98,7 +96,7 @@ export class IORedisInstrumentation extends InstrumentationBase<

private traceSendCommand = (original: Function, moduleVersion?: string) => {
const instrumentation = this;
return function (this: ioredisTypes.Redis, cmd?: IORedisCommand) {
return function (this: RedisInterface, cmd?: IORedisCommand) {
if (arguments.length < 1 || typeof cmd !== 'object') {
return original.apply(this, arguments);
}
Expand Down Expand Up @@ -184,7 +182,7 @@ export class IORedisInstrumentation extends InstrumentationBase<

private traceConnection = (original: Function) => {
const instrumentation = this;
return function (this: ioredisTypes.Redis) {
return function (this: RedisInterface) {
const config =
instrumentation.getConfig() as IORedisInstrumentationConfig;
const hasNoParentSpan = trace.getSpan(context.active()) === undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 type { Command, Redis } from 'ioredis';
import type * as LegacyIORedis from 'ioredis4';

interface LegacyIORedisCommand {
reject: (err: Error) => void;
resolve: (result: {}) => void;
promise: Promise<{}>;
args: Array<string | Buffer | number>;
callback: LegacyIORedis.CallbackFunction<unknown>;
name: string;
}

export type IORedisCommand = Command | LegacyIORedisCommand;
export type RedisInterface = Redis | LegacyIORedis.Redis;
25 changes: 6 additions & 19 deletions plugins/node/opentelemetry-instrumentation-ioredis/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@
* limitations under the License.
*/

import type * as ioredisTypes from 'ioredis';
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { Span } from '@opentelemetry/api';

export interface IORedisCommand {
reject: (err: Error) => void;
resolve: (result: {}) => void;
promise: Promise<{}>;
args: Array<string | Buffer | number>;
callback: ioredisTypes.CallbackFunction<unknown>;
name: string;
}
export type CommandArgs = Array<string | Buffer | number | any[]>;

/**
* Function that can be used to serialize db.statement tag
Expand All @@ -35,14 +27,14 @@ export interface IORedisCommand {
* @returns serialized string that will be used as the db.statement attribute.
*/
export type DbStatementSerializer = (
cmdName: IORedisCommand['name'],
cmdArgs: IORedisCommand['args']
cmdName: string,
cmdArgs: CommandArgs
) => string;

export interface IORedisRequestHookInformation {
moduleVersion?: string;
cmdName: IORedisCommand['name'];
cmdArgs: IORedisCommand['args'];
cmdName: string;
cmdArgs: CommandArgs;
}

export interface RedisRequestCustomAttributeFunction {
Expand All @@ -59,12 +51,7 @@ export interface RedisRequestCustomAttributeFunction {
* The type of the response varies depending on the specific command.
*/
export interface RedisResponseCustomAttributeFunction {
(
span: Span,
cmdName: IORedisCommand['name'],
cmdArgs: IORedisCommand['args'],
response: unknown
): void;
(span: Span, cmdName: string, cmdArgs: CommandArgs, response: unknown): void;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const sanitizeEventForAssertion = (span: ReadableSpan) => {

describe('ioredis', () => {
const provider = new NodeTracerProvider();
let ioredis: typeof ioredisTypes;
let ioredis: typeof ioredisTypes.default;
let instrumentation: IORedisInstrumentation;
const shouldTestLocal = process.env.RUN_REDIS_TESTS_LOCAL;
const shouldTest = process.env.RUN_REDIS_TESTS || shouldTestLocal;
Expand Down Expand Up @@ -188,22 +188,22 @@ describe('ioredis', () => {
name: string;
args: Array<string>;
expectedDbStatement: string;
method: (cb: ioredisTypes.CallbackFunction<unknown>) => unknown;
method: (cb: ioredisTypes.Callback<unknown>) => unknown;
}> = [
{
description: 'insert',
name: 'hset',
args: [hashKeyName, 'testField', 'testValue'],
expectedDbStatement: `${hashKeyName} testField [1 other arguments]`,
method: (cb: ioredisTypes.CallbackFunction<number>) =>
method: (cb: ioredisTypes.Callback<number>) =>
client.hset(hashKeyName, 'testField', 'testValue', cb),
},
{
description: 'get',
name: 'get',
args: [testKeyName],
expectedDbStatement: `${testKeyName}`,
method: (cb: ioredisTypes.CallbackFunction<string | null>) =>
method: (cb: ioredisTypes.Callback<string | null>) =>
client.get(testKeyName, cb),
},
];
Expand Down Expand Up @@ -960,7 +960,7 @@ describe('ioredis', () => {
describe('setConfig - custom dbStatementSerializer config', () => {
const dbStatementSerializer = (
cmdName: string,
cmdArgs: Array<string | Buffer | number>
cmdArgs: Array<string | Buffer | number | any>
) => {
return Array.isArray(cmdArgs) && cmdArgs.length
? `FooBar_${cmdName} ${cmdArgs.join(',')}`
Expand Down

0 comments on commit f5f7ac6

Please sign in to comment.