Skip to content

Commit f5f7ac6

Browse files
authored
feat(ioredis): Update instrumentation-ioredis to version 5.x.x (open-telemetry#1121)
1 parent 7180353 commit f5f7ac6

File tree

8 files changed

+54
-40
lines changed

8 files changed

+54
-40
lines changed

packages/opentelemetry-redis-common/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const serializationSubsets = [
4444

4545
export type DbStatementSerializer = (
4646
cmdName: string,
47-
cmdArgs: Array<string | Buffer | number>
47+
cmdArgs: Array<string | Buffer | number | any[]>
4848
) => string;
4949

5050
/**

plugins/node/opentelemetry-instrumentation-ioredis/.tav.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ioredis:
22
# Ignoring v4.19.0. Tests never ends. Caused by https://github.com/luin/ioredis/pull/1219
3-
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"
3+
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"
44
commands: npm run test
55

66
# Fix missing `contrib-test-utils` package

plugins/node/opentelemetry-instrumentation-ioredis/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ npm install --save @opentelemetry/instrumentation-ioredis
1717

1818
### Supported Versions
1919

20-
- `>=2.0.0 <5`
20+
- `>=2.0.0 <6`
2121

2222
## Usage
2323

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

4949
| Options | Type | Description |
50-
| ----------------------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
50+
|-------------------------|---------------------------------------------------|-------------------------------------------------------------------------------------------------------------------|
5151
| `dbStatementSerializer` | `DbStatementSerializer` | IORedis instrumentation will serialize db.statement using the specified function. |
5252
| `requestHook` | `RedisRequestCustomAttributeFunction` (function) | Function for adding custom attributes on db request. Receives params: `span, { moduleVersion, cmdName, cmdArgs }` |
5353
| `responseHook` | `RedisResponseCustomAttributeFunction` (function) | Function for adding custom attributes on db response |

plugins/node/opentelemetry-instrumentation-ioredis/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
"@types/sinon": "10.0.9",
6060
"@types/node": "18.11.7",
6161
"cross-env": "7.0.3",
62+
"ioredis": "5.2.2",
6263
"gts": "3.1.0",
63-
"ioredis": "4.27.7",
6464
"mocha": "7.2.0",
6565
"nyc": "15.1.0",
6666
"rimraf": "3.0.2",
@@ -73,7 +73,7 @@
7373
"@opentelemetry/instrumentation": "^0.35.1",
7474
"@opentelemetry/redis-common": "^0.34.0",
7575
"@opentelemetry/semantic-conventions": "^1.0.0",
76-
"@types/ioredis": "4.26.6"
76+
"@types/ioredis4": "npm:@types/ioredis@^4.28.10"
7777
},
7878
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-ioredis#readme"
7979
}

plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*/
1616

1717
import { diag, trace, context, SpanKind } from '@opentelemetry/api';
18-
import type * as ioredisTypes from 'ioredis';
1918
import {
2019
InstrumentationBase,
2120
InstrumentationNodeModuleDefinition,
2221
isWrapped,
2322
} from '@opentelemetry/instrumentation';
24-
import { IORedisInstrumentationConfig, IORedisCommand } from './types';
23+
import { IORedisInstrumentationConfig } from './types';
24+
import { IORedisCommand, RedisInterface } from './internal-types';
2525
import {
2626
DbSystemValues,
2727
SemanticAttributes,
@@ -35,9 +35,7 @@ const DEFAULT_CONFIG: IORedisInstrumentationConfig = {
3535
requireParentSpan: true,
3636
};
3737

38-
export class IORedisInstrumentation extends InstrumentationBase<
39-
typeof ioredisTypes
40-
> {
38+
export class IORedisInstrumentation extends InstrumentationBase<any> {
4139
constructor(_config: IORedisInstrumentationConfig = {}) {
4240
super(
4341
'@opentelemetry/instrumentation-ioredis',
@@ -46,11 +44,11 @@ export class IORedisInstrumentation extends InstrumentationBase<
4644
);
4745
}
4846

49-
init(): InstrumentationNodeModuleDefinition<typeof ioredisTypes>[] {
47+
init(): InstrumentationNodeModuleDefinition<any>[] {
5048
return [
51-
new InstrumentationNodeModuleDefinition<typeof ioredisTypes>(
49+
new InstrumentationNodeModuleDefinition<any>(
5250
'ioredis',
53-
['>1 <5'],
51+
['>1', '<6'],
5452
(moduleExports, moduleVersion?: string) => {
5553
diag.debug('Applying patch for ioredis');
5654
if (isWrapped(moduleExports.prototype.sendCommand)) {
@@ -98,7 +96,7 @@ export class IORedisInstrumentation extends InstrumentationBase<
9896

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

185183
private traceConnection = (original: Function) => {
186184
const instrumentation = this;
187-
return function (this: ioredisTypes.Redis) {
185+
return function (this: RedisInterface) {
188186
const config =
189187
instrumentation.getConfig() as IORedisInstrumentationConfig;
190188
const hasNoParentSpan = trace.getSpan(context.active()) === undefined;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import type { Command, Redis } from 'ioredis';
17+
import type * as LegacyIORedis from 'ioredis4';
18+
19+
interface LegacyIORedisCommand {
20+
reject: (err: Error) => void;
21+
resolve: (result: {}) => void;
22+
promise: Promise<{}>;
23+
args: Array<string | Buffer | number>;
24+
callback: LegacyIORedis.CallbackFunction<unknown>;
25+
name: string;
26+
}
27+
28+
export type IORedisCommand = Command | LegacyIORedisCommand;
29+
export type RedisInterface = Redis | LegacyIORedis.Redis;

plugins/node/opentelemetry-instrumentation-ioredis/src/types.ts

+6-19
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type * as ioredisTypes from 'ioredis';
1817
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
1918
import { Span } from '@opentelemetry/api';
2019

21-
export interface IORedisCommand {
22-
reject: (err: Error) => void;
23-
resolve: (result: {}) => void;
24-
promise: Promise<{}>;
25-
args: Array<string | Buffer | number>;
26-
callback: ioredisTypes.CallbackFunction<unknown>;
27-
name: string;
28-
}
20+
export type CommandArgs = Array<string | Buffer | number | any[]>;
2921

3022
/**
3123
* Function that can be used to serialize db.statement tag
@@ -35,14 +27,14 @@ export interface IORedisCommand {
3527
* @returns serialized string that will be used as the db.statement attribute.
3628
*/
3729
export type DbStatementSerializer = (
38-
cmdName: IORedisCommand['name'],
39-
cmdArgs: IORedisCommand['args']
30+
cmdName: string,
31+
cmdArgs: CommandArgs
4032
) => string;
4133

4234
export interface IORedisRequestHookInformation {
4335
moduleVersion?: string;
44-
cmdName: IORedisCommand['name'];
45-
cmdArgs: IORedisCommand['args'];
36+
cmdName: string;
37+
cmdArgs: CommandArgs;
4638
}
4739

4840
export interface RedisRequestCustomAttributeFunction {
@@ -59,12 +51,7 @@ export interface RedisRequestCustomAttributeFunction {
5951
* The type of the response varies depending on the specific command.
6052
*/
6153
export interface RedisResponseCustomAttributeFunction {
62-
(
63-
span: Span,
64-
cmdName: IORedisCommand['name'],
65-
cmdArgs: IORedisCommand['args'],
66-
response: unknown
67-
): void;
54+
(span: Span, cmdName: string, cmdArgs: CommandArgs, response: unknown): void;
6855
}
6956

7057
/**

plugins/node/opentelemetry-instrumentation-ioredis/test/ioredis.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const sanitizeEventForAssertion = (span: ReadableSpan) => {
8181

8282
describe('ioredis', () => {
8383
const provider = new NodeTracerProvider();
84-
let ioredis: typeof ioredisTypes;
84+
let ioredis: typeof ioredisTypes.default;
8585
let instrumentation: IORedisInstrumentation;
8686
const shouldTestLocal = process.env.RUN_REDIS_TESTS_LOCAL;
8787
const shouldTest = process.env.RUN_REDIS_TESTS || shouldTestLocal;
@@ -188,22 +188,22 @@ describe('ioredis', () => {
188188
name: string;
189189
args: Array<string>;
190190
expectedDbStatement: string;
191-
method: (cb: ioredisTypes.CallbackFunction<unknown>) => unknown;
191+
method: (cb: ioredisTypes.Callback<unknown>) => unknown;
192192
}> = [
193193
{
194194
description: 'insert',
195195
name: 'hset',
196196
args: [hashKeyName, 'testField', 'testValue'],
197197
expectedDbStatement: `${hashKeyName} testField [1 other arguments]`,
198-
method: (cb: ioredisTypes.CallbackFunction<number>) =>
198+
method: (cb: ioredisTypes.Callback<number>) =>
199199
client.hset(hashKeyName, 'testField', 'testValue', cb),
200200
},
201201
{
202202
description: 'get',
203203
name: 'get',
204204
args: [testKeyName],
205205
expectedDbStatement: `${testKeyName}`,
206-
method: (cb: ioredisTypes.CallbackFunction<string | null>) =>
206+
method: (cb: ioredisTypes.Callback<string | null>) =>
207207
client.get(testKeyName, cb),
208208
},
209209
];
@@ -960,7 +960,7 @@ describe('ioredis', () => {
960960
describe('setConfig - custom dbStatementSerializer config', () => {
961961
const dbStatementSerializer = (
962962
cmdName: string,
963-
cmdArgs: Array<string | Buffer | number>
963+
cmdArgs: Array<string | Buffer | number | any>
964964
) => {
965965
return Array.isArray(cmdArgs) && cmdArgs.length
966966
? `FooBar_${cmdName} ${cmdArgs.join(',')}`

0 commit comments

Comments
 (0)